Download presentation
Presentation is loading. Please wait.
1
GUI Concepts - 3
2
Menus Creating Other Forms Visual Inheritance Outline
3
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
4
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.).
5
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.
6
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.
7
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...).
8
Menus
9
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 13 14 ' exit program 15 Private Sub mnuitmExit_Click( _ 16 ByVal sender As System.Object, _ 17 ByVal e As System.EventArgs) Handles mnuitmExit.Click 18 19 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
10
21 ' reset font color 22 Private Sub ClearColor() 23 24 ' clear all checkmarks 25 mnuitmBlack.Checked = False 26 mnuitmBlue.Checked = False 27 mnuitmRed.Checked = False 28 mnuitmGreen.Checked = False 29 End Sub ' ClearColor 30 31 ' 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 34 35 ' reset checkmarks for color menu items 36 ClearColor() 37 38 ' 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
11
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 45 46 ' reset checkmarks for color menu items 47 ClearColor() 48 49 ' set color to blue 50 lblDisplay.ForeColor = Color.Blue 51 mnuitmBlue.Checked = True 52 End Sub ' mnuitmBlue_Click 53 54 ' 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 57 58 ' reset checkmarks for color menu items 59 ClearColor() 60 61 ' set color to red 62 lblDisplay.ForeColor = Color.Red 63 mnuitmRed.Checked = True 64 End Sub ' mnuitmRed_Click
12
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 68 69 ' reset checkmarks for color menu items 70 ClearColor() 71 72 ' set color to green 73 lblDisplay.ForeColor = Color.Green 74 mnuitmGreen.Checked = True 75 End Sub ' mnuitmGreen_Click 76 77 ' reset font type 78 Private Sub ClearFont() 79 80 ' clear all checkmarks 81 mnuitmTimes.Checked = False 82 mnuitmCourier.Checked = False 83 mnuitmComic.Checked = False 84 End Sub ' ClearFont 85
13
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 89 90 ' reset checkmarks for font menu items 91 ClearFont() 92 93 ' 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 98 99 ' 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 102 103 ' reset checkmarks for font menu items 104 ClearFont() 105 106 ' 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
14
111 112 ' 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 115 116 ' reset check marks for font menu items 117 ClearFont() 118 119 ' 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 124 125 ' 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 129 130 ' toggle checkmark 131 mnuitmBold.Checked = Not mnuitmBold.Checked 132 133 ' 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
15
138 139 ' 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 143 144 ' toggle checkmark 145 mnuitmItalic.Checked = Not mnuitmItalic.Checked 146 147 ' 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 152 153 End Class ' FrmMenu
16
Menus Demo
17
Example – Long / Short Menu
19
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
20
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
21
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.
22
2- Working with Multiple Forms
24
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
25
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
26
Derived forms contain same functionality as Base form including: – Properties – Methods – Variables – Controls – All visual aspects Sizing Layout Spacing Colors and fonts
27
1 ' Fig. 13.39: 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 12 13 MessageBox.Show("Bugs, Bugs, Bugs is a product of " & _ 14 " Bug2Bug.com.", "Learn More", MessageBoxButtons.OK, _ 15 MessageBoxIcon.Information) 16 End Sub ' cmdLearn_Click 17 18 End Class ' FrmInheritance lblBug lblCopyright cmdLearn Example: 1- Create the Base Form
28
Example: 2- Run the Base Form to make sure it runs correctly
29
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
30
Under Application > Application Type, change Type to Class Library. Building the project produces the.dll.
31
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
32
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.
33
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
34
1 ' Fig. 13.41: VisualTest.vb 2 ' A form that uses visual inheritance. 3 4 Public Class FrmVisualTest 5 Inherits VisualForm.FrmInheritance 6 7 8 ' 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 11 12 MessageBox.Show( _ 13 "This program was created by Deitel & Associates", _ 14 "Learn the Program", MessageBoxButtons.OK, _ 15 MessageBoxIcon.Information) 16 End Sub ' cmdProgram_Click 17 18 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
36
Reading Topics For more information on how to Access Controls on Other Forms: http://www.devcity.net/Articles/100/multipleforms2.aspx For more information on how to Use Events in Multiple Forms: http://www.devcity.net/Articles/102/multipleforms3.aspx
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.