Cameron Kloot

hi, i want to make my program so when the user clicks exit a message box asking if u want to save pops up. then bye clicking save you would bring up the savefiledialog

maybe somthing like this

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
MessageBox.Show("Would you like to save your file ");
MessageBoxButtons.OkNoCancel;

}


but that dont work because it says the the second line in the curly brackets cant be used as a statement
thanks




Re: Visual C# Express Edition save and exit

jrboddie

Try this:

private void button1_Click(object sender, EventArgs e)

{

MessageBox.Show("Would you like to save your file ", "Close", MessageBoxButtons.YesNoCancel);

}





Re: Visual C# Express Edition save and exit

Cameron Kloot

great! that was it
how would i assign, for instance, the savefiledialog to the ok button






Re: Visual C# Express Edition save and exit

jrboddie

private void button1_Click(object sender, EventArgs e)

{

DialogResult result;

result = MessageBox.Show("Would you like to save your file ", "Close", MessageBoxButtons.YesNoCancel);

if(result == DialogResult.Yes)

{

if (saveFileDialog1.ShowDialog() == DialogResult.OK)

{

//Your code for saving the file

}

}

}





Re: Visual C# Express Edition save and exit

Cameron Kloot

thank you so much! how would i make the cancel button work the yes button i got and the no one doesnt need any code, but what about cancel
thanks





Re: Visual C# Express Edition save and exit

jrboddie

Like this:

private void Form1_FormClosing(object sender, FormClosingEventArgs e)

{

DialogResult result =

MessageBox.Show("Save files before quitting ", "Save Files", MessageBoxButtons.YesNoCancel);

if (result == DialogResult.Yes)

{

if (saveFileDialog1.ShowDialog() == DialogResult.OK)

{

//Your code for saving the file

}

else

{

e.Cancel = true;

}

}

else if (result == DialogResult.No)

{

}

else if (result == DialogResult.Cancel)

{

e.Cancel = true;

}

}





Re: Visual C# Express Edition save and exit

B.Lamuna

 jrboddie wrote:

Like this:

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)

        {

            DialogResult result =

            MessageBox.Show("Save files before quitting ", "Save Files", MessageBoxButtons.YesNoCancel);

            if (result == DialogResult.Yes)

            {

                if (saveFileDialog1.ShowDialog() == DialogResult.OK)

                {

 

                    //Your code for saving the file

 

                }

 

                else

                {

                    e.Cancel = true;

                }

 

            }

            else if (result == DialogResult.No)

            {

 

            }

            else if (result == DialogResult.Cancel)

            {

                e.Cancel = true;

            }

        }

 

[Before We Start]

I want to tell you that my english is not good enough ^_^

[Content.....]

Hi, I think that is a good job. but i have another question need your help

My Code is :

=======================

        private void FrmMain_FormClosing( object sender, FormClosingEventArgs e)
        {
            String qsTXT = "Are you sure ";
            String qsCap = "Exit ";
            DialogResult myResult;
            myResult = MessageBox.Show(qsTXT, qsCap, MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
            if (myResult == DialogResult.OK)
            {
                Application.Exit();
            }
            else
            {
                e.Cancel = false;
            }
        }

=======================

I can make sure that my code is right. but the problem is

the messagebox will be shown twice...

I think the main reason is the event

but while i put them into formClose event, complier said e have no this alue: cancel

What should I do






Re: Visual C# Express Edition save and exit

Cameron Kloot

thanks so much J. perfect answer once again. i also want my box only to appear if the file has not ben saved. how would i do that






Re: Visual C# Express Edition save and exit

jrboddie

B. Lamuna:

1. You don't need the Application.Exit(); line. The form is already in the process of closing. When you execute .Exit() in your code, it tries to close again and recalls your event.

2. The line e.Cancel = false; should be e.Cancel = true; because if you click 'Cancel' in your messageBox, you want to cancel the event.







Re: Visual C# Express Edition save and exit

jrboddie

Cameron,

You will need to declare a boolean variable to keep track of when you have changed data that should be saved in the file before closing.

Something like:

bool dataChanged;

Then anytime you make a change to your data, include this:

dataChanged = true;

When you execute the code to save your file, include this:

dataChanged = false;

Then in your formClosing event, surround your messageBox/dialog code with a test of this value. Something like this:

private void Form1_FormClosing(object sender, FormClosingEventArgs e)

{

if (dataChanged)

{

//Put your form closing dialog stuff here

}

}

So, if the dataChanged variable is true, it means that you have made changes and you should be asked the question before closing. If the dataChanged variable is false, it means that no changes have been made (since saving the file) and that it is ok to close without the dialog.





Re: Visual C# Express Edition save and exit

Cameron Kloot

i get what to do except for: how to detect whether the file has ben changed. thanks





Re: Visual C# Express Edition save and exit

jrboddie

Cameron,

It is not to detect whether the file has been changed but if your program has changed the data that you are going to write to file since the last time you saved it.

For example, suppose your application has data in a textBox that you can modify but when you are finished, you want to save the contents of the textBox in a file.

Then whenever the text in the textBox changes, you want to set dataChanged to true.  When you execute the save command, the textBox will be saved and you want to set dataChanged to false.  That way, if you save the data and make no more changes to it, you will not be bothered by the dialog when you close.  In this example, we could detect a change in the textBox with a TextChanged event.

Your application will be different.  Suppose your application saves results in an array and you want to save the array in a file.  Then you would set dataChanged to true anytime your program writes to the array.

What are you saving in your file

 





Re: Visual C# Express Edition save and exit

B.Lamuna

jrboddie wrote:

B. Lamuna:

1. You don't need the Application.Exit(); line. The form is already in the process of closing. When you execute .Exit() in your code, it tries to close again and recalls your event.

2. The line e.Cancel = false; should be e.Cancel = true; because if you click 'Cancel' in your messageBox, you want to cancel the event.

I wish to thank you for your kindly help, so that I can make my program more perfect.