本文介绍了检查文本框为空,并返回消息框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我说这句话,以检查是否文本框为空,但在MessageBox总是显示了
羯羊文本框为空或不是。
私人无效NextButton_Click(对象发件人,EventArgs五)
{
十进制MarkPoints,X,Y;
X = HoursNumericUpDown.Value;
Y = MarkNumericUpDown.Value;
MarkPoints = X * Y;
//十进制MarkPoints =(十进制)HoursNumericUpDown.Value *(十进制)HoursNumericUpDown.Value;
的DataGridViewRow dgvRow =新的DataGridViewRow();
DataGridViewTextBoxCell dgvCell =新DataGridViewTextBoxCell();
dgvCell =新DataGridViewTextBoxCell();
dgvCell.Value = MaterialTextBox.Text;
dgvRow.Cells.Add(dgvCell);
dgvCell =新DataGridViewTextBoxCell();
dgvCell.Value = HoursNumericUpDown.Value;
dgvRow.Cells.Add(dgvCell);
dgvCell =新DataGridViewTextBoxCell();
dgvCell.Value = MarkNumericUpDown.Value;
dgvRow.Cells.Add(dgvCell);
dgvCell =新DataGridViewTextBoxCell();
dgvCell.Value = MarkPoints;
dgvRow.Cells.Add(dgvCell);
dataGridView1.Rows.Add(dgvRow);
MaterialTextBox.Clear();
HoursNumericUpDown.Value = HoursNumericUpDown.Minimum;
MarkNumericUpDown.Value = MarkNumericUpDown.Minimum;
如果(String.IsNullOrEmpty(MaterialTextBox.Text))
{
MessageBox.Show(输入物料名称请,错误,MessageBoxButtons.OK,MessageBoxIcon。警告);
//dataGridView1.Rows.Clear();
}
,否则
{
/ *如果(MarkNumericUpDown.Value< 50)
{
INT指数= dataGridView1.Rows.Add();
dataGridView1.Rows [1] .Cells [4]。价值=F;
}
,否则如果(MarkNumericUpDown.Value大于50&安培;&安培; MarkNumericUpDown.Value< = 64)
{
dataGridView1.Rows [指数] .Cells [4] .value的=F;
} * /
解决方案
试试这个条件,而不是
如果(string.IsNullOrWhiteSpace(MaterialTextBox.Text)){
//消息框
}
这会照顾一些字符串只包含空格字符,你不会要处理的字符串相等有时可能会非常棘手。
I made this statement to check if TextBox is empty, but the MessageBox always shows upwether the TextBox is empty or not.
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";
}*/
解决方案
Try this condition instead:
if (string.IsNullOrWhiteSpace(MaterialTextBox.Text)) {
// Message box
}
This will take care of some strings that only contain whitespace characters and you won't have to deal with string equality which can sometimes be tricky
这篇关于检查文本框为空,并返回消息框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!