You must Sign In to post a response.
  • Category: WPF

    Textbox only allow characters from a-z

    hi

    i want to make a textbox in wpf C# which accepts characters from a-z.now i used the code as
    private void txt_name_PreviewKeyDown(object sender, KeyEventArgs e)
    {
    if (char.IsLetter((char)e.Key)) e.Handled = true;

    }
    but the textbox not allowing v,w,x,y,z pls help me with a right code
  • #762163
    Hi

    Can You try this Code for WPF Concept

    Design Side


    <Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="889" Left="10">
    <Grid>
    <TextBox HorizontalAlignment="Left" Height="23" Margin="19,114,0,0"
    TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="840" Name="TxtName"
    KeyDown="TxtName_KeyDown" />
    </Grid>
    </Window>



    I have mention Below Code for WPF Windows Application Code -> C# Language Code


    private void TxtName_KeyDown(object sender, KeyEventArgs e)
    {
    if (e.Key >= Key.A && e.Key <= Key.Z)
    {
    }
    else
    {
    e.Handled = true;
    }
    }


    In this code we canot enter the numeric value only A-Z value only allow check them.

    I have attached Snapshot also check this

    Name : Dotnet Developer-2015
    Email Id :kumaraspcode2009@gmail.com

    'Not by might nor by power, but by my Spirit,' says the LORD Almighty.

    Delete Attachment

  • #762164
    Hi

    Can you try this code


    private void TxtName_PreviewKeyDown(object sender, KeyEventArgs e)
    {
    if (!Char.IsLetter((char)KeyInterop.VirtualKeyFromKey(e.Key)) & e.Key != Key.Back | e.Key == Key.Space)
    {
    e.Handled = true;
    MessageBox.Show("Only A-Z only");
    }
    }

    Name : Dotnet Developer-2015
    Email Id :kumaraspcode2009@gmail.com

    'Not by might nor by power, but by my Spirit,' says the LORD Almighty.

  • #762166
    thank you for your reply
    dear Dotnet Developer-2015, your code is working but when i press tab key in the keyboard for going to next textbox, showing the messagebox("Only A-Z only");
    pls help

  • #762167
    Hi
    Aparna

    If My Code Fixed in Your Issue means

    Select as best answer . which is have in my Response below.

    Name : Dotnet Developer-2015
    Email Id :kumaraspcode2009@gmail.com

    'Not by might nor by power, but by my Spirit,' says the LORD Almighty.

  • #762168
    Hi
    Apar

    You can follow this code for when the press tab key focusing next control working Good for me.

    Design Side


    <Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="889" Left="10">
    <Grid>
    <TextBox HorizontalAlignment="Left" Height="23" Margin="254,51,0,0"
    TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="112" Name="TxtName"
    KeyDown="TxtName_KeyDown" PreviewKeyDown="TxtName_PreviewKeyDown" />
    <Label Content="Label" HorizontalAlignment="Left" Margin="171,51,0,0" VerticalAlignment="Top"/>
    <TextBox HorizontalAlignment="Left" Height="23" Margin="254,132,0,0"
    TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="112" x:Name="TxtName_Copy"
    KeyDown="TxtName_KeyDown" PreviewKeyDown="TxtName_PreviewKeyDown" />
    <Label Content="Label" HorizontalAlignment="Left" Margin="171,132,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.553,3.731"/>
    </Grid>
    </Window>



    I have mention Below Code for WPF Windows Application Code -> C# Language Code



    private void TxtName_PreviewKeyDown(object sender, KeyEventArgs e)
    {
    if (!Char.IsLetter((char)KeyInterop.VirtualKeyFromKey(e.Key)) & e.Key != Key.Back | e.Key == Key.Space)
    {
    if (e.Key != Key.Tab)
    {
    e.Handled = true;
    MessageBox.Show("Only A-Z only");
    }
    }
    }


    Dont forgot if My answer helpful to you select as best answer thats helpful to others then grow our website in software world.

    Name : Dotnet Developer-2015
    Email Id :kumaraspcode2009@gmail.com

    'Not by might nor by power, but by my Spirit,' says the LORD Almighty.

  • #762170
    Hello Aparna,

    Refer the below code :

    XAML Code :

    <Window x:Class="WpfApplication1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300" Loaded="Window_Loaded">
    <Grid>
    <TextBox Name="TextBox1" Height="25" Width="auto" BorderBrush="Black" PreviewTextInput="TextBox1_PreviewTextInput"></TextBox>
    <Button Name="Button1" Content="Support Button" Height="25" Width="100" Grid.Column="0" FlowDirection="LeftToRight" HorizontalAlignment="Right"></Button>
    </Grid>
    </Window>


    C# Code :

    using System.Text.RegularExpressions;

    private static bool IsTextAllowed(string text)
    {
    Regex regex = new Regex("^[a-zA-Z]+$"); //regex that matches disallowed text
    return regex.IsMatch(text);
    }

    private void TextBox1_PreviewTextInput(object sender, TextCompositionEventArgs e)
    {
    e.Handled = !IsTextAllowed(e.Text);
    }

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
    TextBox1.Focus();
    }


    See the attached image. It's an output i have get by using above code.

    Hope this the exact solution you are looking for.

    Regards,
    Nirav Lalan
    DNS Gold Member
    "Failure is the path of least persistence"

    Delete Attachment

  • #762171
    Hi

    To allow only capitals A to Z use below code

    private void txt_name_PreviewKeyDown(object sender, KeyEventArgs e)
    {
    if (e.keychar>='A' && e.keychar<='Z')
    e.Handled=false;
    else
    e.Handled=true;
    }

    To allow only small letters from a to z use below code

    private void txt_name_PreviewKeyDown(object sender, KeyEventArgs e)
    {
    if (e.keychar>='a' && e.keychar<='z')
    e.Handled=false;
    else
    e.Handled=true;
    }

    If you want textbox to allow A to Z and a to z use below code

    private void txt_name_PreviewKeyDown(object sender, KeyEventArgs e)
    {
    if (e.keychar>='A' && e.keychar<='Z' || e.keychar>='a' && e.keychar<='z')
    e.Handled=false;
    else
    e.Handled=true;
    }


    Regards

    Sridhar Thota.

    Sridhar Thota.
    Editor: DNS Forum.

  • #762179
    Hello Kumar,

    Let me correct you. You have mentioned "Server Side C# Code".

    As per my knowledge in WPF Windows Application Code - behind doesn't call as server side code if i am not wrong.

    Hope you don't mind.

    Regards,
    Nirav Lalan
    DNS Gold Member
    "Failure is the path of least persistence"

  • #762180
    OK NIRAV SIR

    I HAVE UPDATED THIS

    I have mention Below Code for WPF Windows Application Code -> C# Language Code

    Name : Dotnet Developer-2015
    Email Id :kumaraspcode2009@gmail.com

    'Not by might nor by power, but by my Spirit,' says the LORD Almighty.


Sign In to post your comments