Home All Groups Group Topic Archive Search About
Author
2 Dec 2006 9:30 PM
Arne Beruldsen
I have a form where only the letters A-D are allowed in certain text boxes. 
This was easy in vb6...but I don't know what to do in vb.net 2005

Thanks

Author
2 Dec 2006 9:34 PM
Scott M.
CustomValidator control?


Show quoteHide quote
"Arne Beruldsen" <ArneBeruld***@discussions.microsoft.com> wrote in message
news:10BAD029-2E1B-4248-A263-A705647297DC@microsoft.com...
>I have a form where only the letters A-D are allowed in certain text boxes.
> This was easy in vb6...but I don't know what to do in vb.net 2005
>
> Thanks
Author
3 Dec 2006 7:25 PM
Rad [Visual C# MVP]
On Sat, 2 Dec 2006 13:30:00 -0800, Arne Beruldsen wrote:

> I have a form where only the letters A-D are allowed in certain text boxes. 
> This was easy in vb6...but I don't know what to do in vb.net 2005
>
> Thanks

Hey Arne,

What you want to handle is the keydown event of the textbox. In it you can
detect which key was pressed by checking the value of e.KeyCode

If e.KeyCode = Keys.A Then
    e.SuppressKeyPress = True
End If
Author
4 Dec 2006 4:04 PM
Claes Bergefall
Try this (in an inherited TextBox):

Protected Overrides Sub OnKeyPress(ByVal e As KeyPressEventArgs)
    If IsValid(e.KeyChar) Then
        MyBase.OnKeyPress(e)
    ElseIf Char.IsControl(e.KeyChar) Then
        MyBase.OnKeyPress(e)
    Else
        e.Handled = True
    End If
End Sub

Protected Overrides Function ProcessCmdKey(ByRef msg As
System.Windows.Forms.Message, ByVal keyData As System.Windows.Forms.Keys) As
Boolean
    'Need to prevent pasting of invalid characters
    If keyData = (Keys.Shift Or Keys.Insert) OrElse keyData = (Keys.Control
Or Keys.V) Then
        Dim data As IDataObject = Clipboard.GetDataObject
        If data Is Nothing Then
            Return MyBase.ProcessCmdKey(msg, keyData)
        Else
            Dim text As String = CStr(data.GetData(DataFormats.StringFormat,
True))
            If text = String.Empty Then
                Return MyBase.ProcessCmdKey(msg, keyData)
            Else
                For Each ch As Char In text.ToCharArray
                    If Not IsValid(ch) Then
                        Return True
                    End If
                Next
                Return MyBase.ProcessCmdKey(msg, keyData)
            End If
        End If
    Else
        Return MyBase.ProcessCmdKey(msg, keyData)
    End If
End Function

Private Function IsValid(ByVal ch As Char) As Boolean
    If ch = "A"c OrElse ch = "B"c OrElse ch = "C"c OrElse ch = "D"c Then
        Return True
    Else
        Return False
    End If
End Function

    /claes

Show quoteHide quote
"Arne Beruldsen" <ArneBeruld***@discussions.microsoft.com> wrote in message
news:10BAD029-2E1B-4248-A263-A705647297DC@microsoft.com...
>I have a form where only the letters A-D are allowed in certain text boxes.
> This was easy in vb6...but I don't know what to do in vb.net 2005
>
> Thanks