Example : How to create/set Hotkey/Shortcut Key with Button.
- Write ‘&’ symbol before the button name we want to make shortcut in ‘text’ property of 
  property window.To display the effect, press that letter with alt key.(such as &Save- use 
  alt+S)
Example : How to type/display/use ‘&’ Key/letter in VB .net 2013 form.
- Type && to display single & in your form/page at proper place.
Example : How to enable/disable the vb controls.
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        TextBox1.Enabled = True
        TextBox2.Enabled = False
        Button1.Enabled = True
End Sub
Example : How to hide/unhide the vb controls.
 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        TextBox1.Hide()
        Button1.Hide()
        DataGridView1.Hide()

        '----------------  OR  ---------------
        Button1.Visible = True
        Button1.Visible = False
 End Sub

NB : VB Controls are by default unhide/visible.
Example : How to make VB controls/box read only ON/OFF.
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        TextBox1.ReadOnly = True
        TextBox1.ReadOnly = False
End Sub
Example : How to set focus/cursor in specific VB controls/box.
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        TextBox1.Focus()
        Button1.Focus()
End Sub
Example : How to make a button toggle [Same button having two job].
If Button1.Text = "Hide" Then
   Button1.Text = "View"
ElseIf Button1.Text = "View" Then
   Button1.Text = "Hide"
Else
   Button1.Text = "Click Me"
End If

'----------------  OR  ---------------
Private Sub BtnShowHideData_Click(ByVal sender As System.Object, ByVal e  As 
    System.EventArgs) Handles BtnShowHideData.Click
       If BtnShowHideData.Text = "&Show Data " Then
           DataGridView1.Visible = True
           BtnShowHideData.Text = " &Hide Data "
       Else
           BtnShowHideData.Text = " &Hide Data "
           DataGridView1.Visible = False
           BtnShowHideData.Text = "&Show Data "
       End If
End Sub

'NB: Single button show two effects/states(toggle),i.e., Show and Hide- here they are used to 
     enable/disable Datagridview1 controls respectively on clicking the button.Here & symbol 
     before the letter makes it highlighted to use with ALT button as shortcut key.
Example : How to get input values from the user through keyboard dynamically.
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim str As String = InputBox("Enter Your Name")
        MsgBox(str)

        Dim num As Integer = InputBox("Enter Your Mobile Number")
        MsgBox(num)
End Sub
Example : How to use Calculator in our application in VB .Net 2013.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        
        Dim ts As New System.Security.SecureString
        System.Diagnostics.Process.Start("c:\WINDOWS\system32\calc.exe") 

        ----------------  OR  ---------------

        'Shell("Calc.exe")
End Sub
Example : How to Close/Exit the VB Application.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        
        Application.Exit() 
        ----------------  OR  ---------------
        Me.Close() 
        ----------------  OR  ---------------
        End 
        ----------------  OR  ---------------
        Me.Dispose()
 End Sub
Example : How to Maximize the Form in VB .Net 2013.
First Method: Dynamic or through source code - 

     Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.WindowState = FormWindowState.Maximized
     End Sub

----------------  OR  ---------------

Second Method: Static or through Property Window settings -
 
    Select the Form - Property Window - Choose "Maximized" option/attribute from "Window 
    State" property. 
Example : How to disable Maximize symbol of a Form Window.
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.MaximizeBox = False      
End Sub
Example : How to disable Minimize symbol of a Form Window.
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
       Me.MinimizeBox = False
End Sub
Example : How to disable/remove both Maximize & Minimize symbol of a Form Window.
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.MaximizeBox = False
        Me.MinimizeBox = False
    End Sub
Example : How to Lock/Unlock all vb controls used on a form.
- Open vb .net application - Design your form with vb controls - click 'Format' menu - choose 
  'Lock Controls' option - Now Vb controls locked.

NB  :To Unlock all the Controls, repeat the same process.
Example : How to Remove Extra Space from a word/sentence.
Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim str As String = "      Codershelpline    "
        TextBox1.Text = str.Trim()        
    End Sub
End Class

----------------  OR   -----------------
Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim str As New String("      Codershelpline    ")        
        TextBox1.Text = str.Replace(" ", "")
    End Sub
End Class

--------------  OR   ----------------- 
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
 Dim userName As String = TxtUname.Text.ToString().Trim()
 Dim password As String = TxtPassword.Text.ToString().Trim()

 userName = userName.Trim()
 password = password.Trim()
End Sub
Example : How to Move Cursor in Next Controls/Box on Pressing Enter key.
Private Sub Textbox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TxtVechNo.KeyDown
        If e.KeyCode = Keys.Enter Then
            Textbox2.Focus()
        End If
End Sub
Example : How to Move the Cursor in Next/Previous Controls on Pressing Up & Down                       Arrow Key. [Arrow Key Code]
Private Sub Textbox2_KeyDown(sender As Object, e As KeyEventArgs) Handles TxtVechNo.KeyDown
       If e.KeyCode = Keys.Up Then
            Textbox1.Focus()
       If e.KeyCode = Keys.Down Then
            Textbox3.Focus()
       End If 
End Sub
Example : How to Stop Empty/Blank Controls to Save data in the database.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        If TextBox1.Text = "" Then
            MsgBox("PLEASE ENTER THE VALUE IN THE TEXT BOX")
            TextBox1.Focus()
            Exit Sub
        End If

        If RichTextBox1.Text = "" Then
            MsgBox("PLEASE ENTER THE VALUE IN THE RICH TEXT BOX")
            RichTextBox1.Focus()
            Exit Sub
        End If

        If ComboBox1.Text = "" Then
            MsgBox("PLEASE SELECT ONE CHOICE FROM THE LIST")
            ComboBox1.Focus()
            Exit Sub
        End If

        If ComboBox1.Text = "Select One" Then
            MsgBox("PLEASE SELECT ONE CHOICE FROM THE LIST")
            ComboBox1.Focus()
            Exit Sub
        End If

        If CheckBox1.Checked = False And CheckBox2.Checked = False And CheckBox3.Checked = False Then
            MsgBox("PLEASE SELECT AT LEAST ONE CHECK BOX")
            CheckBox1.Focus()
            Exit Sub
        End If

        If RadioButton1.Checked = False And RadioButton2.Checked = False And RadioButton3.Checked = False Then
            MsgBox("PLEASE CHOOSE AT LEAST ONE RADIO BUTTON")
            RadioButton1.Select()
            'RadioButton1.Focus()
            Exit Sub
        End If

        If DateTimePicker1.Text = Date.Now.ToLongDateString.ToString() Then
            MsgBox("PLEASE CHANGE THE CURRENT DATE FROM THE CALENDER")
            DateTimePicker1.Focus()
            Exit Sub
        End If

        If PictureBox1.Image Is Nothing Then
            MsgBox("PLEASE UPLOAD THE PICTURE")
            PictureBox1.Focus()
            Exit Sub
        End If
End Sub

Loading

Categories: VB .Net Codes

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.