日期:2014-05-18  浏览次数:20834 次

Button的DialogResult.OK问题?
C# code

如果将button的DialogResult设为OK,验证失败后,它会直接关闭窗口,这时,我们要求在验证失败后重新输入,而不是关闭窗口,请问怎么做?

主窗口:
  #region
            //if (RValue == "")
            //{
            //    MessageBox.Show(this, "请选择需要修改的数据!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            //    return;
            //}
            Login.FLAGID = "10";
            NewClientAdd editor = new NewClientAdd();
            editor.ShowDialog();
            if (editor.DialogResult == DialogResult.OK)
            {
                DataGridViewBind2();
            }
            #endregion


子窗口:

      
Button的DialogResult.OK






------解决方案--------------------
写了个小程序 :)
在TestForm的输入框中输入123时才会返回OK。

C# code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TestDialog
{
    class Program
    {
        static void Main(string[] args)
        {
            Application.Run(new MainForm());
        }
    }

    class MainForm : Form
    {
        Button cmdTest;

        public MainForm()
        {
            this.SetBounds(0, 0, 300, 400);
            this.StartPosition = FormStartPosition.CenterScreen;
            this.Text = "Main Form";

            cmdTest = new Button();
            cmdTest.SetBounds(100, 180, 100, 40);
            cmdTest.Text = "Test";
            cmdTest.Click += (o, e) =>
            {
                TestForm frm = new TestForm();
                frm.ShowDialog();

                MessageBox.Show("DialogResult: " + frm.DialogResult);
            };

            this.Controls.Add(cmdTest);
        }
    }

    class TestForm : Form
    {
        Button cmdTest;
        TextBox txtTest;

        public TestForm()
        {
            this.SetBounds(0, 0, 200, 200);
            this.StartPosition = FormStartPosition.CenterParent;
            this.Text = "Test Form";

            txtTest = new TextBox();
            txtTest.SetBounds(50, 50, 100, 30);

            cmdTest = new Button();
            cmdTest.SetBounds(70, 100, 60, 40);
            cmdTest.Text = "Test";
            cmdTest.Click += (o, e) =>
            {
                if (txtTest.Text == "123") this.DialogResult = System.Windows.Forms.DialogResult.OK;
            };

            this.Controls.Add(cmdTest);
            this.Controls.Add(txtTest);
        }
    }
}