Check if TextBox is empty and return MessageBox?(检查TextBox是否为空并返回MessageBox?)
问题描述
我做这个声明是为了检查 TextBox 是否为空,但 MessageBox 总是出现TextBox 是否为空.
I made this statement to check if TextBox is empty, but the MessageBox always shows up wether 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";
}*/
推荐答案
试试这个条件:
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
这篇关于检查TextBox是否为空并返回MessageBox?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:检查TextBox是否为空并返回MessageBox?
基础教程推荐
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- SSE 浮点算术是否可重现? 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- 将 XML 转换为通用列表 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01