ofir zakay
ofir zakay

Reputation: 29

Validating user's input before continuing program execution

I have created a program that contains (for now) 2 forms.

In the first form I am asking the user for a file. After the user has selected a file, another form is called followed by closing of the current form.

An if statement indicates if the user has inserted a file when the Open File button is pressed, and if not, the second form will not be loaded.

The problem is that if the user clicks the Close button on the first form (the current one), the form closes and the next one is called.

The options of the next form are based on the user's input in the first form (where the user is asked to select a file), so if the second form is called when the user cancels the first form, it will create problems for methods in the second form.

Any ideas about how to handle the Close Button?

Upvotes: 2

Views: 149

Answers (5)

devavx
devavx

Reputation: 1045

I am assuming that you have an OpenfileDialog (to allow the user to select a file),and a button probably named Open File to pass the filename to next form.If this is the case,then you can try to to disable the open button if no file has been selected.

Consider the code below as the function where all logic takes place;

private void BrowseFile()
{
//dlgopenfile is the name of Openfiledialog that allows the user to browse for a file.
//string filename is the name of selected file if any.
//Form2 is the next form.

try
{

switch (dlgopenfile.ShowDialog())
{
case DialogResult.OK://If Ok(Yes) button is pressed on Openfiledialog.
filename = dlgopenfile.FileName;
break;

case DialogResult.Cancel://If Cancel button is pressed on Openfiledialog.
filename = "";
break;
}

if (filename.Length >= 1)
{
if (File.Exists(filename) == true)
{
ButtonOpenFile.Enabled = true;
}
else
{
ButtonOpenFile.Enabled = false;
throw new FileNotFoundException("The file you selected does not exist.");
}
}
}
catch (FileNotFoundException ex)
{
MessageBox.Show(ex.Message, "Form1", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}

The next function occurs if user tries to close the form in mid session.

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
try
{
switch (MessageBox.Show("Do you want to exit ?", "Form1", MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk))
{
case DialogResult.Yes:
this.Close();
break;

case DialogResult.No:
e.Cancel = true;
break;
}
}
catch (Exception)
{
//Exception handling code goes here.
}
}

Finally the function below calls Form2's constructor with the selected file as argument.

private void ButtonOpenFile_Click(object sender, EventArgs e)
{
//This Button is enabled only if the file has been selected and if its exists.
Form2 form2 = new Form2(filename);//filename is the name of selected file,as decided in function BrowseFile().
this.Close();//Close Form1.
form2.ShowDialog();//Show Form2 as modal dialog.
}

Hope it would help you achieve what you need.Anything more,please let me know.

Upvotes: 1

Thilina H
Thilina H

Reputation: 5804

Just you can handle the close event with your own logic

private void Form1_FormClosing_1(object sender, FormClosingEventArgs e)
        {
            if (MessageBox.Show(text:"Are you sure you want to quit?",caption:string.Empty,buttons: MessageBoxButtons.YesNo) == DialogResult.No)
            {
                e.Cancel = true;
            }
        }

Upvotes: 0

python_kaa
python_kaa

Reputation: 1096

Proposally you start you form with form.ShowDialog(), which returns DialogResult. You should check whether it is DialogResult.Ok or whether form.DialogResult != DialogResult.None. In the form, if the user inserts the file you can set the form.DialogResult explicitely to DialogResult.Ok

Upvotes: 0

JJGAP
JJGAP

Reputation: 161

There is an event named "FormClosing" on forms.

A quick sample:

 private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (MessageBox.Show("Are you sure you want to quit?", MessageBoxButtons.YesNo) == DialogResult.No)
            {
                e.Cancel = true;
            }

        }

Upvotes: 1

Alessandro D'Andria
Alessandro D'Andria

Reputation: 8868

If you want to prevent closing the form you can handle the event

bool cancel = true;
protected override void OnFormClosing(FormClosingEventArgs e)
{
    e.Cancel = cancel;
    base.OnFormClosing(e);
}

Remember to change cancel to false when it's done to close the form.

Upvotes: 1

Related Questions