Handle the TextBox.KeyPress event:
private void myTextBox_KeyPress(object sender, KeyPressEventArgs e)
{
if (!((e.KeyChar >= 'a' && e.KeyChar <= 'z') || e.KeyChar == '-'))
{
e.Handled = true;
}
}
"i make this function can u please tell me what i have to use for "-" becouse seperator is not working"
private string RemoveNonAlphabets(string str)
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < str.Length; i++)
{
if(char.IsLetter(str[i]) || char.IsSeparator(str[i]))
sb.Append(str[i]);
} return sb.ToString();
}
To meet your original question objectives, use IsLower rather than IsLetter - the latter allows 'A'..'Z' as well as 'a'..'z'
Don't use IsSeparator - it checks for:
The Unicode standard recognizes three subcategories of separators:
Space separators (the UnicodeCategory.SpaceSeparator category), which includes characters such as \u0020.
Line separators (the UnicodeCategory.LineSeparator category), which includes \u2028.
Paragraph separators (the UnicodeCategory.ParagraphSeparator category), which includes \u2029.
(Taken from MSDN:
http://msdn.microsoft.com/en-us/library/cta536cf.aspx[
^])
Use '-' as in my example above.