जैसा कि एक टिप्पणी में उल्लिखित है (और जैसा कि मैंने टाइप किया है) और आपको टेक्स्ट बॉक्स पर कीडाउन या कीप्रेस ईवेंट को पकड़ने के लिए एक ईवेंट हैंडलर पंजीकृत करने की आवश्यकता है। इसका कारण यह है TextChanged केवल निकाल दिया जाता है जब पाठ बॉक्स ध्यान केंद्रित
नीचे regex आप उन अक्षरों को आप
Regex regex = new Regex(@"[0-9+\-\/\*\(\)]");
MatchCollection matches = regex.Matches(textValue);
अनुमति देना चाहते हैं मिलाने की सुविधा देती खो देता है और इस विपरीत है और वर्ण की अनुमति नहीं है पकड़ता
Regex regex = new Regex(@"[^0-9^+^\-^\/^\*^\(^\)]");
MatchCollection matches = regex.Matches(textValue);
मुझे लगता है कि कोई भी मैच नहीं होगा क्योंकि कोई टेक्स्ट टेक्स्ट में टेक्स्ट पेस्ट कर सकता है। ऐसी स्थिति में पकड़
textBox1.TextChanged += new TextChangedEventHandler(textBox1_TextChanged);
private void textBox1_TextChanged(object sender, EventArgs e)
{
Regex regex = new Regex(@"[^0-9^+^\-^\/^\*^\(^\)]");
MatchCollection matches = regex.Matches(textBox1.Text);
if (matches.Count > 0) {
//tell the user
}
}
textchanged और एकल कुंजी दबाव
textBox1.KeyPress += new KeyPressEventHandler(textBox1_KeyPress);
private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
// Check for a naughty character in the KeyDown event.
if (System.Text.RegularExpressions.Regex.IsMatch(e.KeyChar.ToString(), @"[^0-9^+^\-^\/^\*^\(^\)]"))
{
// Stop the character from being entered into the control since it is illegal.
e.Handled = true;
}
}
स्रोत
2012-09-26 17:32:15
ASP.NET? WinForms? WPF? ये सभी सी # का उपयोग कर सकते हैं, और सभी अलग हैं। – David
यह एक विंडोज़ फॉर्म एप्लीकेशन है। –
क्या आपने PropertyChanged ईवेंट के लिए ईवेंट हैंडलर बनाने का प्रयास किया है, यदि यह अमान्य है तो चरित्र को हटा देता है? –