Multiple Forms & Procedures. Form Methods: –Show, Hide, Activate, Close Events: –Load, Activated, Closing, Closed.

Slides:



Advertisements
Similar presentations
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7- 1 STARTING OUT WITH Visual Basic 2008 FOURTH EDITION Tony Gaddis.
Advertisements

Multiple Forms, Standard Modules, And Menus
Practical Programming COMP153-08S Lecture: Repetition Continued.
Chapter 5 Menus, Common Dialog Boxes, Sub Procedures, and Function Procedures Copyright © 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved.
Chapter 6 Multiform Projects Copyright © 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved. McGraw-Hill.
Writing General Procedures Often you will encounter programming situations in which multiple procedures perform the same operation This condition can occur.
Addison Wesley is an imprint of © 2011 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 7 Multiple Forms, Modules,
.NET Framework.NET Framework class libraries: A large set of classes that forms the basis for objects that can be used programmatically. –Programming in.
Compunet Corporation Programming with Visual Studio.NET GUI Week 13 Tariq Aziz and Kevin Jones.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 7- 1.
Odds and Ends Component Tray Menu and contextmenu Splash Screen.
Chapter 5 - Menus, Sub Procedures, and Sub Functions  Menus - controls - properties and events –menu editor - create and change –defining menus - menu.
VB.Net Decisions. The If … Then Statement If condition Then Statements End If If condition Then Statements Else Statements End If Condition: –Simple condition:
Scope of Variables and Constants A Variable or Constant may exist and be Visible for an entire project, for only one form, or for only one procedure Therefore,
Creating Menu. Objectives Create a menu system for a form –Create a menu –Create a menu titles –Create a menu items –Create a submenu –Modify menu –Edit.
VB.Net Introduction. Visual Studio 2010 Demo Start page: New project/ Open project/Recent projects Starting project: File/New Project/ –C# or VB –Windows.
Copyright © 2014 Pearson Education, Inc. Chapter 7 Multiple Forms, Modules, and Menus.
Microsoft Visual Basic 2005 CHAPTER 8 Using Procedures and Exception Handling.
Apply Sub Procedures/Methods and User Defined Functions
Visual Basic Fundamental Concepts. Integrated Development Enviroment Generates startup form for new project on which to place controls. Features toolbox.
Microsoft Visual Basic 2012 Using Procedures and Exception Handling CHAPTER SEVEN.
CHAPTER SIX Reducing Program Complexity General Sub Procedures and Developer-defined Functions.
Microsoft Visual Basic 2008 CHAPTER 8 Using Procedures and Exception Handling.
Microsoft Visual Basic 2008 CHAPTER NINE Using Arrays and File Handling.
Microsoft Visual Basic 2005 CHAPTER 9 Using Arrays and File Handling.
Multiple Forms, Standard Modules, And Menus
Multiple Forms and Standard Modules
Chapter 6 Understanding the Structure of an Application: Procedures, Modules, and Classes.
Why to Create a Procedure
Using Arrays and File Handling
5-1 aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf.
1 Κατανεμημένες Διαδικτυακές Εφαρμογές Πολυμέσων Γιάννης Πετράκης.
McGraw-Hill © 2009 The McGraw-Hill Companies, Inc. All rights reserved. Chapter 5 Menus, Common Dialog Boxes, Sub Procedures, and Function Procedures.
© Minder Chen, ASP.NET 2.0: Introduction - 1 ASP.NET 2.0 Minder Chen, Ph.D. Framework Base Class Library ADO.NET: Data & XML.
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 4 I Need a Tour Guide.
Chapter 5 Menus, Common Dialog Boxes, and Methods Programming in C#.NET © 2003 by The McGraw-Hill Companies, Inc. All rights reserved.
Lecture 8 Visual Basic (2).
Introduction to Visual Basic.NET Chapter 2 Introduction to Controls, Events.
VB Procedures. Procedures. Sub procedure: Private/Public Sub SubName(Arguments) … End Sub Private: Can only be accessed by procedures in the same form.
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 7 Using Menus, Common Dialogs, Procedures, Functions, and Arrays.
Chapter 5 Menus, Common Dialog Boxes, Sub Procedures, and Function Procedures Copyright © 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved.
McGraw-Hill © 2009 The McGraw-Hill Companies, Inc. All rights reserved. Chapter 6 Multiform Projects.
IMS 3253: Subroutines 1 Dr. Lawrence West, MIS Dept., University of Central Florida Topics Procedures Subroutines Parameters –By Value.
CS0004: Introduction to Programming Project 1 – Lessons Learned.
3.2 VB.NET Events An Event Procedure Properties and Event Procedures of the Form Tab Order of Controls Exercises.
PROGRAMMING IN VISUAL BASIC.NET VISUAL BASIC PROGRAMMING FUNDAMENTALS Bilal Munir Mughal 1 Chapter-8.
Introduction to ADO.Net and VS Database Tools and Data Binding ISYS 350.
VB Classes ISYS 512/812. Object-Oriented Concepts Abstraction: –To create a model of an object, for the purpose of determining the characteristics (properties)
Created by Alia Al-Abdulkarim 2008 Visual Basic Vs. Java.
VB.Net Introduction. Visual Studio 2008 It supports VB.Net, J#, C#, and C++. Demo: –Start page: Recent projects –Starting project: File/New Project/Project.
Week Procedures And Functions 7 A procedure is a collection of statements that performs a task.
6-1 aslkjdhfalskhjfgalsdkfhalskdhjfglaskdhjflaskdhjfglaksjdhflakshflaksdhjfglaksjhflaksjhf.
Creating Menus Menu Bar – behaves like standard Windows menus Can be used in place of or in addition to buttons to execute a procedure Menu items are controls.
Chapter 5 Menus, Sub Procedures and Sub Functions Programming In Visual Basic.NET.
ADO.NET Objects Data Adapters Dr. Ron Eaglin. Agenda Builds on Information in Part I Should have working knowledge of creating a database connection Continuation.
Creating New Forms Projects can appear more professional when using different windows for different types of information. Select Add Windows Form from.
Visual Basic Declaring Variables Dim x as Integer = 0 In the statement above, x is being declared as an Integer (whole number) and is initialised.
Chapter 7 Multiple Forms, Modules, and Menus. Section 7.2 MODULES A module contains code—declarations and procedures—that are used by other files in a.
Computer Science Up Down Controls, Decisions and Random Numbers.
Chapter 8 Multiple Forms, Modules, and Menus. Introduction This chapter demonstrates how to: – Add multiple forms to a project – Create a module to hold.
Chapter 7, Slide 1Starting Out with Visual Basic 3 rd Edition Chapter 7 Multiple Forms, Standard Modules, And Menus.
Visual Basic Fundamental Concepts
Working with Forms in Visual Basic
Visual Basic..
Chapter 6 Multiform Projects
1.الدوال Function 2.الاجراءاتSub Procedure 3.وحده نمطيه Add Module
Multiple Forms, Modules, and Menus
CIS16 Application Development and Programming using Visual Basic.net
VB.Net Introduction.
GUI Programming in Visual Studio .NET
Presentation transcript:

Multiple Forms & Procedures

Form Methods: –Show, Hide, Activate, Close Events: –Load, Activated, Closing, Closed

Form Closing Event Example If MessageBox.Show("Are you sure?", "Warning", MessageBoxButtons.YesNo) = DialogResult.Yes Then e.Cancel = False Else e.Cancel = True End If

Modeless or Modal Modeless form: Other forms can receive input focus while this form remains active. –formName.Show() Modal form: No other form can receive focus while this form remains active. –formName.ShowDialog()

Multiple Forms Two forms: Form1, Form2 To Open Form2 from Form1: Dim f2 As New Form2() f2.Show() Open Form2 as a Modal form: f2.ShowDialog() Note: Form is defined as a class. Must create an instance of the form class by using the keyword New to access the form.

SharingVariables Among Forms Define these variables with class-level scope using the Public keyword. –Must use formName to qualify variables. – –Dim f2 As New Form2() –TextBox1.Text = f2.g2 *** g2 is declared in form2

SharingVariables Among Forms Define these variables with project-level scope in a module using the Public keyword: –Module Module1 – Public testVar As Integer –End Module –Note: Use Project/Add Windows form to add a module.

Modules A file contains code such as: –Variable declarations –Procedures and functions Variables and procedures used by more than one form should store in a module. Global variables: Public

Starting Application with Sub Main The Sub Main procedure must reside in a module. Choose Sub Main as startup object. The code in Main will execute. –Write code in Main to open forms.

Sub Main Example Public Sub main() Dim dateToday As Date dateToday = Today If dateToday.DayOfWeek = DayOfWeek.Saturday Or dateToday.DayOfWeek = DayOfWeek.Sunday Then Dim frmWeekEnd As New Form2() frmWeekEnd.ShowDialog() Else Dim frmWeekDay As New Form1() frmWeekDay.ShowDialog() End If End Sub

Declare Form’s Instance Variable in An Open Form Event Procedure Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim f2 As New Form2() f2.Show() End Sub Problem: This will create a new instance of the form every time the button is clicked.

Declare Form’s Instance Variable in a Module as Public Variable Module Module1 Public f1 As New Form1() Public f2 As New Form2() End Module It can be accessed in a procedure: Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click f2.Show() End Sub Problem: Once the form is closed the variable is no longer exists. Attempt to access it again will trigger error.

Close Form Or Hide Form Use a form’s Closing event to change Close to Hide: Private Sub Form2_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing e.Cancel() = True Me.Hide() End Sub

Main Menu Control –Add MainMenu control and follow the TypeHere instruction. –Each submenu and each item on a submenu is represented by a MenuItem control. –Use an & to specify an access key in the caption. Ex. &File, Sho&s –Write an event procedure for each menu item.

Shortcut keys –Select the Shortcut key property in the MenuItem’s property window. –Select the Shorcut key from list. –Set ShowShortcut property to true. Separator bar –Right clock a menu item/Insert Separator Inserting, deleting a menu item –Right click and select the option. Rearranging menu items –dragging

Context Menu A context menu is a menu that displays when an object on the screen is right- clicked. Add the ContextMenu control (it is placed in a tray under the form). Right-click the control and choose Edit to create the menu. Use the object’s ContextMenu property to bind the object to the context menu.

Simple Text Editor Textbox’s properties, methods –Help –Object Browser System.Windows.Forms Menu: Undo, Copy, Cut, Paste, SelectAll, Search Clipboard: –Clipboard.SetDataObject(TextBox1.SelectedText) –Clipboard.GetDataObject()

Procedures. Sub procedure: Private/Public Sub SubName(Arguments) … End Sub Private: Can only be accessed by procedures in the same form. Public: Can be accessed by procedures in other forms. To call a sub procedure SUB1 Call SUB1(Argument1, Argument2, …)

Function Private Function tax(salary) As Double tax = salary * 0.1 End Function –Or Private Function tax(salary) Return salary * 0.1 End Function

Call by Reference Call by Value ByRef –Default –The address of the item is passed. Any changes made to the passing variable are made to the variable itself. ByVal –Only the variable’s value is passed.

ByRef, ByVal example Private Sub Command1_Click() Dim myStr As String myStr = TextBox1.Text ChangeTextRef (myStr) TextBox1.Text = myStr End Sub Private Sub ChangeTextRef(ByRef strInput As String) strInput = "New Text" End Sub

Input, Output Arguments Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim sal, tax As Single sal = CSng(TextBox1.Text) Call CalTax(sal, tax) TextBox2.Text = tax.ToString End Sub Private Sub CalTax(ByVal Salary As Single, ByRef Tax As Single) Tax = 0.1 * Salary End Sub Can we pass the Tax ByVal?

Static Variables Static VariableName as DataType Static variables are local variables but are not destroyed when a procedure terminates. Their value are kept.

Static Variable Example Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Static clickCounter As Integer = 0 clickCounter += 1 If clickCounter > 3 Then MsgBox("Sorry, you can only click 3 times!") Button1.Enabled = False End If End Sub

Event Procedures Example: –Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click The Handles keyword –Procedure name may change Handling multiple events: –Private Sub AllButtons_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click, Button2.CLick

Using One Event Handler to Handle Events Generated by Many Controls Assume we have 3 buttons. Use the Handles clause in the event procedure to associate controls with the event procedure. We can assign a value for each control’s Tag property, or use control’s TabIndex property to determine which control generates the event.

Private Sub Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click, Button2.Click, Button3.Click If sender.tag = "1" Then MessageBox.Show("button 1 clicked") ElseIf sender.tag = "2" Then MessageBox.Show("button 2 clicked") Else MessageBox.Show("button 3 clicked") End If End Sub Note: VB IntelliSense will not show the Tag property after you type sender.

Early Binding/Late Binding Early binding: VB compiler knows the object’s data type. It enables the use of IntelliSense. Late binding: VB compiler can’t determine the type of object that we are calling. This occurs because the object is declared as Object data type.

Demo: Phone Simulator