Last active
August 24, 2020 08:37
-
-
Save heanzyzabala/8a0fb8b52b714ced1d52abb3c8d9113d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class TextBoxWithMessage | |
{ | |
private TextBox textbox; | |
private String message; | |
public TextBoxWithError(TextBox textbox, String message) { | |
this.textbox = textbox; | |
this.message = message; | |
} | |
public TextBox getTextBox() { | |
return textbox; | |
} | |
public String getMessage() { | |
return message; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
TextInputValidator v = new TextInputValidator(); | |
Set the rules for the type of input for the textbox that is handled by the keypressed event: | |
v.forInt(myIntTextBox,"Textbox 'myIntTextBox' should not be empty"); | |
v.forString(myStringTextBox,"Textbox 'myStringTextBox' should not be empty"); | |
v.forStringInt(myStringIntTextBox,"Textbox 'myStringIntTextBox' should not be empty"); | |
When submitting the values use: | |
v.onSubmitPassed(); | |
*/ | |
public class TextInputValidator | |
{ | |
private List<TextBoxWithMessage> textboxList = new List<TextBoxWithMessage>(); | |
public TextInputValidator() {} | |
public void forInt(TextBoxWithMessage textbox) { | |
if(textbox.getTextBox() != null) { | |
textboxList.Add(textbox); | |
textbox.getTextBox().KeyPress += new KeyPressEventHandler(forIntKeyPressed); | |
} | |
} | |
public void forString(TextBoxWithMessage textbox) { | |
if(textbox.getTextBox() != null) { | |
textboxList.Add(textbox); | |
textbox.getTextBox().KeyPress += new KeyPressEventHandler(forStringKeyPressed); | |
} | |
} | |
public void forStringInt(TextBoxWithMessage textbox) { | |
if(textbox.getTextBox() != null) { | |
textboxList.Add(textbox); | |
textbox.getTextBox().KeyPress += new KeyPressEventHandler(forStringIntKeyPressed); | |
} | |
} | |
public void forIntKeyPressed(Object o, KeyPressEventArgs e) { | |
if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar)) { | |
e.Handled = true; | |
} | |
} | |
public void forStringKeyPressed(Object o, KeyPressEventArgs e) { | |
if (!char.IsControl(e.KeyChar) && !char.IsLetter(e.KeyChar)) { | |
e.Handled = true; | |
} | |
} | |
public void forStringIntKeyPressed(Object o, KeyPressEventArgs e) { | |
if (!char.IsControl(e.KeyChar) && !char.IsLetter(e.KeyChar) && !char.IsDigit(e.KeyChar)) { | |
e.Handled = true; | |
} | |
} | |
public boolean onSubmitPassed() { | |
foreach(TextBoxWithMessage textbox in textboxList) { | |
if(string.IsNullOrEmpty(textbox.getTextBox().Text)) { | |
MessageBox.Show(textbox.getMessage(), "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error); | |
return false; | |
} | |
} | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment