Example : How to set/change password character and its length in a Password Box

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        TextBox1.Text = ""
        TextBox1.PasswordChar = "#"
        TextBox1.MaxLength = 3
End Sub

Example : How to Align text in a Text Box.

 Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged       
        TextBox1.TextAlign = HorizontalAlignment.Center
        'TextBox1.TextAlign = HorizontalAlignment.Right
        'TextBox1.TextAlign = HorizontalAlignment.Left
 End Sub

Example : How to increase the size of the Text Box in VB.

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        textBox1.AutoSize = False
        textBox1.Height = 50
End Sub

Example : How to select all text on receiving got focus/cursor in a text box.

Private Sub TextBox1_GotFocus(sender As Object, e As EventArgs) Handles TextBox1.GotFocus
        TextBox1.SelectAll()
End Sub

Example : How to set Back color/Fore color in a Text Box.

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        TextBox2.BackColor = Color.Yellow
        TextBox2.ForeColor = Color.Red
End Sub

Example : How to Count the length of Text in a Text Box

Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown
        Dim x As Integer
        If e.KeyCode = Keys.Enter Then
            x = TextBox1.Text.Length
            MsgBox(x)
        End If
End Sub

Example : How to set/create tooltip in a text box .

- First of all click on 'ToolBox' in vb page - Now double click on 'Tooltip' tools - Now select the 
  'Textbox' onto which we want to apply tooltip information/message - Now click on 'Properties 
  Window' - Now select 'ToolTip on ToolTip1' choice - Type the Information/message in its box. - Now 
  run the program - Roll over the mouse onto the textbox and see the written message and rollout the 
  mouse to disappear the message.

Example : How to set placeholder in a text box .

 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        TextBox1.Text = "Enter Student Name"
        ForeColor = SystemColors.GrayText
 End Sub

Private Sub TextBox1_GotFocus(sender As Object, e As EventArgs) Handles TextBox1.GotFocus
        If TextBox1.Text = "Enter Student Name" Then
            TextBox1.Text = String.Empty
            ForeColor = SystemColors.ControlText
        End If
End Sub

Private Sub TextBox1_LostFocus(sender As Object, e As EventArgs) Handles TextBox1.LostFocus
        If TextBox1.Text = String.Empty Then
            TextBox1.Text = "Enter Student Name"
            ForeColor = SystemColors.GrayText
        End If
End Sub

Example : How to Take Only Numeric Value in a Text Box.

Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress
      If Not Char.IsNumber(e.KeyChar) AndAlso Not Char.IsControl(e.KeyChar) Then 
         e.KeyChar = ""
      End If
 End Sub
'-------------  OR  --------------
Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress
     If Not IsNumeric(e.KeyChar) And Not e.KeyChar = ChrW(Keys.Back) Then
        e.Handled = True
     End If
 End Sub
'-------------  OR  --------------
Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress
      If Not IsNumeric(TextBox1.Text) Then
        TextBox2.text=""
      End If
 End Sub

Example : How to Take Only Letters/Characters (No Symbols and Numeric) Value in a Text Box

Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress
     If Not Char.IsLetter(e.KeyChar) And Not e.KeyChar = Chr(Keys.Delete) And Not e.KeyChar = 
     Chr(Keys.Back) And Not e.KeyChar = Chr(Keys.Space) Then
            e.Handled = True
     End If
 End Sub
'-------------  OR  --------------
Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress
        If AscW(e.KeyChar) > 64 And AscW(e.KeyChar) < 91 Or AscW(e.KeyChar) > 96 And 
           AscW(e.KeyChar) < 123 Or AscW(e.KeyChar) = 8 Then
        Else
            e.KeyChar = Nothing
        End If
End Sub

Example : How to Take Only Letters & Symbols (No Numeric) Values in a Text Box

Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress
        If AscW(e.KeyChar) > 48 And AscW(e.KeyChar) < 58 Then
        Else
            e.KeyChar = Nothing
        End If
End Sub

Example : How to Take Only Numeric and Other Symbols not Letters in a Text Box

Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress
    If AscW(e.KeyChar) > 64 And AscW(e.KeyChar) < 91 Or AscW(e.KeyChar) > 96 And 
      AscW(e.KeyChar) < 123 Or AscW(e.KeyChar) = 8 Then
            e.KeyChar = Nothing
    End If
End Sub

Example : How to Take Only Numeric and Letters (Not Symbols) Values in a Text Box

Private Sub Textbox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles Textbox1.KeyPress
        If Not Char.IsLetterOrDigit(e.KeyChar) And Not e.KeyChar = ChrW(Keys.Back) Then
            e.Handled = True
        End If
End Sub

Example : How to Set Maximum Length of Letter/Number in a Text Box

Private Sub TextBox1_TextChanged(sender As Object,e As EventArgs) Handles TextBox1.TextChanged
        TextBox1.MaxLength = 15
End Sub
-------------  OR  --------------
Select Text Box - open Property window - choose Maxlength - feed the value as length (such as 15)

Example : How to Validate E-mail format in a Text Box [E-mail Validation Code]

Imports System.Text.RegularExpressions
----------------------------------
Public Class Form1
  Private Sub TxtUrEmail_KeyDown(sender As Object, e As KeyEventArgs) Handles TxtUrEmail.KeyDown
        If e.KeyCode = Keys.Enter Then
            Dim p As String            
             p="^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*
                 [a-zA-Z]$"
            If Regex.IsMatch(TxtUrEmail.Text, p) Then                
                NumUrMob.Focus()
            Else
                MsgBox("Invalid Email Format")
                TxtUrEmail.Focus()
                TxtUrEmail.SelectAll()
            End If
        End If
    End Sub
End Class

-------------  OR  --------------
Imports System.Text.RegularExpressions
Public Class Form1   

    Function emailcheck(ByVal emailaddress As String) As Boolean
        Dim pattern As String = "^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA- 
             Z][a-zA-Z\.]*[a-zA-Z]$"
        'Dim pattern As String = "^([0-9a-zA-Z]([-\.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA- 
             Z]\.)+[a-zA-Z]{2,9})$"
        Dim emailAddressMatch As Match = Regex.Match(emailaddress, pattern)
        If emailAddressMatch.Success Then
            emailcheck = True
        Else
            emailcheck = False
        End If
    End Function

    Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown
        If e.KeyCode = Keys.Enter Then
            TextBox1.BackColor = Color.White
            Dim Str As String
            Str = TextBox1.Text            
            If emailcheck(Str) = True Then                
                TextBox1.BackColor = Color.Green
            Else                
                TextBox1.BackColor = Color.Red
            End If
        End If
    End Sub    
End Class

-------------  OR  -------------
Imports System.Text.RegularExpressions
Public Class Form1    
   Function emailcheck(ByVal emailaddress As String) As Boolean
      Dim pattern As String = "^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z] 
           [a-zA-Z\.]*[a-zA-Z]$"
        Dim pattern As String = "^([0-9a-zA-Z]([-\.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA- 
           Z]\.)+[a-zA-Z]{2,9})$"
        Dim emailAddressMatch As Match = Regex.Match(emailaddress, pattern)
        If emailAddressMatch.Success Then
            emailcheck = True
        Else
            emailcheck = False
        End If
    End Function

    Private Sub TextBox1_KeyDown(sender As Object, e As KeyEventArgs) Handles TextBox1.KeyDown
        If e.KeyCode = Keys.Enter Then
            TextBox1.BackColor = Color.White
            Dim Str As String
            Str = TextBox1.Text
            If emailcheck(Str) = True Then
                TextBox1.BackColor = Color.Green
            Else
                MessageBox.Show("Please Enter Valid E-mail")
                TextBox1.Text = ""
                TextBox1.BackColor = Color.Red
            End If
        End If
    End Sub
End Class

Example : How to set only Capital/Small letters in a all text boxes.

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        'TextBox1.CharacterCasing = CharacterCasing.Upper
        'TextBox2.CharacterCasing = CharacterCasing.Upper
        'TextBox3.CharacterCasing = CharacterCasing.Upper

        TextBox1.CharacterCasing = CharacterCasing.Lower
        TextBox2.CharacterCasing = CharacterCasing.Lower
        TextBox3.CharacterCasing = CharacterCasing.Lower      
End Sub

Private Sub Textbox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles Textbox1.KeyPress
        e.KeyChar = UCase(e.KeyChar) 
End Sub

'-------------  OR  --------------
Private Sub Textbox1_KeyDown(sender As Object, e As KeyEventArgs) Handles Textbox1.KeyDown
       If e.KeyCode = Keys.Enter Then
            Textbox1.Text = UCase(Textbox1.Text)
       End If
End Sub

'-------------  OR  --------------
Private Sub Textbox1_KeyDown(sender As Object, e As KeyEventArgs) Handles Textbox1.KeyDown
       If e.KeyCode = Keys.Enter Then
           Textbox1.Text = Textbox1.Text.ToUpper
       End If
End Sub

Example : How to set only Small letters in a text box.

Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
        TextBox1.CharacterCasing = CharacterCasing.Lower
End Sub

Private Sub Textbox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles Textbox1.KeyPress        
        e.KeyChar = LCase(e.KeyChar) 
End Sub
'-------------  OR  --------------
Private Sub Textbox1_KeyDown(sender As Object, e As KeyEventArgs) Handles Textbox1.KeyDown
       If e.KeyCode = Keys.Enter Then           
            Textbox1.Text = Textbox1.Text.ToLower
       End If
End Sub
'-------------  OR  --------------
Private Sub Textbox1_KeyDown(sender As Object, e As KeyEventArgs) Handles Textbox1.KeyDown       
        If e.KeyCode = Keys.Enter Then
            Textbox1.Text = LCase(Textbox1.Text)
        End If
End Sub

Example : How to set First letter Capitals in a text box.

Private Sub Textbox1_KeyDown(sender As Object, e As KeyEventArgs) Handles Textbox1.KeyDown
        Dim str As String = "Codershelpline"
        If e.KeyCode = Keys.Enter Then            
            Dim msg As String = StrConv(str, VbStrConv.ProperCase)
            MsgBox(msg)
        End If
 End Sub

Example : How to add Data into a Text Box obtaining from the Database.

Private Sub txtDealerID_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) 
   Handles txtDealerID.KeyDown

        If e.KeyCode = Keys.Enter Then
            cn.Open()
            cm.CommandText = "Select * from Customers Where DealerID='" + txtDealerID.Text + "'"
            rdr = cm.ExecuteReader(CommandBehavior.SequentialAccess)
            While (rdr.Read())
                txtCompany.Text = rdr("Company")
                txtAddress.Text = rdr("Address")
                txtCompCODE.Text = (rdr("centercode").ToString)   'centercode is Numeric type field 
            End While
            cn.Close()
            txtDealerID.Focus()
       End If
End Sub

Example : How to accept only specific character (when typing) in a text box in vb .net 2013.

Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress
        If Not (Char.IsLetterOrDigit(e.KeyChar) Or e.KeyChar = "@" Or e.KeyChar = ".") And Not 
            Char.IsControl(e.KeyChar) Then
            e.Handled = True
        End If
End Sub

 599 total views,  1 views today

Categories: VB .Net

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.