Click here to Skip to main content
15,896,557 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

C#
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();
                }
            }
        }
Posted
Updated 22-Apr-16 2:06am
v3
Comments
CHill60 22-Apr-16 6:47am    
Does my Tip - Allow Form to close when invalid data is present[^] - help with this?
ZurdoDev 22-Apr-16 7:10am    
That happens when the input focus leaves the control. So, click the close button from a different control.
Karthik_Mahalingam 22-Apr-16 7:12am    
because the textbox value is empty. try to enter some value and close it.

1 solution

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:
C#
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;
}

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
 
Share this answer
 
v2
Comments
Karthik_Mahalingam 22-Apr-16 10:00am    
5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900