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

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

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

Add a Button to Frame Dynamically

<source lang="vbnet"> Imports System Imports System.ruponentModel Imports System.Windows.Forms Imports System.Data Imports System.Configuration Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Drawing.Text Imports System.Globalization Imports System.Text Imports System.Collections Public Class MainClass

   Shared Sub Main()
       Dim myform As Form = New Form1()
       Application.Run(myform)
   End Sub

End Class

Public Class Form1

   " Declare the btnHi button WithEvents.
   Private WithEvents buttonHi As Button
   " Make the new buttonHi button.
   Private Sub btnMakeHiButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMakeHiButton.Click
       buttonHi = New Button
       buttonHi.SetBounds(96, 50, 75, 23)
       buttonHi.Text = "Say Hi"
       Me.Controls.Add(buttonHi)
   End Sub
   " The user clicked the buttonHi button.
   Private Sub buttonHi_Click(ByVal sender As Object, _
    ByVal e As System.EventArgs) Handles buttonHi.Click
       MessageBox.Show("Hi")
   End Sub
   Private Sub btnRemoveHiButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRemoveHiButton.Click
       Me.Controls.Remove(buttonHi)
       buttonHi = Nothing
   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.btnMakeHiButton = New System.Windows.Forms.Button
       Me.btnRemoveHiButton = New System.Windows.Forms.Button
       Me.SuspendLayout()
       "
       "btnMakeHiButton
       "
       Me.btnMakeHiButton.Location = New System.Drawing.Point(8, 8)
       Me.btnMakeHiButton.Name = "btnMakeHiButton"
       Me.btnMakeHiButton.Size = New System.Drawing.Size(112, 23)
       Me.btnMakeHiButton.TabIndex = 0
       Me.btnMakeHiButton.Text = "Make Hi Button"
       "
       "btnRemoveHiButton
       "
       Me.btnRemoveHiButton.Location = New System.Drawing.Point(144, 8)
       Me.btnRemoveHiButton.Name = "btnRemoveHiButton"
       Me.btnRemoveHiButton.Size = New System.Drawing.Size(112, 23)
       Me.btnRemoveHiButton.TabIndex = 1
       Me.btnRemoveHiButton.Text = "Remove Hi Button"
       "
       "Form1
       "
       Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
       Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
       Me.ClientSize = New System.Drawing.Size(292, 273)
       Me.Controls.Add(Me.btnRemoveHiButton)
       Me.Controls.Add(Me.btnMakeHiButton)
       Me.Name = "Form1"
       Me.Text = "Form1"
       Me.ResumeLayout(False)
   End Sub
   Friend WithEvents btnMakeHiButton As System.Windows.Forms.Button
   Friend WithEvents btnRemoveHiButton As System.Windows.Forms.Button

End Class

      </source>


Button Paint Event

<source lang="vbnet"> Imports System Imports System.ruponentModel Imports System.Windows.Forms Imports System.Data Imports System.Configuration Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Drawing.Text Public Class MainClass

   Shared Sub Main()
       
       Dim myform As Form = New OwnerDrawnButton()
       Application.Run(myform)
   End Sub

End Class

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>


Change Button Font at Runtime

<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
  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 Button1 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.Button1 = New System.Windows.Forms.Button()
       Me.SuspendLayout()
       "
       "Button1
       "
       Me.Button1.Location = New System.Drawing.Point(72, 104)
       Me.Button1.Name = "Button1"
       Me.Button1.Size = New System.Drawing.Size(184, 48)
       Me.Button1.TabIndex = 0
       Me.Button1.Text = "Click me to change my font"
       "
       "Form1
       "
       Me.AutoScaleBaseSize = New System.Drawing.Size(6, 15)
       Me.ClientSize = New System.Drawing.Size(328, 168)
       Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Button1})
       Me.Name = "Form1"
       Me.Text = "Changing the font on the button"
       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
       Dim myFont As System.Drawing.Font
       myFont = New System.Drawing.Font("Arial", 12,FontStyle.Bold Or FontStyle.Italic)
       Me.Font = myFont
       Button1.Text = "My font was changed to this"
   End Sub

End Class

      </source>


Set button background

<source lang="vbnet"> Imports System Imports System.Drawing Imports System.Collections Imports System.ruponentModel Imports System.Windows.Forms Imports System.Data Imports System.Configuration Imports System.Resources

Public Class OptionsDialog

   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 okButton As System.Windows.Forms.Button
   Friend WithEvents changeColorButton As System.Windows.Forms.Button
   Friend WithEvents label1 As System.Windows.Forms.Label
   Friend WithEvents errorProvider1 As System.Windows.Forms.ErrorProvider
   Friend WithEvents colorDialog1 As System.Windows.Forms.ColorDialog
   Friend WithEvents _cancelButton As System.Windows.Forms.Button
   <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
       Me.okButton = New System.Windows.Forms.Button()
       Me.changeColorButton = New System.Windows.Forms.Button()
       Me._cancelButton = New System.Windows.Forms.Button()
       Me.label1 = New System.Windows.Forms.Label()
       Me.errorProvider1 = New System.Windows.Forms.ErrorProvider()
       Me.colorDialog1 = New System.Windows.Forms.ColorDialog()
       Me.SuspendLayout()
       "
       "okButton
       "
       Me.okButton.Anchor = (System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Right)
       Me.okButton.DialogResult = System.Windows.Forms.DialogResult.OK
       Me.okButton.Location = New System.Drawing.Point(32, 64)
       Me.okButton.Name = "okButton"
       Me.okButton.TabIndex = 5
       Me.okButton.Text = "OK"
       "
       "changeColorButton
       "
       Me.changeColorButton.Location = New System.Drawing.Point(120, 8)
       Me.changeColorButton.Name = "changeColorButton"
       Me.changeColorButton.TabIndex = 4
       Me.changeColorButton.Text = "Change..."
       "
       "_cancelButton
       "
       Me._cancelButton.Anchor = (System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Right)
       Me._cancelButton.CausesValidation = False
       Me._cancelButton.DialogResult = System.Windows.Forms.DialogResult.Cancel
       Me._cancelButton.Location = New System.Drawing.Point(120, 64)
       Me._cancelButton.Name = "_cancelButton"
       Me._cancelButton.TabIndex = 6
       Me._cancelButton.Text = "Cancel"
       "
       "label1
       "
       Me.label1.Location = New System.Drawing.Point(8, 8)
       Me.label1.Name = "label1"
       Me.label1.TabIndex = 3
       Me.label1.Text = "Favorite Color"
       Me.label1.TextAlign = System.Drawing.ContentAlignment.MiddleLeft
       "
       "errorProvider1
       "
       Me.errorProvider1.DataMember = Nothing
       "
       "OptionsDialog
       "
       Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
       Me.ClientSize = New System.Drawing.Size(208, 102)
       Me.Controls.AddRange(New System.Windows.Forms.Control() {Me._cancelButton, Me.label1, Me.okButton, Me.changeColorButton})
       Me.Name = "OptionsDialog"
       Me.Text = "OptionsDialog"
       Me.ResumeLayout(False)
   End Sub
  1. End Region
   Private Sub changeColorButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles changeColorButton.Click
       If colorDialog1.ShowDialog() = DialogResult.OK Then
           changeColorButton.BackColor = colorDialog1.Color
       End If
   End Sub
   Public Property FavoriteColor() As Color
       Get
           Return colorDialog1.Color
       End Get
       Set(ByVal Value As Color)
           changeColorButton.BackColor = Value
           colorDialog1.Color = Value
       End Set
   End Property
   Private Sub cancelButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles _cancelButton.Click
   End Sub
   Shared Sub Main()
       Dim myform As Form = New OptionsDialog()
       Application.Run(myform)
   End Sub

End Class

      </source>


Simple Button action

<source lang="vbnet"> Imports System Imports System.Windows.Forms Class MyFirstForm

   Inherits Form
   Private WithEvents mybutton As Button
   Public Sub New()
       Me.Text = "Hello, WinForms!"
       mybutton = New Button()
       mybutton.Text = "Click Me!"
       Me.Controls.Add(mybutton)
   End Sub
   Public Sub mybutton_Click(sender As Object, e As EventArgs) Handles mybutton.Click
       MessageBox.Show("Message")
   End Sub
   Shared Sub Main()
       Dim myform As Form = New MyFirstForm()
       Application.Run(myform)
   End Sub

End Class

      </source>


Simple Button Click action

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

Module Module1

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

End Module


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 RichTextBox1 As System.Windows.Forms.RichTextBox
   <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
       Me.Button1 = New System.Windows.Forms.Button()
       Me.RichTextBox1 = New System.Windows.Forms.RichTextBox()
       Me.SuspendLayout()
       "
       "Button1
       "
       Me.Button1.Location = New System.Drawing.Point(120, 272)
       Me.Button1.Name = "Button1"
       Me.Button1.TabIndex = 1
       Me.Button1.Text = "Button1"
       "
       "RichTextBox1
       "
       Me.RichTextBox1.Location = New System.Drawing.Point(32, 24)
       Me.RichTextBox1.Name = "RichTextBox1"
       Me.RichTextBox1.Size = New System.Drawing.Size(264, 232)
       Me.RichTextBox1.TabIndex = 2
       Me.RichTextBox1.Text = ""
       "
       "Form1
       "
       Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
       Me.ClientSize = New System.Drawing.Size(328, 309)
       Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.RichTextBox1, Me.Button1})
       Me.Name = "Form1"
       Me.Text = "ButtonDemo"
       Me.ResumeLayout(False)
   End Sub
  1. End Region
   Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
       MessageBox.Show("clicked")
   End Sub

End Class

      </source>