VB.Net Tutorial/GUI/Button — различия между версиями

Материал из VB Эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Текущая версия на 15:57, 26 мая 2010

Add Button to a form and attach event handler

<source lang="vbnet">Imports System.Windows.Forms public class AddButtonAndAddEventHandler

  public Shared Sub Main
       Application.Run(New Form1)
  End Sub

End class

Public Class Form1

   Private Sub ShowTheTime(ByVal sender As System.Object, ByVal e As System.EventArgs)
           Console.WriteLine( Now.ToLongTimeString())
   End Sub
   Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
       Dim dynamicButton As Button
       dynamicButton = New Windows.Forms.Button
       dynamicButton.Location = New System.Drawing.Point(144, 32)
       dynamicButton.Size = New System.Drawing.Size(99, 23)
       dynamicButton.Text = "Get Time"
       dynamicButton.UseVisualStyleBackColor = True
       dynamicButton.TabIndex = 1
       Me.Controls.Add(dynamicButton)
       " ----- Connect the button to an event handler.
       AddHandler dynamicButton.Click, AddressOf ShowTheTime
   End Sub

End Class

<Global.Microsoft.VisualBasic.rupilerServices.DesignerGenerated()> _ Partial Class Form1

   Inherits System.Windows.Forms.Form
   "Form overrides dispose to clean up the component list.
   <System.Diagnostics.DebuggerNonUserCode()> _
   Protected Overrides Sub Dispose(ByVal disposing As Boolean)
       If disposing AndAlso components IsNot Nothing Then
           components.Dispose()
       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.
   <System.Diagnostics.DebuggerStepThrough()> _
   Private Sub InitializeComponent()
       Me.SuspendLayout()
       "
       "Form1
       "
       Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
       Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
       Me.ClientSize = New System.Drawing.Size(260, 76)
       Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle
       Me.MaximizeBox = False
       Me.Name = "Form1"
       Me.Text = "Dynamic Controls"
       Me.ResumeLayout(False)
   End Sub

End Class</source>

Button Anchor

<source lang="vbnet">Imports System.Windows.Forms public class ButtonAnchor

  public Shared Sub Main
       Application.Run(New Form1)
  End Sub

End class

Public Class Form1

   Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
       Button1.Location = New System.Drawing.Point( _
           Me.ClientRectangle.Width - Button1.Width, _
           Me.ClientRectangle.Height - Button1.Height)
       Button1.Anchor = AnchorStyles.Bottom Or AnchorStyles.Right
   End Sub

End Class <Global.Microsoft.VisualBasic.rupilerServices.DesignerGenerated()> _ Partial Public Class Form1

   Inherits System.Windows.Forms.Form
   "Form overrides dispose to clean up the component list.
   <System.Diagnostics.DebuggerNonUserCode()> _
   Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
       If disposing AndAlso components IsNot Nothing Then
           components.Dispose()
       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.
   <System.Diagnostics.DebuggerStepThrough()> _
   Private Sub InitializeComponent()
       Me.Button1 = New System.Windows.Forms.Button
       Me.SuspendLayout()
       "
       "Button1
       "
       Me.Button1.Location = New System.Drawing.Point(24, 24)
       Me.Button1.Name = "Button1"
       Me.Button1.Size = New System.Drawing.Size(75, 23)
       Me.Button1.TabIndex = 0
       Me.Button1.Text = "Button1"
       "
       "Form1
       "
       Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
       Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
       Me.ClientSize = New System.Drawing.Size(185, 84)
       Me.Controls.Add(Me.Button1)
       Me.Name = "Form1"
       Me.Text = "Form1"
       Me.ResumeLayout(False)
   End Sub
   Friend WithEvents Button1 As System.Windows.Forms.Button

End Class</source>

Button click event

<source lang="vbnet">Imports System.Windows.Forms public class GetTextPasswordField

  public Shared Sub Main
       Application.Run(New FrmButtonTest)
  End Sub

End class

Public Class FrmButtonTest

  Inherits System.Windows.Forms.Form
  1. 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 lblOutput As System.Windows.Forms.Label
  Friend WithEvents txtInput As System.Windows.Forms.TextBox
  Friend WithEvents cmdShow As System.Windows.Forms.Button
  "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.cmdShow = New System.Windows.Forms.Button()
     Me.txtInput = New System.Windows.Forms.TextBox()
     Me.lblOutput = New System.Windows.Forms.Label()
     Me.SuspendLayout()
     "
     "cmdShow
     "
     Me.cmdShow.Font = New System.Drawing.Font("Microsoft Sans Serif", 11.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
     Me.cmdShow.Location = New System.Drawing.Point(88, 88)
     Me.cmdShow.Name = "cmdShow"
     Me.cmdShow.Size = New System.Drawing.Size(96, 40)
     Me.cmdShow.TabIndex = 2
     Me.cmdShow.Text = "Show Me"
     "
     "txtInput
     "
     Me.txtInput.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
     Me.txtInput.Location = New System.Drawing.Point(16, 16)
     Me.txtInput.Name = "txtInput"
     Me.txtInput.PasswordChar = Microsoft.VisualBasic.ChrW(42)
     Me.txtInput.Size = New System.Drawing.Size(232, 22)
     Me.txtInput.TabIndex = 0
     Me.txtInput.Text = ""
     "
     "lblOutput
     "
     Me.lblOutput.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
     Me.lblOutput.Font = New System.Drawing.Font("Microsoft Sans Serif", 9.75!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
     Me.lblOutput.Location = New System.Drawing.Point(16, 56)
     Me.lblOutput.Name = "lblOutput"
     Me.lblOutput.Size = New System.Drawing.Size(232, 23)
     Me.lblOutput.TabIndex = 1
     "
     "FrmButtonTest
     "
     Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
     Me.ClientSize = New System.Drawing.Size(264, 141)
     Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.cmdShow, Me.lblOutput, Me.txtInput})
     Me.Name = "FrmButtonTest"
     Me.Text = "LabelTextBoxButtonExample"
     Me.ResumeLayout(False)
  End Sub
  1. End Region
  Private Sub cmdShow_Click(ByVal sender As System.Object, _
     ByVal e As System.EventArgs) Handles cmdShow.Click
     lblOutput.Text = txtInput.Text
  End Sub

End Class</source>

Button click event and text changed event

<source lang="vbnet">Imports System.Windows.Forms public class ButtonClickTextChanged

  public Shared Sub Main
       Application.Run(New Form1)
  End Sub

End class Public Class Form1

   Inherits System.Windows.Forms.Form
  1. 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 Button1 As System.Windows.Forms.Button
   <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
       Me.Button1 = New System.Windows.Forms.Button()
       Me.SuspendLayout()
       "
       "Button1
       "
       Me.Button1.Location = New System.Drawing.Point(96, 72)
       Me.Button1.Name = "Button1"
       Me.Button1.TabIndex = 0
       Me.Button1.Text = "Button1"
       "
       "Form1
       "
       Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
       Me.ClientSize = New System.Drawing.Size(280, 181)
       Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Button1})
       Me.Name = "Form1"
       Me.Text = "EventDemo"
       Me.ResumeLayout(False)
   End Sub
  1. End Region
   Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
       MsgBox("Click event occurred")
       If (Button1.Text = "Button1") Then
           Button1.Text = "Clicked"
       Else
           Button1.Text = "Button1"
       End If
   End Sub
   Private Sub Button1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.TextChanged
       MsgBox("Textchanged event occurred")
   End Sub

End Class</source>

Button FlatStyle

<source lang="vbnet">imports System imports System.Drawing imports System.Windows.Forms public class ButtonFlatStyle : inherits Form

 dim btn as Button
 dim i as integer
 dim theStyles as FlatStyle()
 dim img as Image  = Image.FromFile("yourfile.jpg")
 public sub New()
       Text = "Button Properties"
   Size = new Size(300,200)
   btn = new Button()
   btn.Parent = me
   btn.Text = btn.FlatStyle.ToString()
   btn.Location = new Point(10,10)
   btn.Size = new Size(200,100)
   btn.BackColor = Color.LightGreen
   AddHandler btn.Click, AddressOf btn_Click
   btn.Image = img
   btn.ImageAlign = ContentAlignment.MiddleRight
   btn.TextAlign = ContentAlignment.MiddleLeft
   dim theEnum as new FlatStyle()
   theStyles = CType([Enum].GetValues(theEnum.GetType()), FlatStyle())
 end sub
 public shared sub Main() 
   Application.Run(new ButtonFlatStyle())
 end sub
 private sub btn_Click(ByVal sender as object, ByVal e as EventArgs)
   Dim btn as Button = CType(sender, Button)
   btn.FlatStyle = theStyles(i)
   btn.Text = btn.FlatStyle.ToString()
   if (i < theStyles.Length - 1) then
     i = i + 1
   else
     i = 0
   end if
 end sub

end class</source>

Button ImageList

<source lang="vbnet">imports System imports System.Drawing imports System.Windows.Forms public class ButtonImageList : inherits Form

 dim imgList as ImageList = new ImageList()
   dim lbl as Label 
   dim lnk as LinkLabel
   dim btn as Button
   dim nmbrUpDown as NumericUpDown
 public sub New()
      Size = new Size(300,300)
   dim img as Image
   dim i as integer
   dim arFiles as string() = {"1.ico","2.ico","3.ico","4.ico"}
   for i = 0 to arFiles.Length - 1
     img = Image.FromFile(arFiles(i))
     imgList.Images.Add(img)
   next
   btn = new Button()
   btn.Parent = me
   btn.ImageList = imgList
   btn.ImageIndex = imgList.Images.Count - 1
   btn.Location = new Point(0, 0)
   btn.Size = new Size(200,20)
   "  Create numeric updown to select the image
   nmbrUpDown = new NumericUpDown()
   nmbrUpDown.Parent = me
   nmbrUpDown.Location = new Point(0, 60)
   nmbrUpDown.Value = 0
   nmbrUpDown.Minimum = 0
   nmbrUpDown.Maximum = imgList.Images.Count - 1
   nmbrUpDown.Width = 50
   nmbrUpDown.ReadOnly = true
   AddHandler nmbrUpDown.ValueChanged,AddressOf nmbrUpDown_ValueChanged
   end sub
   public shared sub Main() 
     Application.Run(new ButtonImageList())
   end sub
   private sub nmbrUpDown_ValueChanged(ByVal sender as object,ByVal e as EventArgs)
     dim n as NumericUpDown  = CType(sender, NumericUpDown)
   btn.ImageIndex = CType(n.Value, Integer)
   end sub

end class</source>

Button PerformClick

<source lang="vbnet">Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Windows.Forms public class ButtonPerformClick

  public Shared Sub Main
       Application.Run(New Form1)
  End Sub

End class Public Class Form1

   Inherits System.Windows.Forms.Form
  1. 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 Button1 As System.Windows.Forms.Button
   Friend WithEvents Button2 As System.Windows.Forms.Button
   Friend WithEvents Label1 As System.Windows.Forms.Label
   <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
       Me.Button1 = New System.Windows.Forms.Button
       Me.Button2 = New System.Windows.Forms.Button
       Me.Label1 = New System.Windows.Forms.Label
       Me.SuspendLayout()
       "
       "Button1
       "
       Me.Button1.Location = New System.Drawing.Point(72, 80)
       Me.Button1.Name = "Button1"
       Me.Button1.Size = New System.Drawing.Size(184, 23)
       Me.Button1.TabIndex = 0
       Me.Button1.Text = "Click Me to Click the Other Button"
       "
       "Button2
       "
       Me.Button2.Location = New System.Drawing.Point(72, 160)
       Me.Button2.Name = "Button2"
       Me.Button2.Size = New System.Drawing.Size(184, 23)
       Me.Button2.TabIndex = 1
       Me.Button2.Text = "The Other Button"
       "
       "Label1
       "
       Me.Label1.Font = New System.Drawing.Font("Microsoft Sans Serif", 14.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
       Me.Label1.Location = New System.Drawing.Point(0, 0)
       Me.Label1.Name = "Label1"
       Me.Label1.Size = New System.Drawing.Size(328, 40)
       Me.Label1.TabIndex = 2
       Me.Label1.Text = "Clicked"
       "
       "Form1
       "
       Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
       Me.ClientSize = New System.Drawing.Size(328, 273)
       Me.Controls.Add(Me.Label1)
       Me.Controls.Add(Me.Button2)
       Me.Controls.Add(Me.Button1)
       Me.Name = "Form1"
       Me.Text = "Form1"
       Me.ResumeLayout(False)
   End Sub
  1. End Region
   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
       Button2.PerformClick()
   End Sub
   Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
       Button2.Text = "I"ve been clicked!"
   End Sub

End Class</source>

Button Properties

<source lang="vbnet">imports System imports System.Drawing imports System.Windows.Forms public class ButtonFlatStyle : inherits Form

 dim btn as Button
 dim i as integer
 dim theStyles as FlatStyle()
 dim img as Image  = Image.FromFile("yourfile.jpg")
 public sub New()
       Text = "Button Properties"
   Size = new Size(300,200)
   btn = new Button()
   btn.Parent = me
   btn.Text = btn.FlatStyle.ToString()
   btn.Location = new Point(10,10)
   btn.Size = new Size(200,100)
   btn.BackColor = Color.LightGreen
   AddHandler btn.Click, AddressOf btn_Click
   btn.Image = img
   btn.ImageAlign = ContentAlignment.MiddleRight
   btn.TextAlign = ContentAlignment.MiddleLeft
   dim theEnum as new FlatStyle()
   theStyles = CType([Enum].GetValues(theEnum.GetType()), FlatStyle())
 end sub
 public shared sub Main() 
   Application.Run(new ButtonFlatStyle())
 end sub
 private sub btn_Click(ByVal sender as object, ByVal e as EventArgs)
   Dim btn as Button = CType(sender, Button)
   btn.FlatStyle = theStyles(i)
   btn.Text = btn.FlatStyle.ToString()
   if (i < theStyles.Length - 1) then
     i = i + 1
   else
     i = 0
   end if
 end sub

end class</source>

Button with background image

<source lang="vbnet">Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Windows.Forms public class ButtonBackgroundImage

  public Shared Sub Main
       Application.Run(New Form1)
  End Sub

End class Public Class Form1

   Inherits System.Windows.Forms.Form
  1. 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 Button1 As System.Windows.Forms.Button
   Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
   Friend WithEvents Label1 As System.Windows.Forms.Label
   <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
       Me.Button1 = New System.Windows.Forms.Button
       Me.TextBox1 = New System.Windows.Forms.TextBox
       Me.Label1 = New System.Windows.Forms.Label
       Me.SuspendLayout()
       "
       "Button1
       "
       Me.Button1.BackgroundImage = System.Drawing.Image.FromFile("yourfile.jpg")
       Me.Button1.Location = New System.Drawing.Point(8, 64)
       Me.Button1.Name = "Button1"
       Me.Button1.Size = New System.Drawing.Size(368, 20)
       Me.Button1.TabIndex = 0
       "
       "TextBox1
       "
       Me.TextBox1.Location = New System.Drawing.Point(104, 184)
       Me.TextBox1.Name = "TextBox1"
       Me.TextBox1.Size = New System.Drawing.Size(168, 20)
       Me.TextBox1.TabIndex = 1
       Me.TextBox1.Text = ""
       "
       "Label1
       "
       Me.Label1.Font = New System.Drawing.Font("Microsoft Sans Serif", 24.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
       Me.Label1.Location = New System.Drawing.Point(0, 0)
       Me.Label1.Name = "Label1"
       Me.Label1.Size = New System.Drawing.Size(232, 48)
       Me.Label1.TabIndex = 2
       Me.Label1.Text = "Image Buttons"
       "
       "Form1
       "
       Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
       Me.ClientSize = New System.Drawing.Size(384, 229)
       Me.Controls.Add(Me.Label1)
       Me.Controls.Add(Me.TextBox1)
       Me.Controls.Add(Me.Button1)
       Me.Name = "Form1"
       Me.Text = "Form1"
       Me.ResumeLayout(False)
   End Sub
  1. End Region
   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
       TextBox1.Text = "You clicked the button."
   End Sub

End Class</source>

Irregular shape of Button

<source lang="vbnet">Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Windows.Forms public class IrregularShapeButton

  public Shared Sub Main
       Application.Run(New Form1)
  End Sub

End class

Public Class Form1

   Inherits System.Windows.Forms.Form
   Private originalSize As Size
  1. 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 AnimationBtn As System.Windows.Forms.Button
   Friend WithEvents VideoBtn As System.Windows.Forms.Button
   Friend WithEvents AudioBtn As System.Windows.Forms.Button
   Friend WithEvents ContextMenu1 As System.Windows.Forms.ContextMenu
   Friend WithEvents Circlemenu As System.Windows.Forms.MenuItem
   Friend WithEvents RectMenu As System.Windows.Forms.MenuItem
   Friend WithEvents TriangleMenu As System.Windows.Forms.MenuItem
   Friend WithEvents CloseMenu As System.Windows.Forms.MenuItem
   <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
       Me.AnimationBtn = New System.Windows.Forms.Button
       Me.VideoBtn = New System.Windows.Forms.Button
       Me.AudioBtn = New System.Windows.Forms.Button
       Me.ContextMenu1 = New System.Windows.Forms.ContextMenu
       Me.Circlemenu = New System.Windows.Forms.MenuItem
       Me.RectMenu = New System.Windows.Forms.MenuItem
       Me.TriangleMenu = New System.Windows.Forms.MenuItem
       Me.CloseMenu = New System.Windows.Forms.MenuItem
       Me.SuspendLayout()
       "
       "AnimationBtn
       "
       Me.AnimationBtn.BackColor = System.Drawing.Color.Blue
       Me.AnimationBtn.Font = New System.Drawing.Font("Tahoma", 10.0!, System.Drawing.FontStyle.Bold)
       Me.AnimationBtn.ForeColor = System.Drawing.SystemColors.ControlLightLight
       Me.AnimationBtn.Location = New System.Drawing.Point(120, 146)
       Me.AnimationBtn.Name = "AnimationBtn"
       Me.AnimationBtn.Size = New System.Drawing.Size(152, 104)
       Me.AnimationBtn.TabIndex = 6
       Me.AnimationBtn.Text = "Animation"
       "
       "VideoBtn
       "
       Me.VideoBtn.BackColor = System.Drawing.Color.Green
       Me.VideoBtn.Font = New System.Drawing.Font("Tahoma", 10.0!, System.Drawing.FontStyle.Bold)
       Me.VideoBtn.ForeColor = System.Drawing.SystemColors.ControlLightLight
       Me.VideoBtn.Location = New System.Drawing.Point(208, 26)
       Me.VideoBtn.Name = "VideoBtn"
       Me.VideoBtn.Size = New System.Drawing.Size(160, 96)
       Me.VideoBtn.TabIndex = 5
       Me.VideoBtn.Text = "Play Video"
       "
       "AudioBtn
       "
       Me.AudioBtn.BackColor = System.Drawing.Color.FromArgb(CType(255, Byte), CType(128, Byte), CType(0, Byte))
       Me.AudioBtn.Font = New System.Drawing.Font("Tahoma", 10.0!, System.Drawing.FontStyle.Bold)
       Me.AudioBtn.ForeColor = System.Drawing.SystemColors.ControlLightLight
       Me.AudioBtn.Location = New System.Drawing.Point(96, 26)
       Me.AudioBtn.Name = "AudioBtn"
       Me.AudioBtn.Size = New System.Drawing.Size(160, 96)
       Me.AudioBtn.TabIndex = 4
       Me.AudioBtn.Text = "Play Audio"
       "
       "ContextMenu1
       "
       Me.ContextMenu1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.Circlemenu, Me.RectMenu, Me.TriangleMenu, Me.CloseMenu})
       "
       "Circlemenu
       "
       Me.Circlemenu.Index = 0
       Me.Circlemenu.Text = "Circle"
       "
       "RectMenu
       "
       Me.RectMenu.Index = 1
       Me.RectMenu.Text = "Original"
       "
       "TriangleMenu
       "
       Me.TriangleMenu.Index = 2
       Me.TriangleMenu.Text = "TriangleMenu"
       "
       "CloseMenu
       "
       Me.CloseMenu.Index = 3
       Me.CloseMenu.Text = "Close"
       "
       "Form1
       "
       Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
       Me.ClientSize = New System.Drawing.Size(464, 277)
       Me.Controls.Add(Me.AnimationBtn)
       Me.Controls.Add(Me.VideoBtn)
       Me.Controls.Add(Me.AudioBtn)
       Me.ResumeLayout(False)
   End Sub
  1. End Region
   Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
       originalSize = Me.Size
       Dim path1 As New GraphicsPath(FillMode.Alternate)
       path1.AddEllipse(New Rectangle(30, 30, AudioBtn.Width - 60, AudioBtn.Height - 60))
       AudioBtn.Region = New [Region](path1)
       Dim path2 As New GraphicsPath(FillMode.Alternate)
       path2.AddEllipse(New Rectangle(30, 30, VideoBtn.Width - 60, VideoBtn.Height - 60))
       VideoBtn.Region = New [Region](path2)
       Dim path3 As New GraphicsPath(FillMode.Alternate)
       path3.AddEllipse(New Rectangle(20, 20, VideoBtn.Width - 40, VideoBtn.Height - 40))
       AnimationBtn.Region = New [Region](path3)
   End Sub
   Private Sub AudioBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AudioBtn.Click
       MessageBox.Show("Play Audio Button clicked!")
   End Sub
   Private Sub VideoBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles VideoBtn.Click
       MessageBox.Show("Play Video Button clicked!")
   End Sub
   Private Sub AnimationBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AnimationBtn.Click
       MessageBox.Show("Play Animation Button clicked!")
   End Sub
   

End Class</source>

Owner draw button

<source lang="vbnet">Imports System.Drawing Imports System.Drawing.Drawing2D Public Class OwnerDrawnButton

   Inherits System.Windows.Forms.Form
  1. 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 button4 As System.Windows.Forms.Button
   <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
       Me.button4 = New System.Windows.Forms.Button()
       Me.SuspendLayout()
       "
       "button4
       "
       Me.button4.Location = New System.Drawing.Point(40, 35)
       Me.button4.Name = "button4"
       Me.button4.Size = New System.Drawing.Size(200, 40)
       Me.button4.TabIndex = 2
       Me.button4.Text = "button4"
       "
       "OwnerDrawnButton
       "
       Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
       Me.ClientSize = New System.Drawing.Size(280, 110)
       Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.button4})
       Me.Name = "OwnerDrawnButton"
       Me.Text = "OwnerDrawnButton"
       Me.ResumeLayout(False)
   End Sub
  1. End Region
   Private Sub button4_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles button4.Paint
       Dim pen As Pen = New Pen(Color.Red, 2)
       e.Graphics.DrawRectangle(pen, 5, 5, button4.Width - 10, button4.Height - 10)
   End Sub

End Class</source>