Hi, I am writting an extended TextBox (class) to handle all validations like Numeric, AlphaNumeric, allow special chars...and I am going to use this in all my .xaml files through ot my application.
In my UI, I hv 4 TextBoxes. In that first TextBox is Numeric with Range 1 to 1000. so I hv written validation for in ExtendedTextBox on LostFocus.
Whenever user tries to enter more than 1000 or less than 1, error msg is shown. but aft that the cursor is moving to next control(TextBox). but I want the cursor to be focused to the first textbox only, where the validation was done just now.
And when I trying to set txtBox.Focus(), this is going into infinite loop and error msg pop-up is coming....always.
How I can achive the above functionality, such that when user enters wrong values, need to show the pop up msg and focus to the same contols. Until and unless user enters correct value, the focus remains in the same textbox.
Here is the code I hv written: Please suggect if I am missing anything or Is there any other solutions to achive this....Thanks.
using System; using System.ComponentModel; using System.Net; using System.Text; using System.Text.RegularExpressions; using System.Windows; using System.Windows.Controls; using System.Windows.Input; using System.Linq; namespace SampleApplication{ public class ExtndTextBox : TextBox, INotifyPropertyChanged { #region Constructor public ExtndTextBox() { LostFocus += ExtndTextBox_LostFocus; } public void ExtndTextBox_LostFocus(object sender, RoutedEventArgs e) { ExtndTextBox senderText = sender as ExtndTextBox; if (TextBoxType == ExtTextBoxType.NumericWithRange) { if (base.Text == "0" || base.Text == "1") { MessageBoxResult results = MessageBox.Show("System number must be greater than 1","Hello",MessageBoxButton.OK, MessageBoxImage.Warning,MessageBoxResult.Yes); if(results == MessageBoxResult.OK) { senderText.Focus(); } } } else if (TextBoxType == ExtTextBoxType.MinAndMaxValue) { int tempvalue = Convert.ToInt32(base.Text); if (tempvalue < 1 || tempvalue > 1000) { MessageBoxResult results = MessageBox.Show("Number Of Systems min value is 1 and max value is 1000.", "Hello", MessageBoxButton.OK, MessageBoxImage.Warning, MessageBoxResult.Yes); if (results == MessageBoxResult.OK) { senderText.Focus(); } } } } } #endregion public static readonly DependencyProperty TextBoxTypeProperty = DependencyProperty.Register("TextBoxType", typeof(ExtTextBoxType), typeof(ExtndTextBox), new PropertyMetadata(null)); public ExtTextBoxType TextBoxType { get { return (ExtTextBoxType)GetValue(TextBoxTypeProperty); } set { base.SetValue(TextBoxTypeProperty, value); } } private bool IsNumberKey(Key inKey) { if (inKey < Key.D0 || inKey > Key.D9) { if (inKey < Key.NumPad0 || inKey > Key.NumPad9) { return false; } } return true; } private bool IsActionKey(Key inKey) { return inKey == Key.Delete || inKey == Key.Back || inKey == Key.Tab || inKey == Key.Return || Keyboard.Modifiers.HasFlag(ModifierKeys.Alt); } protected void OnKeyDown(object sender, KeyEventArgs e) { if (TextBoxType == ExtTextBoxType.Numeric) { e.Handled = !IsNumberKey(e.Key) && !IsActionKey(e.Key); } } #region INPC public event PropertyChangedEventHandler PropertyChanged; public void OnPropertyChange(string name) { if (this.PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(name)); } #endregion public enum ExtTextBoxType { Numeric, NumericWithRange, MinAndMaxValue, } }