c# - 检查TextBox是否为空并返回MessageBox?

标签 c# winforms textbox

我做了这个声明来检查 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
}

这将处理一些只包含空白字符的字符串,您将不必处理有时可能很棘手的字符串相等性

关于c# - 检查TextBox是否为空并返回MessageBox?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6156458/

相关文章:

ASP.NET:TextBox.Text 没有更新的值

c# - 更改 MessageBox 按钮文本的语言

css - IE 中的文本区域回退

winforms - 你如何固定 winforms 标签的高度和宽度以接受可变长度的文本

c# - WPF 应用程序渲染不佳

.net - 单击菜单项时验证 DataGridView

c# - 简单的多线程问题

C# 代码分析 - 表达式的结构

c# - 如何检查是否已过期

c# - 并非所有路径都返回值