Dynamicly control user input

petiot

New member
Joined
Aug 3, 2007
Messages
2
Programming Experience
1-3
Hi all. I am new to this forum so thanks in advance for your help.

I am trying to control what a user can input in a text box. So far i have used the "keypress" event, "e.KeyValue" and "e.handled" to prevent user from entering a space character (Chr(32)).

What i want to do is to automatically replace the space by an underscore when the user input space in the text box: i.e. i want the space bar to type an underscore character, basically.

any clues?

Thanks

Dan
 
Hi. I am new to these forums too.

You just want to get what the user pressed, then check to see if it is Chr32 (space).

VB.NET:
    Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
        If e.KeyCode = Keys.Space Then
          'Replace Space for "_"
        End If
    End Sub

There ya go. If you have any questions please ask.
 
Last edited:
almost

Hi Wiz kid,

thanks for your help. My problem was that using the replace function to change the text box text was putting the caret at the beginning of the line, hence interrupting the input flow.

Your code gave me the beginning of the solution. Here it is.

Private Sub txtBox1_TextChanged(ByVal sender As Object, ByVal e As _ System.EventArgs) Handles txtBox1.TextChanged
txtBox1.Text = txtBox1.Text.Replace(Chr(32), Chr(95))
txt_txtBox1.SelectionStart = txtBox1.Text.Length
End Sub

Thanks.
 
Back
Top