franga2000
franga2000

Reputation: 587

VB 2012 TextBox.Clear() not working

I'm working on an encryption program and it uses a "PIN" to calculate some stuff for the encryption. I have a textbox where the user can insert the "PIN". I'd like to prevent people from entering anything but numbers. I added this on the KeyPress event:

    If Not Char.IsControl(e.KeyChar) Then 
        If Not Char.IsNumber(e.KeyChar) Then
            MsgBox("Invalid character", , "WARNING!")
            TextBox3.Clear()
        End If
    End If

It shows the msgbox and it doesn't write to the textbox until i close th emsgbox. The typed character appears in the textbox. When I write another one it works the same as before, but it only replaces the last character instead of writing another one. Is there something I'm missing because that looks like a bug to me?

Upvotes: 1

Views: 857

Answers (4)

Rob
Rob

Reputation: 3843

As MarkPM notes above, if its a key you don't want you can set e.handle=true (as you intercept the key on the keypress event) to have the system eat it.

Along with this, in stead of a pop-up, you can have a label on the form that says "Only numbers can be entered here" or something like that. Set it up so that the color of the text is red. Also set it up so the label is not normally visible.

Finally, also in the keypress event, beyond setting e.handle=true for unwanted keys, when an unwanted key comes along make the label that says "Only numbers can be entered here" visible - you can also set up a timed event to turn the label's visibility off after a few seconds. You can also throw a Beep() into the mix if you like :-)

This is less invasive then a pop-up and moves things along nicely for the user.

Upvotes: 0

Idle_Mind
Idle_Mind

Reputation: 39122

Set the ES_NUMBER windows style for your TextBox:

Public Class Form1

    Public Const GWL_STYLE As Integer = (-16)
    Public Const ES_NUMBER As Integer = &H2000

    Public Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" _
        (ByVal handle As IntPtr, ByVal nIndex As Integer) As Integer

    Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" _
        (ByVal handle As IntPtr, ByVal nIndex As Integer, ByVal dwNewLong As Integer) As Integer

    Public Sub SetNumbersOnlyTextBox(ByVal TB As TextBox)
        SetWindowLong(TB.Handle, GWL_STYLE, GetWindowLong(TB.Handle, GWL_STYLE) Or ES_NUMBER)
    End Sub

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        SetNumbersOnlyTextBox(TextBox3)
    End Sub

End Class

TextBox with ES_NUMBER window style

Upvotes: 2

Mark PM
Mark PM

Reputation: 2919

You just need to set the Handled property to true instead of clear:

e.Handled = True

Upvotes: 0

Ed Swangren
Ed Swangren

Reputation: 124632

It shows the msgbox and it doesn't write to the textbox until i close th emsgbox.

Yes, that's what modal dialogs do. They block the caller from updates until closed. That's the point; the user cannot interact with the parent until they clear the modal child.

Why not simply clear the textbox first? Better yet; don't show an annoying dialog at all. Simply disallow the user from entering invalid characters by setting e.Handled to true. However, it's a bit trickier than it sounds as you need to allow for the backspace and delete keys, disable pasting, etc.

Here's an example of a NumericTextbox: http://msdn.microsoft.com/en-us/library/ms229644(v=vs.80).aspx

Upvotes: 0

Related Questions