当我点击关闭按钮时,控件将转到txtdealer_leave事件........我的要求是当我点击关闭按钮时,控件不应该转到txtdealer_leave事件.. [英] When I am clicking on the close button, the control is going to txtdealer_leave event........my requirement is when I clicking on close button, the control should not go to txtdealer_leave event..

查看:122
本文介绍了当我点击关闭按钮时,控件将转到txtdealer_leave事件........我的要求是当我点击关闭按钮时,控件不应该转到txtdealer_leave事件..的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

1.i有像txtdealer,txtarea等形式的文本框....最多10个文本框......带保存和关闭按钮

2.在输入数据后我显示相同的内容通过清除所有字段进行填写表格。



3.如果我点击关闭按钮,我需要通过询问是或否退出窗口来关闭表单... 。但控制权转到txtdealer_leave并显示消息请进入经销商....



我尝试了什么:



1.i have textboxes in the form like txtdealer,txtarea....upto 10 textboxes.....with save and close buttons
2.after inseting data i am showing the same form by clearing all the fields for furthur adding.

3.if i click on close button i need to close the form by asking yes or no exit window....but the control going to txtdealer_leave and showing the message please enter dealer....

What I have tried:

string close_mode="";
 private void btnClose_Click(object sender, EventArgs e)
        {
            close_mode = "close";
            {
                if (MessageBox.Show("Do you want to exit?", "Exit", MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk) == DialogResult.Yes)
                    this.Close();
            }
        }
        public void close_route(string clsmode)
        {
            close_mode = clsmode;
        }

        private void txtDealer_Leave(object sender, EventArgs e)
        {
            if(close_mode!="close")
            {
                {
                    if (txtDealer.Text == "")
                    {
                        MessageBox.Show("Please Enter dealer");
                        txtDealer.Focus();
                    }

                    if (txtDealer.Text == "")
                        txtDealer.Focus();
                }
            }
        }

推荐答案

总结一下我认为您的要求是什么:



1.无论TextBox中是否有数据,用户都应该能够关闭表格。这是完全合理的 - 您不应强迫用户在尝试离开应用程序时输入有效数据。



2.如果用户试图关闭表单那么你想提示他们确保他们真的想要离开。



3.如果输入了无效数据,那么你希望焦点保留在文本框之后你已经显示了错误信息。



所以...



首先,使用验证TextBox的事件而不是Leave事件。这是放置验证的正确位置(线索在名称:))。这比控制关闭过程(见下文)更有力量,也意味着您只需使用 e.Cancel = true; 而不是尝试重新聚焦TextBox。



接下来,确保点击关闭按钮不会导致验证。一般来说,这意味着不会调用任何Validating事件(对于任何控件) - 当你想要离开那里时,这就是你想要的。



您还需要考虑当用户只是点击表单顶部的(X)或使用Alt-F4快捷键时会发生什么。我假设你想以与点击关闭按钮相同的方式提示它们。





这是完整的解决方案:

To summarise what I think your requirements are:

1. The User should be able to close the form whether there is data in the TextBox or not. This is perfectly reasonable - you should not force the user to enter valid data when they are trying to leave the application.

2. If the User attempts to close the form then you want to prompt them to make sure they really do want to leave.

3. If invalid data is entered then you want the focus to stay in the textbox after you have displayed the error message.

So ...

Firstly, use the Validating event for the TextBox not the Leave event. It's the right place to put validation (the clue is in the name :)). This gives you much more power over controlling the close-down process (see below) and also means that you can just use e.Cancel = true; instead of attempting to refocus on the TextBox.

Next, make sure that hitting the Close button does not cause validation. In general this will mean that none of the Validating events will be called (for any control) - which is what you want when you just want to get out of there.

You also need to consider what will happen when the user just hits the (X) at the top of the form or uses the Alt-F4 shortcut. I've assumed that you want to prompt them in just the same way as if they had hit the Close button.


Here is a full solution:
private void Form1_Load(object sender, EventArgs e)
{
    btnClose.CausesValidation = false; //Or do this in the design view
}

private void btnClose_Click(object sender, EventArgs e)
{
    Close();
}

private bool QueryExit()
{
    return (MessageBox.Show("Do you want to exit?", "Exit", MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk) ==
            DialogResult.Yes);
}
private void txtDealer_Validating(object sender, System.ComponentModel.CancelEventArgs e)
{
    if (txtDealer.Text == "")
    {
        MessageBox.Show("Please Enter dealer");
        e.Cancel = true;
    }
}
protected override void WndProc(ref Message m)
{
    const int WM_CLOSE = 0x0010;
    if (m.Msg == WM_CLOSE) // Attempting to close Form
        AutoValidate = AutoValidate.Disable; //this stops (all) validations
    base.WndProc(ref m);    //call the base method to handle other messages
}

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    if (!QueryExit()) e.Cancel = true;
}



WndProc方法覆盖Windows消息处理程序,以便在用户点击(X)或Alt-F4时停止所有验证...它具有可比性设置 CausesValidation = false;



请注意,btnClose_Click不再询问用户是否要退出 - 这是因为它总是从FormClosing事件调用,我们不想显示消息两次


That WndProc method is overriding the Windows message handler to stop all validations when the user hits the (X) or Alt-F4 ... it's comparable to setting the CausesValidation = false;

Notice that the btnClose_Click no longer asks the user if they want to exit - that is because it is always going to be called from the FormClosing event and we don't want to display the message twice


这篇关于当我点击关闭按钮时,控件将转到txtdealer_leave事件........我的要求是当我点击关闭按钮时,控件不应该转到txtdealer_leave事件..的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆