views:

99

answers:

2

I'm new to C# and would like to allow to Windows forms to comminicate with each other. I googled bubbling in C# but it wasn't much help. What are some good ways I can learn bubbling?

EDIT: I want to have an options form that is shown/created when my user clicks on Edit->Preferances. I then want the settings the user changed in the options form to be relayed to the main form.

A: 

You don't state as much, but is the main form also the form that contains the Edit->Preferences menu? If so, you are already at the correct point in the code

// This is the event handler in the main form
private void mnuEditPreferencesClicked...
{
    FrmPreferences frmPreferences = new FrmPreferences();
    frmPreferences.ShowDialog(this);
    // Preferences saved, implement changes to main form here
}

If the preferences form is not generated from the main form, fire off an event when the preferences form closes, and have the main form handle the event that way.

Bryan
+1  A: 

Two approaches:

Put properties on your preferences form and access them from the main form when the user clicks OK.

if (preferenceForm.ShowDialog() == DialogResult.OK)
{
     this.Color = preferenceForm.UserSelectedColor;
     //etc...
}

Send your preference form a delegate from the main form and let the preference form call it with the appropriate changes.

class FormSettings
{
     object Color {get, set}
}


class MainForm
{
    ...

    void ChangeSettings(FormSettings newSettings)
    { ... }

    void EditPreferences_Click(...)
    {
        ...

        EditPreferencesForm editPreferences = new EditPreferencesForm(this.ChangeSettings)
        editPreferences.ShowDialog();
    }     
}

class EditPreferencesForm
{
     ...
     ChangeSettingsDelegate changeSettings;
     FormSettings formSettings;

     void OkButton_Click(...)
     {
          changeSettings(formSettings);
     }
}
Austin Salonen