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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

replacing the character that is pressed by another 1

Status
Not open for further replies.

stx

Programmer
Sep 24, 2002
62
BE
hi

following problem:
I have a textbox. When the decimal point is pressed on my keyboard i need it to be replaced by the comma.

I vb6 that was easy. you just had to replace the keyascii value in the keypress event.

In .NET the e.keychar seems to be read-only and cannot be replaced.

Is there a way to get around this or another way to solve the problem.

Any thoughts are welcome.
thnx
 
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
If e.KeyChar = "."c Then
e.Handled = True
TextBox1.Text &= ","c
TextBox1.Select(TextBox1.Text.Length, 0)
End If
End Sub Mark [openup]
 
Thanks for the code snippet. I don't understand VB.net (VB6 seemed much simpler). I modified your code to only allow numeric entries in a text box:

Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
If e.KeyChar > Chr(57) Or e.KeyChar < Chr(48) Then
e.Handled = True
TextBox1.Text &= Chr(0)
TextBox1.Select(TextBox1.Text.Length, 0)
End If
End Sub

It works fine, although I don't entirely understand what it does. Why did you use the letter &quot;c&quot; in two lines of your code?
 
c is the only way in vb.net that you can specify you are using a character literal. I guess it would not have made that much difference to use a string literal. In fact, with the second time, it might even have been more efficient.



Mark [openup]
 
What if the user is inserting the decimal point in the middle.
Try SendKeys
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
If e.KeyChar = &quot;.&quot; Then
e.Handled = True
SendKeys.SendWait(&quot;,&quot;)
End If
End Sub


Forms/Controls Resizing/Tabbing Control
Compare Code (Text)
Generate Sort Class in VB or VBScript
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top