220 likes | 364 Views
CVEV 118/698 Visual Basic. Lecture 3. Prof. Mounir Mabsout Elsa Sulukdjian Walid El Asmar. VB Multiple Form Support. VB allows your program to support multiple forms. To add a new form, go to the Project Menu and Select Add Form .
E N D
CVEV 118/698 Visual Basic Lecture 3 Prof. Mounir Mabsout Elsa Sulukdjian Walid El Asmar
VB Multiple Form Support • VB allows your program to support multiple forms. • To add a new form, go to the Project Menu and Select Add Form. • Each new form will be added in Project Explorer Window.
Project Properties • From Project select ProjectName Properties; or right click on the top line of the Project Explorer. • Specify Type/Name/Startup Object, I.e. which of the project’s forms should appear first at startup.
Loading Forms • Some forms need not to be displayed. They can contain procedures needed to be done in the background without showing it to the user. • If a form contains data that need time to be loaded, you can load the form, then show it. The user would not feel that it took time to show the form. Load FormName Unload FormName • Warning: when a Form is loaded, it takes up resources from the system, accordingly you must unload all forms that are not needed.
Showing Forms • Show method is used to display a form. FormName.Show mode • When several forms are shown, and you want the user to proceed with some information on a particular one before taking any other action, use the modality property. This option is specified through the mode argument of the Show command: Example: frmFirst.Show 0 frmSecond.Show 1 • 0 (default): Modeless. • 1: Modal, the Form takes control of the application and won’t let it proceed unless the Form is closed.
Form States • Not loaded: The form exists on a disk file and doesn’t take any resources. • Loaded but not shown: The form is loaded into memory, and is ready to be displayed. • Loaded and shown: The form is shown and the user can interact with it. Several forms may be shown at the same time. • Note: when a form is unloaded and then reloaded, there is a re-initialization of properties and variables declared in it. I.e. variable values are lost. This is not the case if a form is just made invisible, and then visible again.
Control Visibility Option Explicit Private Sub btnShowFirst_Click() FormFirst.Visible = True FormSecond.Visible = False End Sub Option Explicit Private Sub btnShowSecond_Click() FormSecond.Visible = True FormFirst.Visible = False End Sub
Controlling a Form from Within Another • When a form is loaded but invisible, it can still be manipulated. Accessing any control of this form (such as a TextBox) is done in the following manner: FormName.ControlName.Command ex: Form1.txtGreeting.Text = “Hello”
Menu Design • Menus are an important feature of Windows. They usually contain a large number of commands and options specific to the application in use. • Menus can be attached only to forms. • In VB, they are designed with the Menu Editor from the tools menu: Tools > Menu Editor
Menu Editor Shortcut: Run the command when pressed, without entering the menu. e.g. Ctrl+C. Caption: String that appears on the application menu bar. Options: Controls how the commands will appear in the applications. Name: Name to be used when coding. Contents: This window shows the commands available in this menu bar.
Options and Features of Menu Editor • Index: used to make a menu item part of a control array. This is helpful in making dynamic changes in the menu at runtime. • WindowList: used when you have MDI (Multiple Document Interface) windows. • Checked, Enabled, Visible: same commands as for other regular controls.
Menu: Results Option: enabled and visible. Option: visible. Option: enabled, visible and checked.
ListBox & ComboBox • ListBoxes and ComboBoxes are used when dealing with a series of items, whether for simple display or for selection.
ListBox & ComboBox Example ComboBox ListBox
ListBox & ComboBox Basic Properties • MultiSelect: Defines if the user can select multiple item at a time, and the method of selection used. • Sorted: The items in the list are automatically sorted by the program. • Style: Controls the appearance of the control.
ListBox & ComboBox Basic Methods • Add Item: Adds item to the list. List1.AddItem item, index item is the string to be added index is the column where the item should be added in the list (if “sorted” is selected, this argument is neglected) Ex: lstGreetings.AddItem “hello”, 0 • Remove Item: Removes an item from the list lstGreetings.RemoveItem index index = order of the item to be removed • Clear: Removes all items from the control. lstGreetings.Clear
ListBox & ComboBox Manipulation • ListCount: Number of items inside the list. List1.ListCount (Generally useful in loops) • List(): Array holding the list items List1.List(0) = “Hi” • ListIndex: Index of the selected item in the list. If List1.ListIndex > 0 Then … • Selected: Array similar to List() but with elements that have a True or False value (if selected or not). If ( List1.Selected(counter) ) Then… • SelCount: Number of selected item.
Common Dialogs • A tedious but common task in nearly every application is to prompt the user for filenames, font names and sizes, colors, etc. • All Windows applications use standard dialog boxes for common operations such as selecting a file name. These dialogs are built into the Windows operating system. • Common dialogs such as: Open, Save As, Color, Font, Print, Help are provided. All what you need to do is to set the flags properties.
Common Dialogs (cont’d) • Probably you will have to add the common dialog boxes to your toolbox: go to Project Components Microsoft Common Dialog Control 6.0 (SP3) • Create a Common Dialog Control on your form. • The display of a Common Dialog box is done as follows: CommonDialogName.Show**** Ex: CommonDialog1.ShowColor
What’s Next • Thursday’s lab session • XO game assignment • AutoCAD VBA