How to set the TextBox digital only?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi, men.
It's just a small problem. On previous VB, I can reset the keycode in the
KeyDown or KeyUp event. But in VB.NET, The property of parameter "e" is
readonly. How can I have a TextBox just only could be imputed digital.
Without any letter.

Thx
 
There should be a better way to do this, but since I haven't looked I can
tell you about two ways that can work for you.

1. Use SendKeys with an Ignore flag
I expect you are trying to change the keys inputted to some other value.
Well when the user presses a key, set a Flag like IgnoreKey and then use
sendkeys to use the corrected input to the textbox, only this time IgnoreKey
will not process the keystroke and pass it as-is to the textbox. Then you
can set IgnoreKey back to false.

Whew... I hope you got what I tried to explain.

2. Modify the SelText property directly.
Modifying the Text property is one memory intensive momma, so use the
seltext property instead... Put some code like this in your keydown event.

myTextbox.Seltext = mykeychar

hmm....

Personally I really loved it when we could simply modify the KeyCode, I
built a nice little app which sold a few thousand copies using just that
functionality. :)

Regards
Cyril Gupta
 
It's just a small problem. On previous VB, I can reset the keycode in the
KeyDown or KeyUp event. But in VB.NET, The property of parameter "e" is
readonly. How can I have a TextBox just only could be imputed digital.
Without any letter.

Digits (0-9) and no leters (a-z) ?

Masked Edit Control ?
 
This is definitely an easier way to do it... I misunderstood your question a
bit. I didn't quite understand what you meant by digital input and thought
you needed added functionality like changing the keystroke values.

Definitely recommend the Masked Edit Control. It's included in VS2005.

Regards
Cyril Gupta
 
Steven.Xu said:
It's just a small problem. On previous VB, I can reset the keycode in the
KeyDown or KeyUp event. But in VB.NET, The property of parameter "e" is
readonly. How can I have a TextBox just only could be imputed digital.

\\\
Imports System.ComponentModel.Component
Imports System.Text.RegularExpressions
..
..
..
Private Sub TextBox1_Validating( _
ByVal sender As Object, _
ByVal e As CancelEventArgs _
) Handles TextBox1.Validating
Dim SourceControl As TextBox = DirectCast(sender, TextBox)
Dim ErrorText As String
If Not Regex.IsMatch(SourceControl.Text, "^\d*$") Then
ErrorText = "Value must consist of decimal digits."
End If
Me.ErrorProvider1.SetError( _
SourceControl, _
ErrorText _
)
End Sub
///
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top