Button class in Windows Forms represents a Button control. Whenever button clicks, the Click event handler is invoked. You can place code in the Click event handler to perform any action you choose.

Drag and drop Button control from toolbox on windows form.

Button Control in VB.Net

Drag and drop a TextBox change the name of Button

Button Control in VB.Net

Code:

 Public Class Form3
 
 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'TextBox1 value will show in MessageBox
        MessageBox.Show(TextBox1.Text)
 End Sub
End Class

Run the Project

Button Control in VB.Net

When you insert any value or text and click submit button then inserted text will show in the Message box.

Button Control in VB.Net

Designating a Button as the Cancel Button.

On Windows Form you can designate a Button control to be the cancel button. A cancel button is clicked whenever the user presses the ESC key.

Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load       
        'set button1 as cancell button
        Me.CancelButton = Button1
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Me.Close()'close window Form
End Sub

Output:

Button Control in VB.Net

When you press Esc key on the keyboard then Window Form will closed.

 Properties of Button:

BackColor

Through BackColor properties we can change BackColor of Button.

Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'change backcolor of button
        Button1.BackColor = Color.CadetBlue
End Sub

At run time backcolor of button will be changed.

Button Control in VB.Net

 

ForeColor:

Through BackColor properties we can change ForeColor of Button.

 Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ' change ForeColor of button text
        Button1.ForeColor = Color.Red
    End Sub

At run time Forecolor of button will be changed.

Button Control in VB.Net

We can change both BackColor and ForeColor simultaneously.

 Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        '' change BackColor of button tex
        Button1.BackColor = Color.CadetBlue
        ' change ForeColor of button text
        Button1.ForeColor = Color.Red
    End Sub


Button Control in VB.Net

  Modified On Mar-22-2018 07:19:31 AM

Leave Comment