我现在在一个MDI父窗体frmParent中打开一个MDI子窗体frmSon,这个frmSon上有一个按钮btnClose负责关闭frmSon同时frmSon上还有一个textbox名称为txtbxNametxtbxName的CausesValidation为默认值true我现在在txtbxName中输入一个内容后,在焦点离开的时候会自动触发txtbxName的Validating事件,如果输入内容不合格会报一个错误信息但现在的问题是,我在txtbxName中直接输入一个不合格内容后,我直接关闭窗体,可以通过下面三种方式来关闭frmSon第一种方式:点击frmSon右上角的关闭按钮
第二种方式:点击frmSon上的关闭按钮btnClose
第三种方式:点击frmParent上的右上角关闭按钮我下午通过询问已经可以通过override frmSon的WndProc事件捕获到frmSon右上角的关闭按钮点击事件,实现了通过第一种方式关闭窗体frmSon的时候不执行txtbxName的Validating事件但现在通过第二种和第三种方式却还没有实现,通过这两种方式关闭frmSon的时候还是会报告错误输入内容的提示框请问哪位能帮我解决这个问题?分数不多,有分就散!

解决方案 »

  1.   

    我怎么可以捕获到frmSon上特定按钮btnClose的单击消息以及frmSon的父frmParent的WM_CLOSE消息并在捕获到以后通过设置txtbxName的tag属性操作txtbxName的Validating事件的具体是否执行?
      

  2.   

    Form1为主窗体,关闭消息照样截获:
    public Form2 form2;private void Form1_Shown(object sender, EventArgs e)
    {
        form2 = new Form2();
        form2.MdiParent = this;
        form2.Show();
    }protected override void WndProc(ref Message m)
    {
        const int WM_CLOSE = 0x0010;
        switch (m.Msg)
        {
            case WM_CLOSE:
                form2.textBox1.CausesValidation = false;
                break;
        }
        base.WndProc(ref m);
    }
    捕获按钮的按下消息:
    private void button1_Click(object sender, EventArgs e)
    {
        Close();
    }private void textBox1_Validating(object sender, CancelEventArgs e)
    {
        e.Cancel = string.IsNullOrEmpty(((TextBox)sender).Text);
    }private void Form2_FormClosing(object sender, FormClosingEventArgs e)
    {
        textBox1.CausesValidation = true;
    }
    public class SubWindow : NativeWindow
    {
        public TextBox textBox;
        protected override void WndProc(ref Message m)
        {
            const int WM_LBUTTONDOWN = 0x0201;
            switch (m.Msg)
            {
                case WM_LBUTTONDOWN:
                    textBox.CausesValidation = false;
                    break;
            }
            base.WndProc(ref m);
        }    public SubWindow(TextBox textBox)
        {
            this.textBox = textBox;
        }
    }
    protected override void WndProc(ref Message m)
    {
        const int WM_CLOSE = 0x0010;
        switch (m.Msg)
        {
            case WM_CLOSE:
                textBox1.CausesValidation = false;
                break;
        }
        base.WndProc(ref m);
    }protected override void OnEnter(EventArgs e)
    {
        Console.WriteLine("OnEnter");    
        base.OnEnter(e);
    }private void Form2_Load(object sender, EventArgs e)
    {
        SubWindow vSubWindow = new SubWindow(textBox1);    vSubWindow.AssignHandle(button1.Handle);
    }