积极答复者
请教:多个textBox是否已经输入的判断问题

问题
-
请教:多个textBox是否已经输入的判断问题
背景:3个textBox, 1个button;
需求是:
1.textBox1 有输入,则button执行A语句;
2.textBox1,textBox2有输入,则button执行B语句;
3.textBox1,textBox2,textBox3有输入,则button执行C语句;
但以下代码始终执行的是else , 麻烦各位了:if ((textBox1.Text.Trim().Length > 0) && (textBox2.Text.Trim().Length == 0) && (textBox3.Text.Trim().Length == 0))
{
label11.Text = "textBox1有输入";
}
if ((textBox1.Text.Trim().Length > 0) && (textBox2.Text.Trim().Length > 0) && (textBox3.Text.Trim().Length == 0))
{
label11.Text = "textBox1,textBox2有输入";
}
if ((textBox1.Text.Trim().Length > 0) && (textBox2.Text.Trim().Length > 0) && (textBox3.Text.Trim().Length > 0))
{
label11.Text = "textBox1,textBox2,textBox3有输入";
}
else
{
label11.Text = "例外";
}
答案
-
dear
使用else if,依你的需求我理解了以下的代碼,若我所理解的與你的需求有出入,請自行修改
private void button1_Click(object sender, EventArgs e) { var result = string.Empty; if (!string.IsNullOrEmpty(this.textBox1.Text) && !string.IsNullOrEmpty(this.textBox2.Text) && !string.IsNullOrEmpty(this.textBox3.Text)) { result = "textBox1,textBox2,textBox3 有输入"; } else if (!string.IsNullOrEmpty(this.textBox1.Text) && !string.IsNullOrEmpty(this.textBox2.Text)) { result = "textBox1,textBox2 有输入"; } else if (!string.IsNullOrEmpty(this.textBox1.Text)) { result = "textBox1 有输入"; } if (!string.IsNullOrEmpty(result)) { MessageBox.Show(result); } }
另外,這樣的方式很難以維護,建議使用责任链模式来重构
秘訣無它,唯勤而已 http://www.dotblogs.com.tw/yc421206/
- 已建议为答案 Jason Dot WangModerator 2012年11月28日 7:51
- 已标记为答案 Jason Dot WangModerator 2012年12月6日 2:29
全部回复
-
dear
使用else if,依你的需求我理解了以下的代碼,若我所理解的與你的需求有出入,請自行修改
private void button1_Click(object sender, EventArgs e) { var result = string.Empty; if (!string.IsNullOrEmpty(this.textBox1.Text) && !string.IsNullOrEmpty(this.textBox2.Text) && !string.IsNullOrEmpty(this.textBox3.Text)) { result = "textBox1,textBox2,textBox3 有输入"; } else if (!string.IsNullOrEmpty(this.textBox1.Text) && !string.IsNullOrEmpty(this.textBox2.Text)) { result = "textBox1,textBox2 有输入"; } else if (!string.IsNullOrEmpty(this.textBox1.Text)) { result = "textBox1 有输入"; } if (!string.IsNullOrEmpty(result)) { MessageBox.Show(result); } }
另外,這樣的方式很難以維護,建議使用责任链模式来重构
秘訣無它,唯勤而已 http://www.dotblogs.com.tw/yc421206/
- 已建议为答案 Jason Dot WangModerator 2012年11月28日 7:51
- 已标记为答案 Jason Dot WangModerator 2012年12月6日 2:29
-
protected void btnSubmit_Click(object sender, EventArgs e) { //输入空格无效 string str1 = TextBox1.Text.Trim(); string str2 = TextBox2.Text.Trim(); string str3 = TextBox3.Text.Trim(); if (!string.IsNullOrEmpty(str1)&&string.IsNullOrEmpty(str2)&&string.IsNullOrEmpty(str3)) { Label1.Text = "TextBox1 has value!"; } else if (!string.IsNullOrEmpty(str1)&&!string.IsNullOrEmpty(str2)&&string.IsNullOrEmpty(str3)) { Label1.Text = "TextBox1 and TextBox2 has value!"; } else if (!string.IsNullOrEmpty(str1) && !string.IsNullOrEmpty(str2) && !string.IsNullOrEmpty(str3)) { Label1.Text = "TextBox1,TextBox2 and TextBox3 both has value!"; } else { Label1.Text = "input nothing!"; } //TextBox4输入空格时执行下面语句 if (TextBox4.Text.Length>0) { Label1.Text += "TextBox4 has input!"; } }