Example : How to Clear/Cancel/Empty/Default/Reset the different types of VB Controls.
TextBox1.Text = "Enter a Number"
TextBox1.Text = "N/A"
TextBox1.Text = ""
'TextBox1.Text = String.Empty
'TextBox1.Text = "00.00"
Radiobutton1.Checked = False
Checkbox1.Checked=False
Combobox1.Text="Select One"
Combobox1.SelectedIndex = 0
'Combobox1.Items.Clear() 'clear combo box
DateTimePicker1.Text = Date.Now 'To set the current system date
'DateTimePicker1.Text = Date.Now.ToShortDateString.ToString
'DateTimePicker1.Text = Date.Now.ToLongTimeString
'DateTimePicker1.Text = Date.Now.ToLongTimeString.ToString
'DateTimePicker1.Text = ""
DataGridView1.DataSource = Nothing 'To clear the contents of DataGridView1
'DataGridView1.Rows.Clear() 'clear datagridview
ClosePreviewWindow() 'For closing the preview window,if any
Picturebox1.Image = Nothing 'To Clear the image
----------- OR ------------
'Code to Clear specific VB controls on pressing escape button
Private Sub Textbox2_KeyDown(sender As Object, e As KeyEventArgs) Handles
TxtVechNo.KeyDown
If e.KeyCode = Keys.Escape Then
Textbox1.Text= ""
End if
End Sub
----------- OR ------------
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If TextBox1.TextLength <= 0 Then
MsgBox("Enter Text in this Box")
TextBox1.Focus()
Exit Sub
End If
End Sub
0 Comments