-4

I worked in winfoms previously. There was KeyPress event. So I can get the KeyChar.

The below code worked in winforms

Dim allowedChars as String = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz "

If allowedChars.IndexOf(e.KeyChar) = -1
    If Not e.KeyChar = Chr(Keys.Back) Then
        e.Handled = True
        Beep()
    End If
End If

But in WPF I dont know how to implement the above code?

Vishal
  • 6,238
  • 10
  • 82
  • 158

2 Answers2

4

The following is C#, but you can easily convert it to VB.NET.

private void NumericTextBox_PreviewKeyDown(object sender, KeyEventArgs e)
{
    char c = (char)KeyInterop.VirtualKeyFromKey(e.Key);

    if ("ABCDEF".IndexOf(c) < 0)
    {
        e.Handled = true;
        MessageBox.Show("Invalid character.");
    }
}

You may need to import System.Windows.Input to get KeyInterop. The code segment above goes into the PreviewKeyDown event of the TextBox.

All of the above and more can be seen here

dotNET
  • 33,414
  • 24
  • 162
  • 251
  • Thanks for your good effort to answer my question. But I want some specific letters or symbols to my textbox like A-Z,a-z,:?.I mean I want a textbox to accept only `A-Z,a-z,:?.` – Vishal Jun 14 '13 at 09:48
  • that's pretty easy to implement, isn't it? You already have the incoming character. You could use your `IndexOf` technique you have in your original code above. Let me update my code. – dotNET Jun 14 '13 at 09:59
  • @Vishal: Updated my code. You can replace ABCDEF with all the characters that you want to allow in the textbox. – dotNET Jun 14 '13 at 10:02
  • Now I want to add some special keys to the list. like backSpace, Home, End, PgUp, PgDown, Arrow Keys, Insert and all the shortcut keys of my application. Is this possible? – Vishal Jun 14 '13 at 10:29
  • 1
    Sure. You can do `if(e.Key != Key.Back && e.Key != Key.Space)` etc. to achieve that. – dotNET Jun 14 '13 at 10:38
  • sorry to say but now I got a problem with the above code. It does not accept any punctuation marks. like `,./';[]` – Vishal Jun 14 '13 at 12:52
  • I found a way from another question. My answer is posted below. – Vishal Jun 14 '13 at 13:40
1

In C#

private bool ValidChar(string _char)
{
   string Lista = @" ! "" # $ % & ' ( ) * + , - . / 0 1 2 3 4 5 6 7 8 9 : ; < = > ? @ A B C D E F G H I J K L M N O P Q R S T U V W X Y Z ";
   return Lista.IndexOf(_char.ToUpper()) != -1;
}

private void textBoxDescripcion_PreviewTextInput(object sender, TextCompositionEventArgs e)
{
    if (!ValidChar(e.Text))
         e.Handled = true;
}

In vb

Private Function ValidChar(_char As String) As Boolean
    Dim Lista As String = " ! "" # $ % & ' ( ) * + , - . / 0 1 2 3 4 5 6 7 8 9 : ; < = > ? @ A B C D E F G H I J K L M N O P Q R S T U V W X Y Z "
    Return Lista.IndexOf(_char.ToUpper()) <> -1
End Function

Private Sub textBoxDescripcion_PreviewTextInput(sender As Object, e As TextCompositionEventArgs)
    If Not ValidChar(e.Text) Then
        e.Handled = True
    End If
End Sub
Vishal
  • 6,238
  • 10
  • 82
  • 158