GUI Concepts - 3
Menus Creating Other Forms Visual Inheritance Outline
Menus Menu – provides groups of related commands. Menu items – Commands or options in menu Sub-menu – Menu within a menu Hot keys – Alt key shortcuts Press Alt + underlined letter in desired menu item
Menus D C All menu items can have Alt key shortcuts which are accessed by pressing Alt and the underlined letter (for example, Alt + F expands the File menu). Some menu items display checkmarks, usually indicating that multiple options on the menu can be selected at once. Menus that are not top-level menus can have shortcut keys as well (combinations of Ctrl, Shift, Alt, F1, F2, letter keys, etc.).
Menus To create a menu, open the Toolbox and drag a MainStrip control onto the form. This creates a menu bar on the top of the form and places a MainStrip icon at the bottom of the IDE. To add command names to the menu, click the Type Here textbox and type the menu command’s name.
Menus To create an access shortcut (or keyboard shortcut), type an ampersand (&) in front of the character to be underlined. For example, to create the File menu item, type &File. Separator bars are inserted by right-clicking the menu and selecting Insert Separator or by typing “-” for the menu text.
Buttons also can have access shortcuts. Place the & symbol immediately before the desired character. To click the button, the user then presses Alt and the underlined character. It is convention to place an ellipsis (…) after a menu item that display a dialog (such as Save As...).
Menus
1 ' Fig 13.5: MenuTest.vb 2 ' Using menus to change font colors and styles. 3 4 Public Class FrmMenu 5 Private Sub mnuitmAbout_Click( _ 6 ByVal sender As System.Object, _ 7 ByVal e As System.EventArgs) Handles mnuitmAbout.Click 8 9 MessageBox.Show("This is an example" & vbCrLf & _ 10 "of using menus.", "About", MessageBoxButtons.OK, _ 11 MessageBoxIcon.Information) 12 End Sub ' mnuitmAbout_Click ' exit program 15 Private Sub mnuitmExit_Click( _ 16 ByVal sender As System.Object, _ 17 ByVal e As System.EventArgs) Handles mnuitmExit.Click Application.Exit() 20 End Sub ' mnuitmExit_Click This event handler displays a message box when the about menu item in the file menu is clicked This event handler terminates the application when the exit menu item in the file menu is clicked
21 ' reset font color 22 Private Sub ClearColor() ' clear all checkmarks 25 mnuitmBlack.Checked = False 26 mnuitmBlue.Checked = False 27 mnuitmRed.Checked = False 28 mnuitmGreen.Checked = False 29 End Sub ' ClearColor ' update menu state and color display black 32 Private Sub mnuitmBlack_Click(ByVal sender As System.Object, _ 33 ByVal e As System.EventArgs) Handles mnuitmBlack.Click ' reset checkmarks for color menu items 36 ClearColor() ' set color to black 39 lblDisplay.ForeColor = Color.Black 40 mnuitmBlack.Checked = True 41 End Sub ' mnuitmBlack_Click Each color menu item must be mutually exclusive, so each event handler calls method ClearColor
42 ' update menu state and color display blue 43 Private Sub mnuitmBlue_Click(ByVal sender As System.Object, _ 44 ByVal e As System.EventArgs) Handles mnuitmBlue.Click ' reset checkmarks for color menu items 47 ClearColor() ' set color to blue 50 lblDisplay.ForeColor = Color.Blue 51 mnuitmBlue.Checked = True 52 End Sub ' mnuitmBlue_Click ' update menu state and color display red 55 Private Sub mnuitmRed_Click(ByVal sender As System.Object, _ 56 ByVal e As System.EventArgs) Handles mnuitmRed.Click ' reset checkmarks for color menu items 59 ClearColor() ' set color to red 62 lblDisplay.ForeColor = Color.Red 63 mnuitmRed.Checked = True 64 End Sub ' mnuitmRed_Click
65 ' update menu state and color display green 66 Private Sub mnuitmGreen_Click(ByVal sender As System.Object, _ 67 ByVal e As System.EventArgs) Handles mnuitmGreen.Click ' reset checkmarks for color menu items 70 ClearColor() ' set color to green 73 lblDisplay.ForeColor = Color.Green 74 mnuitmGreen.Checked = True 75 End Sub ' mnuitmGreen_Click ' reset font type 78 Private Sub ClearFont() ' clear all checkmarks 81 mnuitmTimes.Checked = False 82 mnuitmCourier.Checked = False 83 mnuitmComic.Checked = False 84 End Sub ' ClearFont 85
86 ' update menu state and set font to Times 87 Private Sub mnuitmTimes_Click(ByVal sender As System.Object, _ 88 ByVal e As System.EventArgs) Handles mnuitmTimes.Click ' reset checkmarks for font menu items 91 ClearFont() ' set Times New Roman font 94 mnuitmTimes.Checked = True 95 lblDisplay.Font = New Font("Times New Roman", 30, _ 96 lblDisplay.Font.Style) 97 End Sub ' mnuitmTimes_Click ' update menu state and set font to Courier 100 Private Sub mnuitmCourier_Click(ByVal sender As System.Object, _ 101 ByVal e As System.EventArgs) Handles mnuitmCourier.Click ' reset checkmarks for font menu items 104 ClearFont() ' set Courier font 107 mnuitmCourier.Checked = True 108 lblDisplay.Font = New Font("Courier New", 30, _ 109 lblDisplay.Font.Style) 110 End Sub ' mnuitmCourier_Click Each font menu item must be mutually exclusive, so each event handler calls method ClearFont
' update menu state and set font to Comic Sans MS 113 Private Sub mnuitmComic_Click(ByVal sender As System.Object, _ 114 ByVal e As System.EventArgs) Handles mnuitmComic.Click ' reset check marks for font menu items 117 ClearFont() ' set Comic Sans font 120 mnuitmComic.Checked = True 121 lblDisplay.Font = New Font("Comic Sans MS", 30, _ 122 lblDisplay.Font.Style) 123 End Sub ' mnuitmComic_Click ' toggle checkmark and toggle bold style 126 Private Sub mnuitmBold_Click( _ 127 ByVal sender As System.Object, _ 128 ByVal e As System.EventArgs) Handles mnuitmBold.Click ' toggle checkmark 131 mnuitmBold.Checked = Not mnuitmBold.Checked ' use Xor to toggle bold, keep all other styles 134 lblDisplay.Font = New Font( _ 135 lblDisplay.Font.FontFamily, 30, _ 136 lblDisplay.Font.Style Xor FontStyle.Bold) 137 End Sub ' mnuitmBold_Click
' toggle checkmark and toggle italic style 140 Private Sub mnuitmItalic_Click( _ 141 ByVal sender As System.Object, _ 142 ByVal e As System.EventArgs) Handles mnuitmItalic.Click ' toggle checkmark 145 mnuitmItalic.Checked = Not mnuitmItalic.Checked ' use Xor to toggle italic, keep all other styles 148 lblDisplay.Font = New Font( _ 149 lblDisplay.Font.FontFamily, 30, _ 150 lblDisplay.Font.Style Xor FontStyle.Italic) 151 End Sub ' mnuitmItalic_Click End Class ' FrmMenu
Menus Demo
Example – Long / Short Menu
Public Class Form1 Private Sub MenuSize_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuSize.Click If MenuSize.Text = "Short Menu" Then MenuSize.Text = "Long Menu" Else MenuSize.Text = "Short Menu" End If mFontUnderline.Visible = Not mFontUnderline.Visible mFontStrike.Visible = Not mFontStrike.Visible mFontSmallCaps.Visible = Not mFontSmallCaps.Visible mFontAllCaps.Visible = Not mFontAllCaps.Visible End Sub
1 -Adding a New Form to the Project You can add a new form to the project with which you are working. To do that, right click on the project name>Add Select Windows Form from the window and click Add to add a new form to the project. Creating other Forms
To run the application with other Startup object: Right-click on the project name in Solution Explorer window and select Properties which displays the Property Pages window. On this window click the drop- down box which is labeled as Startup Object. Doing that displays all the forms available in the project. Select the form which you want to be displayed when you run the application. Now, when you run the application, the form you assigned as Startup object will be displayed.
2- Working with Multiple Forms
Public Class Form1 Private Sub btnFrm2_Click(ByVal sender As _ System.Object, ByVal e As System.EventArgs) _ Handles btnFrm2.Click Form2.Show() Me.Hide() Form3.Hide() End Sub Private Sub btnFrm3_Click(ByVal sender As _ System.Object, ByVal e As System.EventArgs) _ Handles btnFrm3.Click Form3.Show() Me.Hide() Form2.Hide() End Sub End Class Public Class Form2 Private Sub btnFrm1_Click(ByVal _ sender As System.Object, ByVal e As _ System.EventArgs) Handles _ btnFrm1.Click Form1.Show() Me.Hide() End Sub End Class Public Class Form3 Private Sub btnFrm1_Click(ByVal _ sender As System.Object, ByVal e As _ System.EventArgs) Handles _ btnFrm1.Click Form1.Show() Me.Hide() End Sub End Class
In OOP, Inheritance is the ability to use all of the functionality of an existing class, and extend those capabilities without re-writing the original class. In.NET, inheritance is not just limited to designing classes but also extended to visual designing. So, what does it mean? Well, it means we can use inheritance in Form designing too, so this kind of usage is called Visual Inheritance. One of the main advantages of visual inheritance is it reduces or cuts down the development time, and helps in designing consistency in the Forms layouts. Visual Inheritance
Derived forms contain same functionality as Base form including: – Properties – Methods – Variables – Controls – All visual aspects Sizing Layout Spacing Colors and fonts
1 ' Fig : FrmInheritance.vb 2 ' Form template for use with visual inheritance. 3 4 Imports System.Windows.Forms 5 6 Public Class FrmInheritance 7 Inherits Form 8 9 ' invoked when user clicks Learn More button 10 Private Sub cmdLearn_Click(ByVal sender As System.Object, _ 11 ByVal e As System.EventArgs) Handles cmdLearn.Click MessageBox.Show("Bugs, Bugs, Bugs is a product of " & _ 14 " Bug2Bug.com.", "Learn More", MessageBoxButtons.OK, _ 15 MessageBoxIcon.Information) 16 End Sub ' cmdLearn_Click End Class ' FrmInheritance lblBug lblCopyright cmdLearn Example: 1- Create the Base Form
Example: 2- Run the Base Form to make sure it runs correctly
To allow other forms to inherit from FrmInheritance, we must package FrmInheritance as a.dll. Right click the project's name in the Solution Explorer and choose Properties. Example: 3- Packaging the Base Form
Under Application > Application Type, change Type to Class Library. Building the project produces the.dll.
To visually inherit from FrmInheritance, we create an empty project. From the Project menu, select Add Inherited Form... to display the Add New Item dialog. Select Inherited Form from the Templates pane. Clicking OK displays the Inheritance Picker tool. Example: 4- Create an empty project to make the derived form
The Inheritance Picker tool enables programmers to create a form which inherits from a specified form. Click button Browse and select the.dll file corresponding to FrmInheritance. This.dll file normally is located within the project’s bin directory. Click OK. The Form Designer should now display the inherited form We can add components to the form.
In the design view Visual Studio adds Glyphs to the image to let you know that these components are inherited, not actually in this form No Glyphs, it is not inherited
1 ' Fig : VisualTest.vb 2 ' A form that uses visual inheritance. 3 4 Public Class FrmVisualTest 5 Inherits VisualForm.FrmInheritance ' invoke when user clicks Learn the Program button 9 Private Sub cmdProgram_Click(ByVal sender As System.Object, _ 10 ByVal e As System.EventArgs) Handles cmdProgram.Click MessageBox.Show( _ 13 "This program was created by Deitel & Associates", _ 14 "Learn the Program", MessageBoxButtons.OK, _ 15 MessageBoxIcon.Information) 16 End Sub ' cmdProgram_Click End Class ' FrmVisualTest Components, layout and functionality of the base form are inherited by the new form cmdProgram new button added to form Example: 5- Add new controls to the derived form
Reading Topics For more information on how to Access Controls on Other Forms: For more information on how to Use Events in Multiple Forms: