如何从 Windows.Form 调用 UserControl 的方法
How to call a UserControl's methods from a Windows.Form
窗体有一个按钮和一个带有 UserControl 的面板,UserControl 有一个 ListBox 和一个 TextBox。
当我单击 Windows.Form 按钮时,它会调用 UserControl 的 Add()
listBoxTitles.Items.Add(metroTextBoxTitles.Text);
metroTextBoxTitles.Clear();
这只是将 UserControl 的 TextBox.Text 所具有的任何内容添加到 UserControl 的列表框中。
出于某种原因,当我点击按钮时没有任何反应。
为什么。 UserControl 上的任何内容都不能更改或使用?或者它改变了但没有改变 update/show 这是怎么回事?
处理容器间通信的最佳方式是实现一个观察者class
观察者模式是一种软件设计模式,在该模式中,称为主体的对象维护其依赖者(称为观察者)的列表,并自动通知它们任何状态更改,通常是通过调用它们的方法之一。
(维基百科)
我这样做的方法是创建一个观察者 class:
1 public delegate void dlFuncToBeImplemented(int signal);
2 public static event dlFuncToBeImplemented OnFuncToBeImplemented;
3 public static void FuncToBeImplemented(int signal)
4 {
5 OnFuncToBeImplemented(signal);
6 }
所以基本上:第一行说会有其他人将实现的功能
第二行是创建一个在委托函数调用时发生的事件
第三行是创建调用事件的函数
所以在你的 UserControl 中你应该添加一个像这样的函数:
private void ObserverRegister()//will contain all observer function registration
{
Observer.OnFuncToBeImplemented += Observer_OnFuncToBeImplemented;
/*and more observer function registration............*/
}
void Observer_OnFuncToBeImplemented(int signal)//the function that will occur when FuncToBeImplemented(signal) will call
{
MessageBox.Show("Signal received!", "Atention!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
在您的表单中,您应该执行以下操作:
public static int signal = 0;
public void button1_Click(object sender, EventArgs e)
{
Observer.FuncToBeImplemented(signal);//will call the event in the user control
}
现在,您可以将此函数注册到一大堆其他控件和容器,它们都会收到信号
希望对您有所帮助:)
所以,发生的事情是,当您创建一个 UserControl 并将其添加到 Window.Form 时,表单的设计者已经启动了该 UserControl:
private UserControl userControl1;
所以为了解决这个问题,我只需要使用设计器代码创建的 UserControl:
usercontrol1.add();
一切正常。
窗体有一个按钮和一个带有 UserControl 的面板,UserControl 有一个 ListBox 和一个 TextBox。
当我单击 Windows.Form 按钮时,它会调用 UserControl 的 Add()
listBoxTitles.Items.Add(metroTextBoxTitles.Text);
metroTextBoxTitles.Clear();
这只是将 UserControl 的 TextBox.Text 所具有的任何内容添加到 UserControl 的列表框中。
出于某种原因,当我点击按钮时没有任何反应。
为什么。 UserControl 上的任何内容都不能更改或使用?或者它改变了但没有改变 update/show 这是怎么回事?
处理容器间通信的最佳方式是实现一个观察者class
观察者模式是一种软件设计模式,在该模式中,称为主体的对象维护其依赖者(称为观察者)的列表,并自动通知它们任何状态更改,通常是通过调用它们的方法之一。 (维基百科)
我这样做的方法是创建一个观察者 class:
1 public delegate void dlFuncToBeImplemented(int signal);
2 public static event dlFuncToBeImplemented OnFuncToBeImplemented;
3 public static void FuncToBeImplemented(int signal)
4 {
5 OnFuncToBeImplemented(signal);
6 }
所以基本上:第一行说会有其他人将实现的功能
第二行是创建一个在委托函数调用时发生的事件
第三行是创建调用事件的函数
所以在你的 UserControl 中你应该添加一个像这样的函数:
private void ObserverRegister()//will contain all observer function registration
{
Observer.OnFuncToBeImplemented += Observer_OnFuncToBeImplemented;
/*and more observer function registration............*/
}
void Observer_OnFuncToBeImplemented(int signal)//the function that will occur when FuncToBeImplemented(signal) will call
{
MessageBox.Show("Signal received!", "Atention!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
在您的表单中,您应该执行以下操作:
public static int signal = 0;
public void button1_Click(object sender, EventArgs e)
{
Observer.FuncToBeImplemented(signal);//will call the event in the user control
}
现在,您可以将此函数注册到一大堆其他控件和容器,它们都会收到信号
希望对您有所帮助:)
所以,发生的事情是,当您创建一个 UserControl 并将其添加到 Window.Form 时,表单的设计者已经启动了该 UserControl:
private UserControl userControl1;
所以为了解决这个问题,我只需要使用设计器代码创建的 UserControl:
usercontrol1.add();
一切正常。