Sergey Shafiev
Sergey Shafiev

Reputation: 4375

Interaction between forms in WinForms application, c#

guys! I've got 2 forms in application - working form (frmMain) and form of settings (frmSettings). There are two buttons on frmSettings - Save and Cancel. In frmMain I use the following approach to show the frmSettings:

 private void btnSettings_Click(object sender, EventArgs e)
        {
            frmSettings = new SettingsForm();
            frmSettings.ShowDialog();
            // ...
        }

The problem is I don't know, how to detect, which button was pressed on the frmMain - Save or Cancel. The further logic of the program depends on this fact. I need something like this:

private void btnSettings_Click(object sender, EventArgs e)
        {
            frmSettings = new SettingsForm();
            frmSettings.ShowDialog();

            if(/* frmSettings.SaveButton.WasClicked == true */)
            {
                InitializeServices();
            }
            // ...
        }

Please, give me an advice, how to implement such kind of interaction between forms. Better without using global variables for saving buttons state. Thanks beforehand.

Upvotes: 0

Views: 1244

Answers (4)

daniloquio
daniloquio

Reputation: 3912

ShowDialog returns a DialogResult object that allow you to know that. You have to:

On Save Button's click event, set this.DialogResult to DialogResult.OK

On Cancel Button's click event, set this.DialogResult to DialogResult.Cancel

private void btnSettings_Click(object sender, EventArgs e)
{
   frmSettings = new SettingsForm();
   if(frmSettings.ShowDialog() == DialogResult.OK)
   {
       InitializeServices();
   }
   //.......    
}

Edited to manage the DialogResult as @tsiorn's answer: setting form's DialgoResult insted of setting that property on each button.

Upvotes: 3

Bill Tarbell
Bill Tarbell

Reputation: 5234

in the frmSettings window you handle the Click events on the buttons. Then set the dialog result:

void frmSettings_Save_Click(object sender, EventArgs e)
{
  this.DialogResult = DialogResult.OK;
}

void frmSettings_Cancel_Click(object sender, EventArgs e)
{
  this.DialogResult = DialogResult.Cancel;
}

in the main form you do something like this to capture and evaluate the result:

DialogResult answer = frmSettings.ShowDialog();
if (answer == DialogResult.OK)
{
  ...
}

Additional information and usage can be found here: http://msdn.microsoft.com/en-us/library/system.windows.forms.form.dialogresult.aspx

Upvotes: 0

tsiorn
tsiorn

Reputation: 2236

You chould use DialogResult to handle this. On your form settings window, you can set the result as so:

protected void btnSave_Click(object sender, EventArgs e) {
    DialogResult = System.Windows.Forms.DialogResult.OK
    this.close;
}
protected void btnCancel_Click(object sender, EventArgs e) {
    DialogResult = System.Windows.Forms.DialogResult.Cancel;
    this.close;
}

Then ...

private void btnSettings_Click(object sender, EventArgs e) 
        { 
            frmSettings = new SettingsForm(); 
            frmSettings.ShowDialog(); 

            if(frmSettings.DialogResult == DialogResult.OK) 
            { 
                // save
                InitializeServices(); 
            } 
            //  ... 
        } 

Upvotes: 1

Servy
Servy

Reputation: 203847

Start with an enumeration of the possible values:

public enum ExitMethod
{
    Other, //this should be first, as a default value
    Save,
    Cancel,
    Error
}

Then make a property on SettingsForm of that type:

public ExitMethod ExitMethod { get; private set; }

In SettingsForm's save/exit methods set that property to the appropriate enum value, and in the main form you can read that property value.

Upvotes: 0

Related Questions