+ All Categories
Home > Documents > CVEV 118/698 Visual Basic Lecture 3 Prof. Mounir Mabsout Elsa Sulukdjian Walid El Asmar.

CVEV 118/698 Visual Basic Lecture 3 Prof. Mounir Mabsout Elsa Sulukdjian Walid El Asmar.

Date post: 21-Dec-2015
Category:
View: 215 times
Download: 2 times
Share this document with a friend
22
CVEV 118/698 CVEV 118/698 Visual Basic Visual Basic Lecture 3 Lecture 3 Prof. Mounir Mabsout Prof. Mounir Mabsout Elsa Sulukdjian Elsa Sulukdjian Walid El Asmar Walid El Asmar
Transcript
Page 1: CVEV 118/698 Visual Basic Lecture 3 Prof. Mounir Mabsout Elsa Sulukdjian Walid El Asmar.

CVEV 118/698CVEV 118/698 Visual Basic Visual Basic

Lecture 3Lecture 3

Prof. Mounir MabsoutProf. Mounir MabsoutElsa SulukdjianElsa SulukdjianWalid El AsmarWalid El Asmar

Page 2: CVEV 118/698 Visual Basic Lecture 3 Prof. Mounir Mabsout Elsa Sulukdjian Walid El Asmar.

VB allows your program to support VB allows your program to support multiple forms.multiple forms.

To add a new form, go to the To add a new form, go to the Project Project MenuMenu and Select and Select Add FormAdd Form..

Each new form will be added in Each new form will be added in Project Explorer WindowProject Explorer Window..

VB Multiple Form VB Multiple Form SupportSupport

Page 3: CVEV 118/698 Visual Basic Lecture 3 Prof. Mounir Mabsout Elsa Sulukdjian Walid El Asmar.

Project PropertiesProject Properties From From ProjectProject select select ProjectName PropertiesProjectName Properties; or right ; or right

click on the top line of the Project Explorer.click on the top line of the Project Explorer. Specify Type/Name/Startup Object, I.e. which of the Specify Type/Name/Startup Object, I.e. which of the

project’s forms should appear first at startup.project’s forms should appear first at startup.

Page 4: CVEV 118/698 Visual Basic Lecture 3 Prof. Mounir Mabsout Elsa Sulukdjian Walid El Asmar.

Loading FormsLoading Forms Some forms need not to be displayed. They can Some forms need not to be displayed. They can

contain procedures needed to be done in the contain procedures needed to be done in the background without showing it to the user.background without showing it to the user.

If a form contains data that need time to be If a form contains data that need time to be loaded, you can load the form, then show it. loaded, you can load the form, then show it. The user would not feel that it took time to The user would not feel that it took time to show the form.show the form.Load FormNameLoad FormName

Unload FormNameUnload FormName Warning: when a Form is loaded, it takes up Warning: when a Form is loaded, it takes up

resources from the system, accordingly you resources from the system, accordingly you must unload all forms that are not needed.must unload all forms that are not needed.

Page 5: CVEV 118/698 Visual Basic Lecture 3 Prof. Mounir Mabsout Elsa Sulukdjian Walid El Asmar.

Showing FormsShowing Forms ShowShow method is used to display a form. method is used to display a form.

FormName.FormName.ShowShow mode mode When several forms are shown, and you want When several forms are shown, and you want

the user to proceed with some information on the user to proceed with some information on a particular one before taking any other a particular one before taking any other action, use the modality property. This option action, use the modality property. This option is specified through the mode argument of the is specified through the mode argument of the Show Show command: command: Example: Example: frmFirst.frmFirst.ShowShow 0 0

frmSecond.frmSecond.ShowShow 1 1– 0 (default): Modeless. 0 (default): Modeless. – 1: Modal, the Form takes control of the application 1: Modal, the Form takes control of the application and won’t let it proceed unless the Form is closed.and won’t let it proceed unless the Form is closed.

Page 6: CVEV 118/698 Visual Basic Lecture 3 Prof. Mounir Mabsout Elsa Sulukdjian Walid El Asmar.

Form StatesForm States Not loaded:Not loaded: The form exists on a disk file The form exists on a disk file

and doesn’t take any resources.and doesn’t take any resources. Loaded but not shown: Loaded but not shown: The form is loaded The form is loaded

into memory, and is ready to be displayed.into memory, and is ready to be displayed. Loaded and shown: Loaded and shown: The form is shown and The form is shown and

the user can interact with it. Several forms the user can interact with it. Several forms may be shown at the same time.may be shown at the same time.

Note: Note: when a form is unloaded and then when a form is unloaded and then reloaded, there is a re-initialization of reloaded, there is a re-initialization of properties and variables declared in it. I.e. properties and variables declared in it. I.e. variable values are lost. This is not the case if variable values are lost. This is not the case if a form is just made invisible, and then visible a form is just made invisible, and then visible again.again.

Page 7: CVEV 118/698 Visual Basic Lecture 3 Prof. Mounir Mabsout Elsa Sulukdjian Walid El Asmar.

Control VisibilityControl Visibility

Option Explicit

Private Sub btnShowFirst_Click() FormFirst.Visible = True FormSecond.Visible = FalseEnd Sub

Option Explicit

Private Sub btnShowSecond_Click() FormSecond.Visible = True FormFirst.Visible = FalseEnd Sub

Page 8: CVEV 118/698 Visual Basic Lecture 3 Prof. Mounir Mabsout Elsa Sulukdjian Walid El Asmar.

Controlling a Form from Controlling a Form from Within AnotherWithin Another

When a form is loaded but invisible, it can When a form is loaded but invisible, it can still be manipulated. Accessing any control still be manipulated. Accessing any control of this form (such as a TextBox) is done in of this form (such as a TextBox) is done in the following manner:the following manner:

FormName.ControlName.CommandFormName.ControlName.Command

ex: Form1.txtGreeting.Text = “Hello”ex: Form1.txtGreeting.Text = “Hello”

Page 9: CVEV 118/698 Visual Basic Lecture 3 Prof. Mounir Mabsout Elsa Sulukdjian Walid El Asmar.

Menu DesignMenu Design Menus are an important feature of Menus are an important feature of

Windows. They usually contain a large Windows. They usually contain a large number of commands and options number of commands and options specific to the application in use.specific to the application in use.

Menus can be attached only to forms.Menus can be attached only to forms. In VB, they are designed with the In VB, they are designed with the

Menu Editor from the tools menu:Menu Editor from the tools menu:Tools > Menu EditorTools > Menu Editor

Page 10: CVEV 118/698 Visual Basic Lecture 3 Prof. Mounir Mabsout Elsa Sulukdjian Walid El Asmar.

Menu EditorMenu Editor

Caption: String that appears on the application menu bar.

Name: Name to be used when coding.

Options: Controls how the commands will appear in the applications.

Contents: This window shows the commands available in this menu bar.

Shortcut: Run the command when pressed, without entering the menu. e.g. Ctrl+C.

Page 11: CVEV 118/698 Visual Basic Lecture 3 Prof. Mounir Mabsout Elsa Sulukdjian Walid El Asmar.

Options and Features of Options and Features of Menu EditorMenu Editor

Index:Index: used to make a menu item part of used to make a menu item part of a control array. This is helpful in making a control array. This is helpful in making dynamic changes in the menu at dynamic changes in the menu at runtime.runtime.

WindowList: WindowList: used when you have MDI used when you have MDI (Multiple Document Interface) windows.(Multiple Document Interface) windows.

Checked, Enabled, Visible: Checked, Enabled, Visible: same same commands as for other regular controls.commands as for other regular controls.

Page 12: CVEV 118/698 Visual Basic Lecture 3 Prof. Mounir Mabsout Elsa Sulukdjian Walid El Asmar.

Menu: ResultsMenu: Results

Option: enabled and visible.

Option: visible. Option: enabled, visible and checked.

Page 13: CVEV 118/698 Visual Basic Lecture 3 Prof. Mounir Mabsout Elsa Sulukdjian Walid El Asmar.

VB ControlsVB Controls

Page 14: CVEV 118/698 Visual Basic Lecture 3 Prof. Mounir Mabsout Elsa Sulukdjian Walid El Asmar.

ListBox & ComboBoxListBox & ComboBox ListBoxes and ComboBoxes are used when ListBoxes and ComboBoxes are used when

dealing with a series of items, whether for dealing with a series of items, whether for simple display or for selection.simple display or for selection.

ListBoxListBox ComboBoxComboBox

- List appended by - List appended by the program the program onlyonly..- Usually relatively Usually relatively large sized on large sized on screen.screen.- Data accessed Data accessed directly or with directly or with scrollbars.scrollbars.

- User can also - User can also implement the list.implement the list.- Usually small sized Usually small sized controls.controls.- Data accessed Data accessed through dropdown through dropdown list.list.

Page 15: CVEV 118/698 Visual Basic Lecture 3 Prof. Mounir Mabsout Elsa Sulukdjian Walid El Asmar.

ListBox & ComboBoxListBox & ComboBox Example Example

ListBoxComboBox

Page 16: CVEV 118/698 Visual Basic Lecture 3 Prof. Mounir Mabsout Elsa Sulukdjian Walid El Asmar.

ListBox & ComboBoxListBox & ComboBox Basic Properties Basic Properties

MultiSelect:MultiSelect: Defines if the user can Defines if the user can select multiple item at a time, and the select multiple item at a time, and the method of selection used.method of selection used.

Sorted:Sorted: The items in the list are The items in the list are automatically sorted by the program.automatically sorted by the program.

Style: Style: Controls the appearance of the Controls the appearance of the control.control.

Page 17: CVEV 118/698 Visual Basic Lecture 3 Prof. Mounir Mabsout Elsa Sulukdjian Walid El Asmar.

ListBox & ComboBoxListBox & ComboBox Basic Methods Basic Methods

Add Item:Add Item: Adds item to the list. Adds item to the list.List1.List1.AddItemAddItem item, index item, indexitemitem is the string to be added is the string to be addedindexindex is the column where the item should be is the column where the item should be

added in the list (if “sorted” is selected, this added in the list (if “sorted” is selected, this argument is neglected)argument is neglected)

ExEx: lstGreetings.AddItem “hello”, 0: lstGreetings.AddItem “hello”, 0 Remove Item:Remove Item: Removes an item from the Removes an item from the

listlistlstGreetings.lstGreetings.RemoveItemRemoveItem index indexindex = order of the item to be removedindex = order of the item to be removed

Clear:Clear: Removes all items from the control. Removes all items from the control.lstGreetings.lstGreetings.ClearClear

Page 18: CVEV 118/698 Visual Basic Lecture 3 Prof. Mounir Mabsout Elsa Sulukdjian Walid El Asmar.

ListBox & ComboBox ListBox & ComboBox ManipulationManipulation

ListCount:ListCount: Number of items inside the list. Number of items inside the list.List1.List1.ListCountListCount (Generally useful in loops) (Generally useful in loops)

List():List(): Array holding the list items Array holding the list itemsList1.List1.List(0)List(0) = “Hi” = “Hi”

ListIndex:ListIndex: Index of the selected item in the Index of the selected item in the list. list. If List1.If List1.ListIndex ListIndex > 0 Then …> 0 Then …

Selected:Selected: Array similar to List() but with Array similar to List() but with elements that have a True or False value (if elements that have a True or False value (if selected or not). selected or not).

If ( List1.If ( List1.Selected(counter)Selected(counter) ) Then… ) Then… SelCount:SelCount: Number of selected item. Number of selected item.

Page 19: CVEV 118/698 Visual Basic Lecture 3 Prof. Mounir Mabsout Elsa Sulukdjian Walid El Asmar.

Common DialogsCommon Dialogs A tedious but common task in nearly every A tedious but common task in nearly every

application is to prompt the user for application is to prompt the user for filenames, font names and sizes, colors, etc.filenames, font names and sizes, colors, etc.

All Windows applications use standard All Windows applications use standard dialog boxes for common operations such as dialog boxes for common operations such as selecting a file name. These dialogs are built selecting a file name. These dialogs are built into the Windows operating system.into the Windows operating system.

Common dialogs such as: Open, Save As, Common dialogs such as: Open, Save As, Color, Font, Print, Help are provided. All Color, Font, Print, Help are provided. All what you need to do is to set the flags what you need to do is to set the flags properties.properties.

Page 20: CVEV 118/698 Visual Basic Lecture 3 Prof. Mounir Mabsout Elsa Sulukdjian Walid El Asmar.

Common Dialogs Common Dialogs (cont’d)(cont’d)

Page 21: CVEV 118/698 Visual Basic Lecture 3 Prof. Mounir Mabsout Elsa Sulukdjian Walid El Asmar.

Common Dialogs (cont’d)Common Dialogs (cont’d) Probably you will have to add the Probably you will have to add the

common dialog boxes to your toolbox: go common dialog boxes to your toolbox: go to Project to Project Components Components Microsoft Microsoft Common Dialog Control 6.0 (SP3)Common Dialog Control 6.0 (SP3)

Create a Common Dialog Control on your Create a Common Dialog Control on your form.form.

The display of a Common Dialog box is The display of a Common Dialog box is done as follows: done as follows: CommonDialogName.Show****CommonDialogName.Show****Ex: Ex: CommonDialog1.ShowColorCommonDialog1.ShowColor

Page 22: CVEV 118/698 Visual Basic Lecture 3 Prof. Mounir Mabsout Elsa Sulukdjian Walid El Asmar.

What’s NextWhat’s Next

Thursday’s lab sessionThursday’s lab session XO game assignmentXO game assignment AutoCAD VBAAutoCAD VBA


Recommended