【问题标题】:Why the new Form is close on the second time i click OK and the textBox is empty?为什么新表单在我第二次单击确定时关闭并且文本框为空?
【发布时间】:2012-09-08 18:51:06
【问题描述】:
private void button2_Click(object sender, EventArgs e)
        {
            cl = new ChangeLink();
            cl.StartPosition = FormStartPosition.CenterParent;
            DialogResult dr = cl.ShowDialog(this);
            if (dr == DialogResult.Ok)
            {
                if (cl.getText() == "")
                {
                    MessageBox.Show("The TextBox Cannot Be Empty");
                    cl.ShowDialog(this);
                    return;
                }
                else
                {
                    label4.Text = cl.getText();
                    cl.Close();
                }
            }
            else if (dr == DialogResult.Cancel)
            { 
                cl.Close();
            }

cl 是我从中获取文本的新表单。 现在我检查如果 cl.getText() 为空“”它会向用户发送一条消息,当用户在消息框上单击“确定”时,我希望它返回并再次显示新的表单对话框。 当我单击一次新表单的确定按钮并显示messaeBox消息然后再次显示新表单的对话框结果框但是当我再次单击确定并且文本框仍然为空时它不会再次显示消息框只需关闭新表单并将 label4 文本设置为空即可。

我希望每次用户单击“确定”并且文本框为空时,它将一直显示新的“表单”文本框对话框,直到用户单击“取消”或在文本框中输入内容然后单击“确定”。

【问题讨论】:

    标签: c# winforms


    【解决方案1】:

    如果您在第二个表格中进行检查,它会干净得多。您需要将代码添加到 Form2 的 OK 按钮的 Click 事件中。确保从 OK 按钮属性中删除默认的 DialogResult

    private void button2_Click(object sender, EventArgs e)
    {
        if(!string.IsNullOrEmpty(textBox1.Text))
            DialogResult = DialogResult.OK;
    }
    

    【讨论】:

      猜你喜欢
      • 2015-11-15
      • 2013-01-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-02
      • 1970-01-01
      相关资源
      最近更新 更多