I'm trying to restrict the first character other than English alphabet, but I'm still able to enter whitespace as first character which is not what I want. How can I prevent the first character from beeing a whitespace during the typing?
private void TbxCode_PreviewTextInput(object sender, TextCompositionEventArgs e)
{
System.Text.RegularExpressions.Regex myRegex = new System.Text.RegularExpressions.Regex(@"^[a-zA-Z]$");
if (tbxCode.Text.Length == 0)
{
if (!myRegex.IsMatch(e.Text))
{
e.Handled = true;
}
}
}
<Window x:Class="WpfApp"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:cgmsui="clr-namespace:ClassGetMSUI;assembly=ClassGetMSUI">
<Grid>
<TextBox Name="tbxCode"
Width="100"
Height="20"
PreviewTextInput="TbxCodeHoraire_PreviewTextInput" />
</Grid>
</Window>