VB.Net by API/System.Windows.Forms/MenuItem
Содержание
MenuItem.Checked
<source lang="vbnet">
Imports System.Windows.Forms public class DynamicMenu
public Shared Sub Main Application.Run(New Form1) End Sub
End class Public Class Form1
Inherits System.Windows.Forms.Form Public Sub New() MyBase.New() InitializeComponent() End Sub Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub Private components As System.ruponentModel.IContainer Friend WithEvents MainMenu1 As System.Windows.Forms.MainMenu Friend WithEvents MenuItem1 As System.Windows.Forms.MenuItem Friend WithEvents MenuItem2 As System.Windows.Forms.MenuItem Friend WithEvents MenuItem3 As System.Windows.Forms.MenuItem Friend WithEvents MenuItem4 As System.Windows.Forms.MenuItem Friend WithEvents MenuItem5 As System.Windows.Forms.MenuItem Friend WithEvents MenuItem6 As System.Windows.Forms.MenuItem Friend WithEvents Button1 As System.Windows.Forms.Button Friend WithEvents Button2 As System.Windows.Forms.Button Friend WithEvents Button3 As System.Windows.Forms.Button Friend WithEvents Button4 As System.Windows.Forms.Button <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() Me.MainMenu1 = New System.Windows.Forms.MainMenu Me.MenuItem1 = New System.Windows.Forms.MenuItem Me.MenuItem2 = New System.Windows.Forms.MenuItem Me.MenuItem3 = New System.Windows.Forms.MenuItem Me.MenuItem4 = New System.Windows.Forms.MenuItem Me.MenuItem5 = New System.Windows.Forms.MenuItem Me.MenuItem6 = New System.Windows.Forms.MenuItem Me.Button1 = New System.Windows.Forms.Button Me.Button2 = New System.Windows.Forms.Button Me.Button3 = New System.Windows.Forms.Button Me.Button4 = New System.Windows.Forms.Button Me.SuspendLayout() " "MainMenu1 " Me.MainMenu1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MenuItem1, Me.MenuItem3}) " "MenuItem1 " Me.MenuItem1.Index = 0 Me.MenuItem1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MenuItem2}) Me.MenuItem1.Text = "File" " "MenuItem2 " Me.MenuItem2.Index = 0 Me.MenuItem2.Text = "Open" " "MenuItem3 " Me.MenuItem3.Index = 1 Me.MenuItem3.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MenuItem4, Me.MenuItem5, Me.MenuItem6}) Me.MenuItem3.Text = "Edit" " "MenuItem4 " Me.MenuItem4.Index = 0 Me.MenuItem4.Text = "Cut" " "MenuItem5 " Me.MenuItem5.Index = 1 Me.MenuItem5.Text = "Copy" " "MenuItem6 " Me.MenuItem6.Index = 2 Me.MenuItem6.Text = "Paste" " "Button1 " Me.Button1.Location = New System.Drawing.Point(200, 8) Me.Button1.Name = "Button1" Me.Button1.Size = New System.Drawing.Size(72, 24) Me.Button1.TabIndex = 3 Me.Button1.Text = "Select" " "Button2 " Me.Button2.Location = New System.Drawing.Point(200, 48) Me.Button2.Name = "Button2" Me.Button2.Size = New System.Drawing.Size(72, 24) Me.Button2.TabIndex = 4 Me.Button2.Text = "Disable" " "Button3 " Me.Button3.Location = New System.Drawing.Point(200, 88) Me.Button3.Name = "Button3" Me.Button3.Size = New System.Drawing.Size(72, 24) Me.Button3.TabIndex = 5 Me.Button3.Text = "RadioButton" " "Button4 " Me.Button4.Location = New System.Drawing.Point(200, 136) Me.Button4.Name = "Button4" Me.Button4.Size = New System.Drawing.Size(72, 24) Me.Button4.TabIndex = 6 Me.Button4.Text = "Hide" " "Form1 " Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(328, 225) Me.Controls.Add(Me.Button4) Me.Controls.Add(Me.Button3) Me.Controls.Add(Me.Button2) Me.Controls.Add(Me.Button1) Me.Menu = Me.MainMenu1 Me.ResumeLayout(False) End Sub Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click MenuItem4.RadioCheck = False MenuItem5.RadioCheck = False MenuItem6.RadioCheck = False MenuItem6.Checked = MenuItem4.Checked MenuItem4.Checked = Not MenuItem4.Checked MenuItem5.Checked = Not MenuItem5.Checked MenuItem6.Checked = Not MenuItem6.Checked End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click MenuItem4.RadioCheck = False MenuItem5.RadioCheck = False MenuItem6.RadioCheck = False MenuItem6.Checked = MenuItem4.Checked MenuItem4.Enabled = Not MenuItem4.Enabled MenuItem5.Enabled = Not MenuItem5.Enabled MenuItem6.Enabled = Not MenuItem6.Enabled End Sub Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click MenuItem4.Checked = False MenuItem5.Checked = False MenuItem6.Checked = True MenuItem6.RadioCheck = True End Sub Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click MenuItem6.Visible = Not MenuItem6.Visible End Sub
End Class
</source>
Menuitem.Click
<source lang="vbnet"> Imports System Imports System.Collections Imports System.Data Imports System.IO Imports System.Xml.Serialization Imports System.Windows.Forms Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Drawing.Text Imports System.Drawing.Printing
Public Class MainClass
Shared Sub Main() Dim form1 As Form = New Form1() Application.Run(form1) End Sub
End Class
Public Class Form1
Inherits System.Windows.Forms.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 Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub Friend WithEvents MainMenu1 As System.Windows.Forms.MainMenu Friend WithEvents mnuExit As System.Windows.Forms.MenuItem Friend WithEvents mnuFile As System.Windows.Forms.MenuItem Friend WithEvents mnuNew As System.Windows.Forms.MenuItem "Required by the Windows Form Designer Private components As System.ruponentModel.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.MainMenu1 = New System.Windows.Forms.MainMenu() Me.mnuFile = New System.Windows.Forms.MenuItem() Me.mnuNew = New System.Windows.Forms.MenuItem() Me.mnuExit = New System.Windows.Forms.MenuItem() " "MainMenu1 " Me.MainMenu1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.mnuFile}) " "mnuFile " Me.mnuFile.Index = 0 Me.mnuFile.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.mnuNew, Me.mnuExit}) Me.mnuFile.Text = "File" " "mnuNew " Me.mnuNew.Index = 0 Me.mnuNew.Text = "New" " "mnuExit " Me.mnuExit.Index = 1 Me.mnuExit.Text = "Exit" " "Form1 " Me.AutoScaleBaseSize = New System.Drawing.Size(6, 15) Me.ClientSize = New System.Drawing.Size(292, 272) Me.Menu = Me.MainMenu1 Me.Name = "Form1" Me.Text = "MDI Example 2" End Sub
- End Region
Private Sub Form1_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles MyBase.Load Me.Text = "I"m an MDI Parent" Me.IsMdiContainer = True Dim MyChild As New System.Windows.Forms.Form() MyChild.MdiParent = Me MyChild.Show() MyChild.Text = "First MDI Child" InitializeMenu() End Sub Public Sub InitializeMenu() Dim mnuWindow As New MenuItem("&Window") MainMenu1.MenuItems.Add(mnuWindow) mnuWindow.MenuItems.Add(New MenuItem("&Cascade", AddressOf WindowCascade_Clicked)) mnuWindow.MenuItems.Add(New MenuItem("Tile &Horizontal", AddressOf WindowTileHoriz_Clicked)) mnuWindow.MenuItems.Add(New MenuItem("Tile &Vertical", AddressOf WindowTileVert_Clicked)) mnuWindow.MdiList = True End Sub Protected Sub WindowCascade_Clicked(ByVal Sender As Object, _ ByVal e As System.EventArgs) Me.LayoutMdi(MdiLayout.Cascade) End Sub Protected Sub WindowTileHoriz_Clicked(ByVal Sender As Object, _ ByVal e As System.EventArgs) Me.LayoutMdi(MdiLayout.TileHorizontal) End Sub Protected Sub WindowTileVert_Clicked(ByVal Sender As Object, _ ByVal e As System.EventArgs) Me.LayoutMdi(MdiLayout.TileVertical) End Sub Private Sub mnuExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuExit.Click System.Environment.Exit(0) End Sub Private Sub mnuNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuNew.Click Static numOfChildren As Integer = 2 Dim MyChild As New System.Windows.Forms.Form() MyChild.MdiParent = Me MyChild.Show() MyChild.Text = "MDI Child " & CStr(numOfChildren) numOfChildren += 1 End Sub
End Class
</source>
MenuItem.DrawItem
<source lang="vbnet"> Imports System Imports System.Drawing Imports System.Reflection Imports System.Windows.Forms Imports System.IO
public class MainClass
Shared Sub Main() Dim form1 As Form = New Form1 Application.Run(form1) End Sub
End Class
Public Class Form1
Inherits System.Windows.Forms.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 Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub Friend WithEvents MainMenu1 As System.Windows.Forms.MainMenu Friend WithEvents MenuItem1 As System.Windows.Forms.MenuItem "Required by the Windows Form Designer Private components As System.ruponentModel.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. Friend WithEvents Red As System.Windows.Forms.MenuItem Friend WithEvents Green As System.Windows.Forms.MenuItem Friend WithEvents Blue As System.Windows.Forms.MenuItem Friend WithEvents Yellow As System.Windows.Forms.MenuItem Friend WithEvents Black As System.Windows.Forms.MenuItem Friend WithEvents White As System.Windows.Forms.MenuItem Friend WithEvents MenuItem2 As System.Windows.Forms.MenuItem Friend WithEvents AlignLeft As System.Windows.Forms.MenuItem Friend WithEvents AlignCenter As System.Windows.Forms.MenuItem Friend WithEvents AlignRight As System.Windows.Forms.MenuItem <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() Me.MainMenu1 = New System.Windows.Forms.MainMenu() Me.MenuItem1 = New System.Windows.Forms.MenuItem() Me.Red = New System.Windows.Forms.MenuItem() Me.Green = New System.Windows.Forms.MenuItem() Me.Blue = New System.Windows.Forms.MenuItem() Me.Yellow = New System.Windows.Forms.MenuItem() Me.Black = New System.Windows.Forms.MenuItem() Me.White = New System.Windows.Forms.MenuItem() Me.MenuItem2 = New System.Windows.Forms.MenuItem() Me.AlignLeft = New System.Windows.Forms.MenuItem() Me.AlignCenter = New System.Windows.Forms.MenuItem() Me.AlignRight = New System.Windows.Forms.MenuItem() " "MainMenu1 " Me.MainMenu1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MenuItem1, Me.MenuItem2}) " "MenuItem1 " Me.MenuItem1.Index = 0 Me.MenuItem1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.Red, Me.Green, Me.Blue, Me.Yellow, Me.Black, Me.White}) Me.MenuItem1.Text = "Color" " "Red " Me.Red.Index = 0 Me.Red.OwnerDraw = True Me.Red.Text = "" " "Green " Me.Green.Index = 1 Me.Green.OwnerDraw = True Me.Green.Text = "" " "Blue " Me.Blue.Index = 2 Me.Blue.OwnerDraw = True Me.Blue.Text = "" " "Yellow " Me.Yellow.Index = 3 Me.Yellow.OwnerDraw = True Me.Yellow.Text = "" " "Black " Me.Black.Index = 4 Me.Black.OwnerDraw = True Me.Black.Text = "" " "White " Me.White.Index = 5 Me.White.OwnerDraw = True Me.White.Text = "" " "MenuItem2 " Me.MenuItem2.Index = 1 Me.MenuItem2.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.AlignLeft, Me.AlignCenter, Me.AlignRight}) Me.MenuItem2.Text = "Alignment" " "AlignLeft " Me.AlignLeft.Index = 0 Me.AlignLeft.OwnerDraw = True Me.AlignLeft.Text = "Left" " "AlignCenter " Me.AlignCenter.Index = 1 Me.AlignCenter.OwnerDraw = True Me.AlignCenter.Text = "Center" " "AlignRight " Me.AlignRight.Index = 2 Me.AlignRight.OwnerDraw = True Me.AlignRight.Text = "Right" " "Form1 " Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(292, 129) Me.Menu = Me.MainMenu1 Me.Name = "Form1" Me.Text = "Owner-Drawn Menu" End Sub
- End Region
Dim currentFont As Font Private Sub Red_DrawItem(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles Red.DrawItem Dim R As New RectangleF(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height) e.Graphics.FillRectangle(Brushes.Red, R) End Sub Private Sub Red_MeasureItem(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MeasureItemEventArgs) Handles Red.MeasureItem Dim itemSize As SizeF itemSize = New SizeF(80, 18) e.ItemHeight = itemSize.Height e.ItemWidth = itemSize.Width End Sub Private Sub Green_MeasureItem(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MeasureItemEventArgs) Handles Red.MeasureItem Dim itemSize As SizeF itemSize = New SizeF(80, 18) e.ItemHeight = itemSize.Height e.ItemWidth = itemSize.Width End Sub Private Sub Green_DrawItem(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles Green.DrawItem Dim R As New RectangleF(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height) e.Graphics.FillRectangle(Brushes.Green, R) End Sub Private Sub Blue_MeasureItem(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MeasureItemEventArgs) Handles Blue.MeasureItem Dim itemSize As SizeF itemSize = New SizeF(80, 18) e.ItemHeight = itemSize.Height e.ItemWidth = itemSize.Width End Sub Private Sub Blue_DrawItem(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles Blue.DrawItem Dim R As New RectangleF(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height) e.Graphics.FillRectangle(Brushes.Blue, R) End Sub Private Sub Yellow_MeasureItem(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MeasureItemEventArgs) Handles Yellow.MeasureItem Dim itemSize As SizeF itemSize = New SizeF(80, 18) e.ItemHeight = itemSize.Height e.ItemWidth = itemSize.Width End Sub Private Sub Yellow_DrawItem(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles Yellow.DrawItem Dim R As New RectangleF(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height) e.Graphics.FillRectangle(Brushes.Yellow, R) End Sub Private Sub White_MeasureItem(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MeasureItemEventArgs) Handles White.MeasureItem Dim itemSize As SizeF itemSize = New SizeF(80, 18) e.ItemHeight = itemSize.Height e.ItemWidth = itemSize.Width End Sub Private Sub White_DrawItem(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles White.DrawItem Dim R As New RectangleF(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height) e.Graphics.FillRectangle(Brushes.White, R) End Sub Private Sub Black_MeasureItem(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MeasureItemEventArgs) Handles Black.MeasureItem Dim itemSize As SizeF itemSize = New SizeF(80, 18) e.ItemHeight = itemSize.Height e.ItemWidth = itemSize.Width End Sub Private Sub Black_DrawItem(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles Black.DrawItem Dim R As New RectangleF(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height) e.Graphics.FillRectangle(Brushes.Black, R) End Sub Private Sub AlignLeft_MeasureItem(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MeasureItemEventArgs) Handles AlignLeft.MeasureItem Dim itemSize As SizeF itemSize = New SizeF(40, 14) e.ItemHeight = itemSize.Height e.ItemWidth = itemSize.Width End Sub Private Sub AlignLeft_DrawItem(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles AlignLeft.DrawItem Dim R As New RectangleF(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height) Dim strfmt As New StringFormat() strfmt.Alignment = StringAlignment.Near e.Graphics.DrawString("Left", Me.Font, Brushes.Black, R, strfmt) End Sub Private Sub AlignRight_MeasureItem(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MeasureItemEventArgs) Handles AlignRight.MeasureItem Dim itemSize As SizeF itemSize = New SizeF(40, 14) e.ItemHeight = itemSize.Height e.ItemWidth = itemSize.Width End Sub Private Sub AlignRight_DrawItem(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles AlignRight.DrawItem Dim R As New RectangleF(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height) Dim strfmt As New StringFormat() strfmt.Alignment = StringAlignment.Far e.Graphics.DrawString("Right", Me.Font, Brushes.Black, R, strfmt) End Sub Private Sub AlignCenter_MeasureItem(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MeasureItemEventArgs) Handles AlignCenter.MeasureItem Dim itemSize As SizeF itemSize = New SizeF(40, 14) e.ItemHeight = itemSize.Height e.ItemWidth = itemSize.Width End Sub Private Sub AlignCenter_DrawItem(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles AlignCenter.DrawItem Dim R As New RectangleF(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height) Dim strfmt As New StringFormat() strfmt.Alignment = StringAlignment.Center e.Graphics.DrawString("Center", Me.Font, Brushes.Black, R, strfmt) End Sub
Private Sub Alignment_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AlignCenter.Click, AlignLeft.Click, AlignRight.Click,Red.Click,Green.Click,Blue.Click,Yellow.Click,Black.Click,White.Click MessageBox.Show(sender.Text) End Sub
End Class
</source>
MenuItem.Enabled
<source lang="vbnet">
Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Windows.Forms public class OwnerDrawMenuAndSubmenu
public Shared Sub Main Application.Run(New Form1) End Sub
End class Public Class Form1
Inherits System.Windows.Forms.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 Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub "Required by the Windows Form Designer Private components As System.ruponentModel.IContainer "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. Friend WithEvents MainMenu1 As System.Windows.Forms.MainMenu Friend WithEvents MenuItem1 As System.Windows.Forms.MenuItem Friend WithEvents MenuItem2 As System.Windows.Forms.MenuItem Friend WithEvents MenuItem3 As System.Windows.Forms.MenuItem Friend WithEvents MenuItem4 As System.Windows.Forms.MenuItem Friend WithEvents MenuItem5 As System.Windows.Forms.MenuItem Friend WithEvents MenuItem6 As System.Windows.Forms.MenuItem Friend WithEvents MenuItem7 As System.Windows.Forms.MenuItem Friend WithEvents MenuItem8 As System.Windows.Forms.MenuItem Friend WithEvents MenuItem9 As System.Windows.Forms.MenuItem <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() Me.MainMenu1 = New System.Windows.Forms.MainMenu Me.MenuItem1 = New System.Windows.Forms.MenuItem Me.MenuItem2 = New System.Windows.Forms.MenuItem Me.MenuItem7 = New System.Windows.Forms.MenuItem Me.MenuItem8 = New System.Windows.Forms.MenuItem Me.MenuItem9 = New System.Windows.Forms.MenuItem Me.MenuItem3 = New System.Windows.Forms.MenuItem Me.MenuItem4 = New System.Windows.Forms.MenuItem Me.MenuItem5 = New System.Windows.Forms.MenuItem Me.MenuItem6 = New System.Windows.Forms.MenuItem Me.SuspendLayout() " "MainMenu1 " Me.MainMenu1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MenuItem1}) " "MenuItem1 " Me.MenuItem1.Index = 0 Me.MenuItem1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MenuItem2, Me.MenuItem3, Me.MenuItem4, Me.MenuItem5, Me.MenuItem6}) Me.MenuItem1.Text = "&File" " "MenuItem2 " Me.MenuItem2.Index = 0 Me.MenuItem2.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MenuItem7, Me.MenuItem8, Me.MenuItem9}) Me.MenuItem2.Text = "Item 1" " "MenuItem7 " Me.MenuItem7.Index = 0 Me.MenuItem7.OwnerDraw = True Me.MenuItem7.Text = "Item 5" " "MenuItem8 " Me.MenuItem8.Checked = True Me.MenuItem8.Index = 1 Me.MenuItem8.Text = "Item 6" " "MenuItem9 " Me.MenuItem9.Index = 2 Me.MenuItem9.Text = "Item 7" " "MenuItem3 " Me.MenuItem3.Index = 1 Me.MenuItem3.Text = "Item 2" " "MenuItem4 " Me.MenuItem4.Index = 2 Me.MenuItem4.Text = "Item 3" " "MenuItem5 " Me.MenuItem5.Index = 3 Me.MenuItem5.Text = "-" " "MenuItem6 " Me.MenuItem6.Index = 4 Me.MenuItem6.Shortcut = System.Windows.Forms.Shortcut.CtrlX Me.MenuItem6.Text = "E&xit" " "Form1 " Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(292, 273) Me.Menu = Me.MainMenu1 Me.Name = "Form1" Me.Text = "Form1" Me.ResumeLayout(False) End Sub
- End Region
Private Sub MenuItem6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem6.Click End End Sub Private Sub MenuItem9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem9.Click MsgBox("You clicked Item 7.") End Sub Private Sub MenuItem8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem8.Click MenuItem8.Checked = Not MenuItem8.Checked End Sub Private Sub MenuItem4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem4.Click MenuItem4.Visible = False End Sub Private Sub MenuItem3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem3.Click MenuItem3.Enabled = False End Sub Private Sub MenuItem7_MeasureItem(ByVal sender As Object, ByVal e As System.Windows.Forms.MeasureItemEventArgs) Handles MenuItem7.MeasureItem e.ItemHeight = 15 e.ItemWidth = 60 End Sub Private Sub MenuItem7_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles MenuItem7.DrawItem Dim penRed As New Pen(Color.Red) e.Graphics.DrawEllipse(penRed, e.Bounds) End Sub
End Class
</source>
MenuItem.MeasureItem
<source lang="vbnet"> Imports System Imports System.Drawing Imports System.Reflection Imports System.Windows.Forms Imports System.IO
public class MainClass
Shared Sub Main() Dim form1 As Form = New Form1 Application.Run(form1) End Sub
End Class
Public Class Form1
Inherits System.Windows.Forms.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 Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub Friend WithEvents MainMenu1 As System.Windows.Forms.MainMenu Friend WithEvents MenuItem1 As System.Windows.Forms.MenuItem "Required by the Windows Form Designer Private components As System.ruponentModel.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. Friend WithEvents Red As System.Windows.Forms.MenuItem Friend WithEvents Green As System.Windows.Forms.MenuItem Friend WithEvents Blue As System.Windows.Forms.MenuItem Friend WithEvents Yellow As System.Windows.Forms.MenuItem Friend WithEvents Black As System.Windows.Forms.MenuItem Friend WithEvents White As System.Windows.Forms.MenuItem Friend WithEvents MenuItem2 As System.Windows.Forms.MenuItem Friend WithEvents AlignLeft As System.Windows.Forms.MenuItem Friend WithEvents AlignCenter As System.Windows.Forms.MenuItem Friend WithEvents AlignRight As System.Windows.Forms.MenuItem <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() Me.MainMenu1 = New System.Windows.Forms.MainMenu() Me.MenuItem1 = New System.Windows.Forms.MenuItem() Me.Red = New System.Windows.Forms.MenuItem() Me.Green = New System.Windows.Forms.MenuItem() Me.Blue = New System.Windows.Forms.MenuItem() Me.Yellow = New System.Windows.Forms.MenuItem() Me.Black = New System.Windows.Forms.MenuItem() Me.White = New System.Windows.Forms.MenuItem() Me.MenuItem2 = New System.Windows.Forms.MenuItem() Me.AlignLeft = New System.Windows.Forms.MenuItem() Me.AlignCenter = New System.Windows.Forms.MenuItem() Me.AlignRight = New System.Windows.Forms.MenuItem() " "MainMenu1 " Me.MainMenu1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MenuItem1, Me.MenuItem2}) " "MenuItem1 " Me.MenuItem1.Index = 0 Me.MenuItem1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.Red, Me.Green, Me.Blue, Me.Yellow, Me.Black, Me.White}) Me.MenuItem1.Text = "Color" " "Red " Me.Red.Index = 0 Me.Red.OwnerDraw = True Me.Red.Text = "" " "Green " Me.Green.Index = 1 Me.Green.OwnerDraw = True Me.Green.Text = "" " "Blue " Me.Blue.Index = 2 Me.Blue.OwnerDraw = True Me.Blue.Text = "" " "Yellow " Me.Yellow.Index = 3 Me.Yellow.OwnerDraw = True Me.Yellow.Text = "" " "Black " Me.Black.Index = 4 Me.Black.OwnerDraw = True Me.Black.Text = "" " "White " Me.White.Index = 5 Me.White.OwnerDraw = True Me.White.Text = "" " "MenuItem2 " Me.MenuItem2.Index = 1 Me.MenuItem2.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.AlignLeft, Me.AlignCenter, Me.AlignRight}) Me.MenuItem2.Text = "Alignment" " "AlignLeft " Me.AlignLeft.Index = 0 Me.AlignLeft.OwnerDraw = True Me.AlignLeft.Text = "Left" " "AlignCenter " Me.AlignCenter.Index = 1 Me.AlignCenter.OwnerDraw = True Me.AlignCenter.Text = "Center" " "AlignRight " Me.AlignRight.Index = 2 Me.AlignRight.OwnerDraw = True Me.AlignRight.Text = "Right" " "Form1 " Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(292, 129) Me.Menu = Me.MainMenu1 Me.Name = "Form1" Me.Text = "Owner-Drawn Menu" End Sub
- End Region
Dim currentFont As Font Private Sub Red_DrawItem(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles Red.DrawItem Dim R As New RectangleF(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height) e.Graphics.FillRectangle(Brushes.Red, R) End Sub Private Sub Red_MeasureItem(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MeasureItemEventArgs) Handles Red.MeasureItem Dim itemSize As SizeF itemSize = New SizeF(80, 18) e.ItemHeight = itemSize.Height e.ItemWidth = itemSize.Width End Sub Private Sub Green_MeasureItem(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MeasureItemEventArgs) Handles Red.MeasureItem Dim itemSize As SizeF itemSize = New SizeF(80, 18) e.ItemHeight = itemSize.Height e.ItemWidth = itemSize.Width End Sub Private Sub Green_DrawItem(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles Green.DrawItem Dim R As New RectangleF(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height) e.Graphics.FillRectangle(Brushes.Green, R) End Sub Private Sub Blue_MeasureItem(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MeasureItemEventArgs) Handles Blue.MeasureItem Dim itemSize As SizeF itemSize = New SizeF(80, 18) e.ItemHeight = itemSize.Height e.ItemWidth = itemSize.Width End Sub Private Sub Blue_DrawItem(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles Blue.DrawItem Dim R As New RectangleF(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height) e.Graphics.FillRectangle(Brushes.Blue, R) End Sub Private Sub Yellow_MeasureItem(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MeasureItemEventArgs) Handles Yellow.MeasureItem Dim itemSize As SizeF itemSize = New SizeF(80, 18) e.ItemHeight = itemSize.Height e.ItemWidth = itemSize.Width End Sub Private Sub Yellow_DrawItem(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles Yellow.DrawItem Dim R As New RectangleF(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height) e.Graphics.FillRectangle(Brushes.Yellow, R) End Sub Private Sub White_MeasureItem(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MeasureItemEventArgs) Handles White.MeasureItem Dim itemSize As SizeF itemSize = New SizeF(80, 18) e.ItemHeight = itemSize.Height e.ItemWidth = itemSize.Width End Sub Private Sub White_DrawItem(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles White.DrawItem Dim R As New RectangleF(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height) e.Graphics.FillRectangle(Brushes.White, R) End Sub Private Sub Black_MeasureItem(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MeasureItemEventArgs) Handles Black.MeasureItem Dim itemSize As SizeF itemSize = New SizeF(80, 18) e.ItemHeight = itemSize.Height e.ItemWidth = itemSize.Width End Sub Private Sub Black_DrawItem(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles Black.DrawItem Dim R As New RectangleF(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height) e.Graphics.FillRectangle(Brushes.Black, R) End Sub Private Sub AlignLeft_MeasureItem(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MeasureItemEventArgs) Handles AlignLeft.MeasureItem Dim itemSize As SizeF itemSize = New SizeF(40, 14) e.ItemHeight = itemSize.Height e.ItemWidth = itemSize.Width End Sub Private Sub AlignLeft_DrawItem(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles AlignLeft.DrawItem Dim R As New RectangleF(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height) Dim strfmt As New StringFormat() strfmt.Alignment = StringAlignment.Near e.Graphics.DrawString("Left", Me.Font, Brushes.Black, R, strfmt) End Sub Private Sub AlignRight_MeasureItem(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MeasureItemEventArgs) Handles AlignRight.MeasureItem Dim itemSize As SizeF itemSize = New SizeF(40, 14) e.ItemHeight = itemSize.Height e.ItemWidth = itemSize.Width End Sub Private Sub AlignRight_DrawItem(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles AlignRight.DrawItem Dim R As New RectangleF(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height) Dim strfmt As New StringFormat() strfmt.Alignment = StringAlignment.Far e.Graphics.DrawString("Right", Me.Font, Brushes.Black, R, strfmt) End Sub Private Sub AlignCenter_MeasureItem(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MeasureItemEventArgs) Handles AlignCenter.MeasureItem Dim itemSize As SizeF itemSize = New SizeF(40, 14) e.ItemHeight = itemSize.Height e.ItemWidth = itemSize.Width End Sub Private Sub AlignCenter_DrawItem(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles AlignCenter.DrawItem Dim R As New RectangleF(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height) Dim strfmt As New StringFormat() strfmt.Alignment = StringAlignment.Center e.Graphics.DrawString("Center", Me.Font, Brushes.Black, R, strfmt) End Sub
Private Sub Alignment_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AlignCenter.Click, AlignLeft.Click, AlignRight.Click,Red.Click,Green.Click,Blue.Click,Yellow.Click,Black.Click,White.Click MessageBox.Show(sender.Text) End Sub
End Class
</source>
MenuItem.MenuItems.Add
<source lang="vbnet"> Imports System Imports System.Collections Imports System.Data Imports System.IO Imports System.Xml.Serialization Imports System.Windows.Forms Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Drawing.Text Imports System.Drawing.Printing
Public Class MainClass
Shared Sub Main() Dim form1 As Form = New Form1() Application.Run(form1) End Sub
End Class
Public Class Form1
Inherits System.Windows.Forms.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 Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub Friend WithEvents MainMenu1 As System.Windows.Forms.MainMenu Friend WithEvents mnuExit As System.Windows.Forms.MenuItem Friend WithEvents mnuFile As System.Windows.Forms.MenuItem Friend WithEvents mnuNew As System.Windows.Forms.MenuItem "Required by the Windows Form Designer Private components As System.ruponentModel.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.MainMenu1 = New System.Windows.Forms.MainMenu() Me.mnuFile = New System.Windows.Forms.MenuItem() Me.mnuNew = New System.Windows.Forms.MenuItem() Me.mnuExit = New System.Windows.Forms.MenuItem() " "MainMenu1 " Me.MainMenu1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.mnuFile}) " "mnuFile " Me.mnuFile.Index = 0 Me.mnuFile.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.mnuNew, Me.mnuExit}) Me.mnuFile.Text = "File" " "mnuNew " Me.mnuNew.Index = 0 Me.mnuNew.Text = "New" " "mnuExit " Me.mnuExit.Index = 1 Me.mnuExit.Text = "Exit" " "Form1 " Me.AutoScaleBaseSize = New System.Drawing.Size(6, 15) Me.ClientSize = New System.Drawing.Size(292, 272) Me.Menu = Me.MainMenu1 Me.Name = "Form1" Me.Text = "MDI Example 2" End Sub
- End Region
Private Sub Form1_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles MyBase.Load Me.Text = "I"m an MDI Parent" Me.IsMdiContainer = True Dim MyChild As New System.Windows.Forms.Form() MyChild.MdiParent = Me MyChild.Show() MyChild.Text = "First MDI Child" InitializeMenu() End Sub Public Sub InitializeMenu() Dim mnuWindow As New MenuItem("&Window") MainMenu1.MenuItems.Add(mnuWindow) mnuWindow.MenuItems.Add(New MenuItem("&Cascade", AddressOf WindowCascade_Clicked)) mnuWindow.MenuItems.Add(New MenuItem("Tile &Horizontal", AddressOf WindowTileHoriz_Clicked)) mnuWindow.MenuItems.Add(New MenuItem("Tile &Vertical", AddressOf WindowTileVert_Clicked)) mnuWindow.MdiList = True End Sub Protected Sub WindowCascade_Clicked(ByVal Sender As Object, _ ByVal e As System.EventArgs) Me.LayoutMdi(MdiLayout.Cascade) End Sub Protected Sub WindowTileHoriz_Clicked(ByVal Sender As Object, _ ByVal e As System.EventArgs) Me.LayoutMdi(MdiLayout.TileHorizontal) End Sub Protected Sub WindowTileVert_Clicked(ByVal Sender As Object, _ ByVal e As System.EventArgs) Me.LayoutMdi(MdiLayout.TileVertical) End Sub Private Sub mnuExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuExit.Click System.Environment.Exit(0) End Sub Private Sub mnuNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuNew.Click Static numOfChildren As Integer = 2 Dim MyChild As New System.Windows.Forms.Form() MyChild.MdiParent = Me MyChild.Show() MyChild.Text = "MDI Child " & CStr(numOfChildren) numOfChildren += 1 End Sub
End Class
</source>
MenuItem.RadioCheck
<source lang="vbnet">
Imports System.Windows.Forms public class DynamicMenu
public Shared Sub Main Application.Run(New Form1) End Sub
End class Public Class Form1
Inherits System.Windows.Forms.Form Public Sub New() MyBase.New() InitializeComponent() End Sub Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) If disposing Then If Not (components Is Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub Private components As System.ruponentModel.IContainer Friend WithEvents MainMenu1 As System.Windows.Forms.MainMenu Friend WithEvents MenuItem1 As System.Windows.Forms.MenuItem Friend WithEvents MenuItem2 As System.Windows.Forms.MenuItem Friend WithEvents MenuItem3 As System.Windows.Forms.MenuItem Friend WithEvents MenuItem4 As System.Windows.Forms.MenuItem Friend WithEvents MenuItem5 As System.Windows.Forms.MenuItem Friend WithEvents MenuItem6 As System.Windows.Forms.MenuItem Friend WithEvents Button1 As System.Windows.Forms.Button Friend WithEvents Button2 As System.Windows.Forms.Button Friend WithEvents Button3 As System.Windows.Forms.Button Friend WithEvents Button4 As System.Windows.Forms.Button <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() Me.MainMenu1 = New System.Windows.Forms.MainMenu Me.MenuItem1 = New System.Windows.Forms.MenuItem Me.MenuItem2 = New System.Windows.Forms.MenuItem Me.MenuItem3 = New System.Windows.Forms.MenuItem Me.MenuItem4 = New System.Windows.Forms.MenuItem Me.MenuItem5 = New System.Windows.Forms.MenuItem Me.MenuItem6 = New System.Windows.Forms.MenuItem Me.Button1 = New System.Windows.Forms.Button Me.Button2 = New System.Windows.Forms.Button Me.Button3 = New System.Windows.Forms.Button Me.Button4 = New System.Windows.Forms.Button Me.SuspendLayout() " "MainMenu1 " Me.MainMenu1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MenuItem1, Me.MenuItem3}) " "MenuItem1 " Me.MenuItem1.Index = 0 Me.MenuItem1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MenuItem2}) Me.MenuItem1.Text = "File" " "MenuItem2 " Me.MenuItem2.Index = 0 Me.MenuItem2.Text = "Open" " "MenuItem3 " Me.MenuItem3.Index = 1 Me.MenuItem3.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MenuItem4, Me.MenuItem5, Me.MenuItem6}) Me.MenuItem3.Text = "Edit" " "MenuItem4 " Me.MenuItem4.Index = 0 Me.MenuItem4.Text = "Cut" " "MenuItem5 " Me.MenuItem5.Index = 1 Me.MenuItem5.Text = "Copy" " "MenuItem6 " Me.MenuItem6.Index = 2 Me.MenuItem6.Text = "Paste" " "Button1 " Me.Button1.Location = New System.Drawing.Point(200, 8) Me.Button1.Name = "Button1" Me.Button1.Size = New System.Drawing.Size(72, 24) Me.Button1.TabIndex = 3 Me.Button1.Text = "Select" " "Button2 " Me.Button2.Location = New System.Drawing.Point(200, 48) Me.Button2.Name = "Button2" Me.Button2.Size = New System.Drawing.Size(72, 24) Me.Button2.TabIndex = 4 Me.Button2.Text = "Disable" " "Button3 " Me.Button3.Location = New System.Drawing.Point(200, 88) Me.Button3.Name = "Button3" Me.Button3.Size = New System.Drawing.Size(72, 24) Me.Button3.TabIndex = 5 Me.Button3.Text = "RadioButton" " "Button4 " Me.Button4.Location = New System.Drawing.Point(200, 136) Me.Button4.Name = "Button4" Me.Button4.Size = New System.Drawing.Size(72, 24) Me.Button4.TabIndex = 6 Me.Button4.Text = "Hide" " "Form1 " Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(328, 225) Me.Controls.Add(Me.Button4) Me.Controls.Add(Me.Button3) Me.Controls.Add(Me.Button2) Me.Controls.Add(Me.Button1) Me.Menu = Me.MainMenu1 Me.ResumeLayout(False) End Sub Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click MenuItem4.RadioCheck = False MenuItem5.RadioCheck = False MenuItem6.RadioCheck = False MenuItem6.Checked = MenuItem4.Checked MenuItem4.Checked = Not MenuItem4.Checked MenuItem5.Checked = Not MenuItem5.Checked MenuItem6.Checked = Not MenuItem6.Checked End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click MenuItem4.RadioCheck = False MenuItem5.RadioCheck = False MenuItem6.RadioCheck = False MenuItem6.Checked = MenuItem4.Checked MenuItem4.Enabled = Not MenuItem4.Enabled MenuItem5.Enabled = Not MenuItem5.Enabled MenuItem6.Enabled = Not MenuItem6.Enabled End Sub Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click MenuItem4.Checked = False MenuItem5.Checked = False MenuItem6.Checked = True MenuItem6.RadioCheck = True End Sub Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click MenuItem6.Visible = Not MenuItem6.Visible End Sub
End Class
</source>
MenuItem.Visible
<source lang="vbnet">
Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Windows.Forms public class OwnerDrawMenuAndSubmenu
public Shared Sub Main Application.Run(New Form1) End Sub
End class Public Class Form1
Inherits System.Windows.Forms.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 Nothing) Then components.Dispose() End If End If MyBase.Dispose(disposing) End Sub "Required by the Windows Form Designer Private components As System.ruponentModel.IContainer "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. Friend WithEvents MainMenu1 As System.Windows.Forms.MainMenu Friend WithEvents MenuItem1 As System.Windows.Forms.MenuItem Friend WithEvents MenuItem2 As System.Windows.Forms.MenuItem Friend WithEvents MenuItem3 As System.Windows.Forms.MenuItem Friend WithEvents MenuItem4 As System.Windows.Forms.MenuItem Friend WithEvents MenuItem5 As System.Windows.Forms.MenuItem Friend WithEvents MenuItem6 As System.Windows.Forms.MenuItem Friend WithEvents MenuItem7 As System.Windows.Forms.MenuItem Friend WithEvents MenuItem8 As System.Windows.Forms.MenuItem Friend WithEvents MenuItem9 As System.Windows.Forms.MenuItem <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() Me.MainMenu1 = New System.Windows.Forms.MainMenu Me.MenuItem1 = New System.Windows.Forms.MenuItem Me.MenuItem2 = New System.Windows.Forms.MenuItem Me.MenuItem7 = New System.Windows.Forms.MenuItem Me.MenuItem8 = New System.Windows.Forms.MenuItem Me.MenuItem9 = New System.Windows.Forms.MenuItem Me.MenuItem3 = New System.Windows.Forms.MenuItem Me.MenuItem4 = New System.Windows.Forms.MenuItem Me.MenuItem5 = New System.Windows.Forms.MenuItem Me.MenuItem6 = New System.Windows.Forms.MenuItem Me.SuspendLayout() " "MainMenu1 " Me.MainMenu1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MenuItem1}) " "MenuItem1 " Me.MenuItem1.Index = 0 Me.MenuItem1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MenuItem2, Me.MenuItem3, Me.MenuItem4, Me.MenuItem5, Me.MenuItem6}) Me.MenuItem1.Text = "&File" " "MenuItem2 " Me.MenuItem2.Index = 0 Me.MenuItem2.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MenuItem7, Me.MenuItem8, Me.MenuItem9}) Me.MenuItem2.Text = "Item 1" " "MenuItem7 " Me.MenuItem7.Index = 0 Me.MenuItem7.OwnerDraw = True Me.MenuItem7.Text = "Item 5" " "MenuItem8 " Me.MenuItem8.Checked = True Me.MenuItem8.Index = 1 Me.MenuItem8.Text = "Item 6" " "MenuItem9 " Me.MenuItem9.Index = 2 Me.MenuItem9.Text = "Item 7" " "MenuItem3 " Me.MenuItem3.Index = 1 Me.MenuItem3.Text = "Item 2" " "MenuItem4 " Me.MenuItem4.Index = 2 Me.MenuItem4.Text = "Item 3" " "MenuItem5 " Me.MenuItem5.Index = 3 Me.MenuItem5.Text = "-" " "MenuItem6 " Me.MenuItem6.Index = 4 Me.MenuItem6.Shortcut = System.Windows.Forms.Shortcut.CtrlX Me.MenuItem6.Text = "E&xit" " "Form1 " Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(292, 273) Me.Menu = Me.MainMenu1 Me.Name = "Form1" Me.Text = "Form1" Me.ResumeLayout(False) End Sub
- End Region
Private Sub MenuItem6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem6.Click End End Sub Private Sub MenuItem9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem9.Click MsgBox("You clicked Item 7.") End Sub Private Sub MenuItem8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem8.Click MenuItem8.Checked = Not MenuItem8.Checked End Sub Private Sub MenuItem4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem4.Click MenuItem4.Visible = False End Sub Private Sub MenuItem3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem3.Click MenuItem3.Enabled = False End Sub Private Sub MenuItem7_MeasureItem(ByVal sender As Object, ByVal e As System.Windows.Forms.MeasureItemEventArgs) Handles MenuItem7.MeasureItem e.ItemHeight = 15 e.ItemWidth = 60 End Sub Private Sub MenuItem7_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles MenuItem7.DrawItem Dim penRed As New Pen(Color.Red) e.Graphics.DrawEllipse(penRed, e.Bounds) End Sub
End Class
</source>