如何解决检查TextBox是否为空并返回MessageBox?
| 我发出此语句来检查TextBox是否为空,但MessageBox始终显示 TextBox是否为空。 private void NextButton_Click(object sender,EventArgs e)
{
decimal MarkPoints,x,y;
x = HoursNumericUpDown.Value;
y = MarkNumericUpDown.Value;
MarkPoints = x * y;
//decimal MarkPoints = (decimal)HoursNumericUpDown.Value * (decimal)HoursNumericUpDown.Value;
DataGridViewRow dgvRow = new DataGridViewRow();
DataGridViewTextBoxCell dgvCell = new DataGridViewTextBoxCell();
dgvCell = new DataGridViewTextBoxCell();
dgvCell.Value = MaterialTextBox.Text;
dgvRow.Cells.Add(dgvCell);
dgvCell = new DataGridViewTextBoxCell();
dgvCell.Value = HoursNumericUpDown.Value;
dgvRow.Cells.Add(dgvCell);
dgvCell = new DataGridViewTextBoxCell();
dgvCell.Value = MarkNumericUpDown.Value;
dgvRow.Cells.Add(dgvCell);
dgvCell = new DataGridViewTextBoxCell();
dgvCell.Value = MarkPoints;
dgvRow.Cells.Add(dgvCell);
dataGridView1.Rows.Add(dgvRow);
MaterialTextBox.Clear();
HoursNumericUpDown.Value = HoursNumericUpDown.Minimum;
MarkNumericUpDown.Value = MarkNumericUpDown.Minimum;
if (String.IsNullOrEmpty(MaterialTextBox.Text))
{
MessageBox.Show(\"Enter Material Name Please.\",\"Error\",MessageBoxButtons.OK,MessageBoxIcon.Warning);
//dataGridView1.Rows.Clear();
}
else
{
/*if (MarkNumericUpDown.Value < 50)
{
int index = dataGridView1.Rows.Add();
dataGridView1.Rows[1].Cells[4].Value = \"F\";
}
else if (MarkNumericUpDown.Value > 50 && MarkNumericUpDown.Value <= 64)
{
dataGridView1.Rows[index].Cells[4].Value = \"F\";
}*/
解决方法
请尝试以下条件:
if (string.IsNullOrWhiteSpace(MaterialTextBox.Text)) {
// Message box
}
这将处理一些仅包含空格字符的字符串,并且您不必处理有时很棘手的字符串相等性
, 好吧,您正在检查文本框是否为空之前清除文本框
/* !! This clears the textbox BEFORE you check if it\'s empty */
MaterialTextBox.Clear();
HoursNumericUpDown.Value = HoursNumericUpDown.Minimum;
MarkNumericUpDown.Value = MarkNumericUpDown.Minimum;
if (String.IsNullOrEmpty(MaterialTextBox.Text))
{
MessageBox.Show(\"Enter Material Name Please.\",\"Error\",MessageBoxButtons.OK,MessageBoxIcon.Warning);
//dataGridView1.Rows.Clear();
}
, 使用如下内容:
if (String.IsNullOrEmpty(MaterialTextBox.Text))
, 尝试执行以下操作
if (String.IsNullOrEmpty(MaterialTextBox.Text) || String.IsNullOrWhiteSpace(MaterialTextBox.Text))
{
//do job
}
else
{
MessageBox.Show(\"Please enter correct path\");
}
希望能帮助到你
, 加上@ tjg184所说的内容,您可以做类似...
if (String.IsNullOrEmpty(MaterialTextBox.Text.Trim()))
...
, if (MaterialTextBox.Text.length==0)
{
message
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。