Answered by:
Boolean method

Question
-
I'm trying to write a Boolean method to perform validations. For example....
public bool validation(textbox textbox1, textbox textbox2, textbox textbox3) { bool result = true; textbox[] txtFields = new textbox[3] { textbox1, textbox2, textbox3 }; string[] strFields = new string[3] { textbox1.Text, textbox2.Text, textbox3.Text }; for (var i = 0; i <= txtFields.Count && i <= strFields.Length; i++) { if ((string)IsStringEmpty(strFields[i])) { txtFields[i].background = Color.Red; result = false;
} else { txtFields[i].background = Color.White; } } return result; }Then i would like to wright some code that say's if that method return true continue and if not stop code.Im thinking it would look like this.
if (validation()) { //// do something }
- Edited by uknowmeim Thursday, September 15, 2011 3:18 PM
Answers
-
If you only wanna do some checking if controls are empty (no text inside), and setting background color, then you can do something like this:
private void button2_Click(object sender, EventArgs e) { TextBox[] tbs = { textBox1, textBox2, textBox3 }; bool bChecking = TextBoxesValidation(tbs); if (!bChecking) MessageBox.Show("Some of the textBoxes are empty. Fil red textBoxes before continuing."); else { //all ok.. } } private bool TextBoxesValidation(TextBox[] tbs) { bool result = true; for (int i = 0; i < tbs.Length; i++) { if (String.IsNullOrEmpty(tbs[i].Text)) { tbs[i].BackColor = Color.Red; result = false; } else tbs[i].BackColor = Color.White; } return result; }
Mitja
All replies
-
-
If you only wanna do some checking if controls are empty (no text inside), and setting background color, then you can do something like this:
private void button2_Click(object sender, EventArgs e) { TextBox[] tbs = { textBox1, textBox2, textBox3 }; bool bChecking = TextBoxesValidation(tbs); if (!bChecking) MessageBox.Show("Some of the textBoxes are empty. Fil red textBoxes before continuing."); else { //all ok.. } } private bool TextBoxesValidation(TextBox[] tbs) { bool result = true; for (int i = 0; i < tbs.Length; i++) { if (String.IsNullOrEmpty(tbs[i].Text)) { tbs[i].BackColor = Color.Red; result = false; } else tbs[i].BackColor = Color.White; } return result; }
Mitja -
I think your code is ok.
Hasibul Haque,MCC,MCPD blog.e-rains.com- Proposed as answer by Link.fr Thursday, September 15, 2011 4:45 PM
-
-
- Proposed as answer by Link.fr Thursday, September 15, 2011 4:45 PM
-
Thank you all for the support and encouragement and thank you Mitja Bonca for providing a simpler way to write it.