[英]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. 我是C#的新手,并且希望允许Windows窗体相互简化。 I googled bubbling in C# but it wasn't much help. 我用C#在Google上冒泡,但是并没有太大帮助。 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. 如果首选项表单不是从主表单生成的,则在首选项表单关闭时触发事件 ,并让主表单以这种方式处理该事件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.