Using menus to change font colors and styles : Menu « GUI « VB.Net Tutorial

VB.Net Tutorial
1. Language Basics
2. Data Type
3. Operator
4. Statements
5. Date Time
6. Class Module
7. Development
8. Collections
9. Generics
10. Attributes
11. Event
12. Stream File
13. GUI
14. GUI Applications
15. 2D Graphics
16. I18N Internationlization
17. Reflection
18. Regular Expressions
19. Security
20. Socket Network
21. Thread
22. Windows
23. XML
24. Database ADO.net
25. Design Patterns
Microsoft Office Word 2007 Tutorial
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial » GUI » Menu 
13. 17. 4. Using menus to change font colors and styles
Using menus to change font colors and styles
 


Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms

public class FormMenuAction
   public Shared Sub Main
        Application.Run(New FrmMenu)
   End Sub
End class


Public Class FrmMenu
   Inherits Form

#Region " Windows Form Designer generated code "

   Public Sub New()
      MyBase.New()

      'This call is required by the Windows Form Designer.
      InitializeComponent()

      'Add any initialization after the InitializeComponent() call

   End Sub

   'Form overrides dispose to clean up the component list.
   Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
      If disposing Then
         If Not (components Is NothingThen
            components.Dispose()
         End If
      End If
      MyBase.Dispose(disposing)
   End Sub

   ' display label
   Friend WithEvents lblDisplay As Label

   ' main menu (contains file and format menus)
   Friend WithEvents mnuMainMenu As MainMenu

   ' file menu 
   Friend WithEvents mnuFile As MenuItem
   Friend WithEvents mnuitmAbout As MenuItem
   Friend WithEvents mnuitmExit As MenuItem

   ' format menu (contains format and font submenus)
   Friend WithEvents mnuFormat As MenuItem

   ' color submenu
   Friend WithEvents mnuitmColor As MenuItem
   Friend WithEvents mnuitmBlack As MenuItem
   Friend WithEvents mnuitmBlue As MenuItem
   Friend WithEvents mnuitmRed As MenuItem
   Friend WithEvents mnuitmGreen As MenuItem

   ' font submenu
   Friend WithEvents mnuitmFont As MenuItem
   Friend WithEvents mnuitmTimes As MenuItem
   Friend WithEvents mnuitmCourier As MenuItem
   Friend WithEvents mnuitmComic As MenuItem
   Friend WithEvents mnuitmDash As MenuItem
   Friend WithEvents mnuitmBold As MenuItem
   Friend WithEvents mnuitmItalic As MenuItem

   'Required by the Windows Form Designer
   Private components As System.ComponentModel.Container

   'NOTE: The following procedure is required by the Windows Form Designer
   'It can be modified using the Windows Form Designer.  
   'Do not modify it using the code editor.
   <System.Diagnostics.DebuggerStepThrough()Private Sub InitializeComponent()
      Me.mnuitmItalic = New System.Windows.Forms.MenuItem()
      Me.mnuitmExit = New System.Windows.Forms.MenuItem()
      Me.mnuitmCourier = New System.Windows.Forms.MenuItem()
      Me.mnuitmBlue = New System.Windows.Forms.MenuItem()
      Me.mnuitmGreen = New System.Windows.Forms.MenuItem()
      Me.mnuMainMenu = New System.Windows.Forms.MainMenu()
      Me.mnuFile = New System.Windows.Forms.MenuItem()
      Me.mnuitmAbout = New System.Windows.Forms.MenuItem()
      Me.mnuFormat = New System.Windows.Forms.MenuItem()
      Me.mnuitmColor = New System.Windows.Forms.MenuItem()
      Me.mnuitmBlack = New System.Windows.Forms.MenuItem()
      Me.mnuitmRed = New System.Windows.Forms.MenuItem()
      Me.mnuitmFont = New System.Windows.Forms.MenuItem()
      Me.mnuitmTimes = New System.Windows.Forms.MenuItem()
      Me.mnuitmComic = New System.Windows.Forms.MenuItem()
      Me.mnuitmDash = New System.Windows.Forms.MenuItem()
      Me.mnuitmBold = New System.Windows.Forms.MenuItem()
      Me.lblDisplay = New System.Windows.Forms.Label()
      Me.SuspendLayout()
      '
      'mnuitmItalic
      '
      Me.mnuitmItalic.Index = 5
      Me.mnuitmItalic.Text = "Italic"
      '
      'mnuitmExit
      '
      Me.mnuitmExit.Index = 1
      Me.mnuitmExit.Text = "Exit"
      '
      'mnuitmCourier
      '
      Me.mnuitmCourier.Index = 1
      Me.mnuitmCourier.Text = "Courier"
      '
      'mnuitmBlue
      '
      Me.mnuitmBlue.Index = 1
      Me.mnuitmBlue.RadioCheck = True
      Me.mnuitmBlue.Text = "Blue"
      '
      'mnuitmGreen
      '
      Me.mnuitmGreen.Index = 3
      Me.mnuitmGreen.RadioCheck = True
      Me.mnuitmGreen.Text = "Green"
      '
      'mnuMainMenu
      '
      Me.mnuMainMenu.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.mnuFile, Me.mnuFormat})
      '
      'mnuFile
      '
      Me.mnuFile.Index = 0
      Me.mnuFile.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.mnuitmAbout, Me.mnuitmExit})
      Me.mnuFile.Text = "File "
      '
      'mnuitmAbout
      '
      Me.mnuitmAbout.Index = 0
      Me.mnuitmAbout.Text = "About"
      '
      'mnuFormat
      '
      Me.mnuFormat.Index = 1
      Me.mnuFormat.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.mnuitmColor, Me.mnuitmFont})
      Me.mnuFormat.Text = "Format"
      '
      'mnuitmColor
      '
      Me.mnuitmColor.Index = 0
      Me.mnuitmColor.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.mnuitmBlack, Me.mnuitmBlue, Me.mnuitmRed, Me.mnuitmGreen})
      Me.mnuitmColor.Text = "Color"
      '
      'mnuitmBlack
      '
      Me.mnuitmBlack.Index = 0
      Me.mnuitmBlack.RadioCheck = True
      Me.mnuitmBlack.Text = "Black"
      '
      'mnuitmRed
      '
      Me.mnuitmRed.Index = 2
      Me.mnuitmRed.RadioCheck = True
      Me.mnuitmRed.Text = "Red"
      '
      'mnuitmFont
      '
      Me.mnuitmFont.Index = 1
      Me.mnuitmFont.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.mnuitmTimes, Me.mnuitmCourier, Me.mnuitmComic, Me.mnuitmDash, Me.mnuitmBold, Me.mnuitmItalic})
      Me.mnuitmFont.Text = "Font"
      '
      'mnuitmTimes
      '
      Me.mnuitmTimes.Index = 0
      Me.mnuitmTimes.Text = "Times New Roman"
      '
      'mnuitmComic
      '
      Me.mnuitmComic.Index = 2
      Me.mnuitmComic.Text = "Comic Sans"
      '
      'mnuitmDash
      '
      Me.mnuitmDash.Index = 3
      Me.mnuitmDash.Text = "-"
      '
      'mnuitmBold
      '
      Me.mnuitmBold.Index = 4
      Me.mnuitmBold.Text = "Bold"
      '
      'lblDisplay
      '
      Me.lblDisplay.Font = New System.Drawing.Font("Times New Roman"26.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
      Me.lblDisplay.Location = New System.Drawing.Point(88)
      Me.lblDisplay.Name = "lblDisplay"
      Me.lblDisplay.Size = New System.Drawing.Size(24048)
      Me.lblDisplay.TabIndex = 0
      Me.lblDisplay.Text = "www.Java2s.com"
      '
      'FrmMenu
      '
      Me.AutoScaleBaseSize = New System.Drawing.Size(513)
      Me.ClientSize = New System.Drawing.Size(256121)
      Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.lblDisplay})
      Me.Menu = Me.mnuMainMenu
      Me.Name = "FrmMenu"
      Me.Text = "MenuTest"
      Me.ResumeLayout(False)

   End Sub

#End Region

   Private Sub mnuitmAbout_Click _
      (ByVal sender As System.Object, _
      ByVal e As System.EventArgsHandles mnuitmAbout.Click

      MessageBox.Show("This is an example" & vbCrLf & _
         "of using menus.""About", MessageBoxButtons.OK, _
         MessageBoxIcon.Information)
   End Sub
   Private Sub mnuitmExit_Click _
      (ByVal sender As System.Object, _
      ByVal e As System.EventArgsHandles mnuitmExit.Click

      Application.Exit()
   End Sub
   
   Private Sub ClearColor()
      mnuitmBlack.Checked = False
      mnuitmBlue.Checked = False
      mnuitmRed.Checked = False
      mnuitmGreen.Checked = False
   End Sub 
   Private Sub mnuitmBlack_Click(ByVal sender As System.Object, _
      ByVal e As System.EventArgsHandles mnuitmBlack.Click
      ClearColor()

      lblDisplay.ForeColor = Color.Black
      mnuitmBlack.Checked = True
   End Sub
   
   Private Sub mnuitmBlue_Click(ByVal sender As System.Object, _
      ByVal e As System.EventArgsHandles mnuitmBlue.Click
      ClearColor()
      lblDisplay.ForeColor = Color.Blue
      mnuitmBlue.Checked = True
   End Sub 
   
   Private Sub mnuitmRed_Click(ByVal sender As System.Object, _
      ByVal e As System.EventArgsHandles mnuitmRed.Click

      ClearColor()

      lblDisplay.ForeColor = Color.Red
      mnuitmRed.Checked = True
   End Sub
   
   Private Sub mnuitmGreen_Click(ByVal sender As System.Object, _
      ByVal e As System.EventArgsHandles mnuitmGreen.Click

      ClearColor()

      lblDisplay.ForeColor = Color.Green
      mnuitmGreen.Checked = True
   End Sub

   Private Sub ClearFont()
      mnuitmTimes.Checked = False
      mnuitmCourier.Checked = False
      mnuitmComic.Checked = False
   End Sub 

   Private Sub mnuitmTimes_Click(ByVal sender As System.Object, _
      ByVal e As System.EventArgsHandles mnuitmTimes.Click

      ClearFont()

      mnuitmTimes.Checked = True
      lblDisplay.Font = New Font("Times New Roman"30, _
         lblDisplay.Font.Style)
   End Sub 
   
   Private Sub mnuitmCourier_Click(ByVal sender As System.Object, _
      ByVal e As System.EventArgsHandles mnuitmCourier.Click

      ClearFont()

      mnuitmCourier.Checked = True
      lblDisplay.Font = New Font("Courier New"30, _
         lblDisplay.Font.Style)
   End Sub 
   Private Sub mnuitmComic_Click(ByVal sender As System.Object, _
      ByVal e As System.EventArgsHandles mnuitmComic.Click

      ClearFont()

      mnuitmComic.Checked = True
      lblDisplay.Font = New Font("Comic Sans MS"30, _
         lblDisplay.Font.Style)
   End Sub 

   Private Sub mnuitmBold_Click _
      (ByVal sender As System.Object, _
      ByVal e As System.EventArgsHandles mnuitmBold.Click

      mnuitmBold.Checked = Not mnuitmBold.Checked

      lblDisplay.Font = New Font("Times New Roman"30, lblDisplay.Font.Style Xor FontStyle.Bold)
   End Sub 

   Private Sub mnuitmItalic_Click _
      (ByVal sender As System.Object, _
      ByVal e As System.EventArgsHandles mnuitmItalic.Click

      mnuitmItalic.Checked = Not mnuitmItalic.Checked

      lblDisplay.Font = New Font("Times New Roman"30, lblDisplay.Font.Style Xor FontStyle.Italic)
   End Sub 

End Class 

        
13. 17. Menu
13. 17. 1. Create Menu in your codeCreate Menu in your code
13. 17. 2. Add Menu to Form window and attach actionsAdd Menu to Form window and attach actions
13. 17. 3. Build menu at runtimeBuild menu at runtime
13. 17. 4. Using menus to change font colors and stylesUsing menus to change font colors and styles
13. 17. 5. Owner draw menuOwner draw menu
ww_w_.__jav__a__2s__.___c_o__m_ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.