Search code examples
c#winformsevent-bubbling

Where can I find a good tutorial on bubbling?


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.


Solution

  • 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);
         }
    }