add text to textbox?

aliusman

Member
Joined
Jul 5, 2011
Messages
9
Programming Experience
Beginner
tell me when i type any web site address in textbox1 and i press ctrl+enter it will add www. and .com.
 
Handle the KeyDown event of the TextBox and check the keys from 'e' event parameter.
Get/set text to the TextBox using the Text property.
 
For alphabetic key you can use
<code>
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
If e.KeyChar = "a" Then
MsgBox("got it")
End If
End Sub
</code>

But for enter key you can try


<code>
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
Dim data As String = TextBox1.Text
If e.KeyChar = Chr(13) Then
TextBox1.Text = "http://www." & Data & ".com"
End If
End Sub
</code>

try using different combination for different results !!
 
tell me when i type any web site address in textbox1 and i press ctrl+enter it will add www. and .com.

handle the keydown event like johnH said. you'd probably want to check first that the text doesn't already contain the prefix/suffix you want to add. then add it if it doesn't.

example code:
    Private Sub TextBox1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
        If e.Control AndAlso e.KeyCode = Keys.Enter Then
            If Not TextBox1.Text.StartsWith("www.") Then
                TextBox1.Text = "www." & TextBox1.Text
            End If

            If Not TextBox1.Text.EndsWith(".com") Then
                TextBox1.Text &= ".com"
            End If

            e.SuppressKeyPress = True 'to prevent beep
        End If
    End Sub
 
Back
Top