|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Key ValidationI 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 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 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. Hey Arne,> This was easy in vb6...but I don't know what to do in vb.net 2005 > > Thanks 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 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
enter key press
best way to learn Windows Forms Merging two tables in Dataset? I only want to get the matching info based on the two key fields Database Connection Designer Variables How to set an objects property using Reflection Working with a database PIAs for Excel 9.0 Make Code Fail How Do I Kill Non-Printable Characters? Select All CheckBoxes Need some Simple Help knowing when a form is accepted??????????? |
|||||||||||||||||||||||