Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Automatically add a char while typing 1

Status
Not open for further replies.

LonnieJohnson

Programmer
Apr 16, 2001
2,628
US
I want to add a character to a string while a user is typing. I want to add slashes (/) to a field sort of like a date mask.

This is what I have so far in the key press event...

If Len(TextBox1.Text) = 2 Or Len(TextBox1.Text) = 5 Then
e.Handled = True
TextBox1.Text &= "/"
TextBox1.Select(TextBox1.Text.Length, 0)
End If

Two problems

1. If I type 102504 (Today's date) It will not type the third char.
2. If I back space it will not go all the way back.

I snipped this code from somewhere else. Thanks.




ProDev, MS Access Applications
Visit me at ==>
May God bless you beyond your imagination!!!
 
leave out the e.handle is true you don't need it in this case.

Christiaan Baes
Belgium

If you want to get an answer read this FAQ faq796-2540
There's no such thing as a winnable war - Sting
 
Marvelous! I am learning .Net.

1. What does that e.Handles mean?

2. When I hit the backspace it stops when it gets to either of the slashes. How can I consider back spacing?

Thanks again. You've been a big help in the past before.

ProDev, MS Access Applications
Visit me at ==>
May God bless you beyond your imagination!!!
 
I got the backspace deal worked out like so....

Code:
        If (Len(TextBox1.Text) = 2 Or Len(TextBox1.Text) = 5) _
        And Convert.ToByte(e.KeyChar) <> 8 Then

            TextBox1.Text &= "/"
            TextBox1.Select(TextBox1.Text.Length, 1)

        End If


Thanks again. I still would like to hear your explaination of the e.Handles.


ProDev, MS Access Applications
Visit me at ==>
May God bless you beyond your imagination!!!
 
if you set e.handled = true then it will skip the keypress event that caused the event in the first place, if you don't add the e.handled is true then it will just process the request.

for example if the user type 3 and you put e.handled = true in the keypress event then the 3 wont appear in the textbox. if dont put the e.handle= true or make it e.handled = false in the event then the 3 will appear.

I hope that clears things up.

Christiaan Baes
Belgium

If you want to get an answer read this FAQ faq796-2540
There's no such thing as a winnable war - Sting
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top