public class MyTextBox: TextBox
{
public MyTextBox()
{
this.PreviewTextInput += new TextCompositionEventHandler(TextBox_PreviewTextInput);
this.AddHandler(DataObject.PastingEvent, new DataObjectPastingEventHandler(OnPaste));
}
private void OnPaste(object sender, DataObjectPastingEventArgs e)
{
if (e.DataObject.GetDataPresent(typeof(String)))
{
String text = (String)e.DataObject.GetData(typeof(String));
if (!IsTextAllowed(text))
{
e.CancelCommand();
}
}
else
{
e.CancelCommand();
}
}
void TextBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
{
e.Handled = !IsTextAllowed(e.Text);
}
private static bool IsTextAllowed(string text)
{
Regex regex = new Regex("^[a-zA-Z]+$"); //regex that matches disallowed text
return regex.IsMatch(text);
}
}
的的StringFormat使用customTextBox像上面
我們需要知道你打算使用'StringFormat'什麼。 –
聽起來像你想正則表達式不'StringFormat' –
我如何使用正則表達式爲此,我真的堅持在這... – user1050667