2011-05-27 50 views
17

我做了这个语句来检查TextBox是否为空,但MessageBox总是显示为 如果TextBox为空或者不是。检查TextBox是否为空并返回MessageBox?

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"; 
      }*/ 
+2

请修复代码示例。注意开合花括号 – Dyppl 2011-05-27 20:33:51

回答

2

使用的东西,如下列:

if (String.IsNullOrEmpty(MaterialTextBox.Text)) 
+1

这不会处理空白 – abhilash 2011-05-27 18:57:36

+0

同样的问题不工作:( – sam 2011-05-27 19:02:21

+0

感谢男人它的作品唯一的错误是我的代码 – sam 2011-05-27 20:00:13

46

试试这个条件来代替:

if (string.IsNullOrWhiteSpace(MaterialTextBox.Text)) { 
    // Message box 
} 

这将需要一些字符串只包含空白字符的照顾,你不会必须处理字符串相等,有时可能会很棘手

+0

谢谢你的帮助,但不工作相同的问题!!!:| – sam 2011-05-27 18:59:46

+0

@ ghost_j1:请提供一些上下文,也许扩展你的代码示例,包括整个方法 – Dyppl 2011-05-27 19:01:55

+0

@ghost然后你做的是其他的错误 – lincolnk 2011-05-27 19:02:11

0

加上什么@ tjg184说,你可以不喜欢......

if (String.IsNullOrEmpty(MaterialTextBox.Text.Trim())) 

...

+0

谢谢男人它的工作唯一的错误是我的代码 – sam 2011-05-27 19:23:50

11

那么,你是清除文本框,你检查的权利之前,如果它是空的

/* !! 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(); 
} 
+0

是啊谢谢我注意到这个愚蠢的错误,我做了......但在此消息后显示datagridview插入新行,所以在那里是一种方法来删除它或可能丢弃整个插入的行或进度? – sam 2011-05-27 20:23:56

0
if (MaterialTextBox.Text.length==0) 
{ 
message 

} 
1

尝试做以下操作

if (String.IsNullOrEmpty(MaterialTextBox.Text) || String.IsNullOrWhiteSpace(MaterialTextBox.Text)) 
    { 
     //do job 
    } 
    else 
    { 
     MessageBox.Show("Please enter correct path"); 
    } 

希望我t帮助