Home All Groups Group Topic Archive Search About

How to set the TextBox digital only?

Author
15 Jan 2006 3:02 AM
Steven.Xu
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

Author
15 Jan 2006 3:23 AM
Cyril Gupta
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
Author
15 Jan 2006 3:23 AM
Homer J Simpson
"Steven.Xu" <Steve***@discussions.microsoft.com> wrote in message
news:FC2B4C9C-8B27-4C22-A0BC-8E1D306DAF7B@microsoft.com...

> 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 ?
Author
15 Jan 2006 3:26 AM
Cyril Gupta
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
Author
15 Jan 2006 12:43 PM
Herfried K. Wagner [MVP]
"Steven.Xu" <Steve***@discussions.microsoft.com> schrieb:
> 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
///

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>