3 Replies - 3921 Views - Last Post: 13 January 2011 - 12:57 PM Rate Topic: -----

#1 Black_Widow  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 01-February 09

Validation in WPF(Vb.NET) using ASCII

Posted 10 January 2011 - 12:30 AM

I am trying to validate a textbox by checking the ASCII values on PreviewKeyDown event of the textbox.
This method woorked fine with WinForms using vb.NET.

The problem comes wen I use it in WPF.
Is there a better method of carrying out validation of textbox in WPF forms ??

Private Sub txtname_PreviewKeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Input.KeyEventArgs) Handles txtname.PreviewKeyDown
 
 Dim no As Integer
        Dim s As String = e.Key.ToString()
        Dim chars As Char() = s.ToCharArray()
        Dim chr As Char = chars(0)
        no = System.Convert.ToInt32(chr)
        If (no >= 65 And no <= 90) Then
            e.Handled = True
                   Else
            txtname.Text = ""
        End If
 
End Sub




The problem in the above code is that it doesn't accept any characters even if e.Handled=True

Is This A Good Question/Topic? 0
  • +

Replies To: Validation in WPF(Vb.NET) using ASCII

#2 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1062
  • View blog
  • Posts: 4,143
  • Joined: 02-July 08

Re: Validation in WPF(Vb.NET) using ASCII

Posted 10 January 2011 - 05:18 AM

That's what e.Handled does.
Was This Post Helpful? 0
  • +
  • -

#3 dbasnett  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 109
  • View blog
  • Posts: 608
  • Joined: 01-October 08

Re: Validation in WPF(Vb.NET) using ASCII

Posted 10 January 2011 - 05:41 AM

In forms it looked like

    Dim allowedChars As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" 'abcdefghijklmnopqrstuvwxyz"

    Private Sub txtBoxesKeyPress(ByVal sender As Object, _
                                  ByVal e As System.Windows.Forms.KeyPressEventArgs) _
                              Handles TextBox1.KeyPress
        '
        If Not allowedChars.Contains(e.KeyChar) Then
            'bad character
            e.Handled = True
        End If
    End Sub


Was This Post Helpful? 1
  • +
  • -

#4 Black_Widow  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 01-February 09

Re: Validation in WPF(Vb.NET) using ASCII

Posted 13 January 2011 - 12:57 PM

Got it... Thanks to eclipsed4utoo


1. e.Handled = True tells the event that you have already handled the input. Therefore, it will not put the input into the textbox.

2. You are also assuming that all keypresses come in as one character. Put a breakpoint after the Dim s As String = e.Key.ToString() and then look at the value of s. You will see that for numbers and special characters, it is more than one character.

Now knowing 1 and 2, you should be able to now change your code to do what you are wanting. It seems that you only want to allow letters. Since everything other than letters have more than one character, a simple check for that should be enough.

	Dim s As String = e.Key.ToString()
	Dim chars As Char() = s.ToCharArray()
	If chars.Length > 1 Then
	    e.Handled = True
End If



basically, you are fooling the event into thinking that you have already handled the keystroke. So now, the e.Handled = true is now stopping the event from putting that input into the textbox....which is what you want. You don't want the invalid character to go to the textbox.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1