简体   繁体   中英

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.

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM