NekoManu Posted October 20, 2004 Posted October 20, 2004 I want to create a user control, based on a textbox, that validates the input. I have tried it again and again but I don't know how to do this. This is what I have come up with, but if there are other and better ways to do this, please let me know. namespace soControls { public partial class MNLTextBox : TextBox { private string _OldValue; private string _ErrorMessage1 = ""; private string _ErrorMessage2 = ""; private ErrorProvider TextBoxErrorProvider; public MNLTextBox() { InitializeComponent(); this.Validating += new CancelEventHandler(MNLTextBox_Validating); this.Validated += new EventHandler(MNLTextBox_Validated); TextBoxErrorProvider = new ErrorProvider(); } private void MNLTextBox_Validating(object sender, CancelEventArgs e) { if (this.Text != _OldValue) { if (ValidTextBox()) { _OldValue = this.Text; if (((MNLForm)MNLForm.ActiveForm).DBStatus == soSystem.cUnChanged) ((MNLForm)MNLForm.ActiveForm).DBStatus = soSystem.cEdit; } else { e.Cancel = true; this.Select(0, this.Text.Length); this.TextBoxErrorProvider.SetIconAlignment(this, ErrorIconAlignment.MiddleLeft); this.TextBoxErrorProvider.SetError(this, (_ErrorMessage1.Length > 0 ? _ErrorMessage1 : "Unidentified Error") + (_ErrorMessage2.Length > 0 ? "\n" + _ErrorMessage2 : "")); } } } private void MNLTextBox_Validated(object sender, EventArgs e) { this.TextBoxErrorProvider.SetError(this, ""); } public virtual Boolean ValidTextBox() { return true; } public string OldValue { get { return _OldValue; } set { _OldValue = value; } } public string ErrorMessage1 { get { return _ErrorMessage1; } set { _ErrorMessage1 = value; } } public string ErrorMessage2 { get { return _ErrorMessage2; } set { _ErrorMessage2 = value; } } } } My big problem is the ValidTextBox() method. When I use this control on a form, I want to write a new method that does the actual validation for that specific control (e.g. checking if a field is empty). I tried with override, but that does NOT seem to work, because I get the error that there is no such function to override. Quote
Administrators PlausiblyDamp Posted October 20, 2004 Administrators Posted October 20, 2004 Do you want other controls to inherit from this and provide their own ValidTextBox method or do you wish the validation code to be provided by the form that the control is placed on? Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
NekoManu Posted October 21, 2004 Author Posted October 21, 2004 I not sure I understand the question, but I'll try to explain what I want to do. I want to write the test in the form, because they are different for each time you use the control. Like testing if a textbox is empty or testing if it is a valid e-mail address, that sort of stuff. I need the ValidTextBox() method in the control, because otherwise I can't do the test there. Like I said before, if there is a better way to do this, please let me know. Quote
Administrators PlausiblyDamp Posted October 21, 2004 Administrators Posted October 21, 2004 Rather than it being a method of the class you probably want to raise an event to allow the calling form the chance to execute code and / or cancel the update. Could you not just use the existing Validating event of the text box to do the validation logic? Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
NekoManu Posted October 21, 2004 Author Posted October 21, 2004 Could you not just use the existing Validating event of the text box to do the validation logic? I could do that, but than I have to write the same code in each textbox. Or am I wrong with that? Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.