C# 从c上的两个不同窗体控制单个窗体#
因此,我有以下几点: FormA,FormB,FormCtrl 加载FormA时,显示FormCtrl:C# 从c上的两个不同窗体控制单个窗体#,c#,winforms,C#,Winforms,因此,我有以下几点: FormA,FormB,FormCtrl 加载FormA时,显示FormCtrl: FormCtrl fc = new FormCtrl(); fc.Show(); 从FormA,我可以在FormCtrl上控制Panel1,也可以加载FormB: FormB fb = new FormB(); fb.Show(); fc.Panel1.Visible = true; fc.Panel2.Visible = true; // I don't know how to do
FormCtrl fc = new FormCtrl();
fc.Show();
从FormA,我可以在FormCtrl上控制Panel1,也可以加载FormB:
FormB fb = new FormB();
fb.Show();
fc.Panel1.Visible = true;
fc.Panel2.Visible = true; // I don't know how to do this :)
从FormB,我需要在FormCtrl上控制Panel2:
FormB fb = new FormB();
fb.Show();
fc.Panel1.Visible = true;
fc.Panel2.Visible = true; // I don't know how to do this :)
当然这是一种不好的做法,但是您可以像这样传递对FormCtrl的引用:
public partial class FormB: Form
{
public FormCtrl Reference { get; set; }
public FormB(FormCtrl referencedForm)
{
InitializeComponent();
Reference = referencedForm;
Reference.Panel2.Visible = true;
}
}
处理容器之间通信的最佳方法是实现一个观察者类 观察者模式是一种软件设计模式,其中一个对象(称为subject)维护其从属对象(称为观察者)的列表,并通常通过调用其方法之一自动通知其任何状态更改。(维基百科) 我这样做的方式是创建一个Observer类,并在其中编写如下内容:
1 public delegate void dlFuncToBeImplemented(string signal);
2 public static event dlFuncToBeImplemented OnFuncToBeImplemented;
3 public static void FuncToBeImplemented(string signal)
4 {
5 OnFuncToBeImplemented(signal);
6 }
private void ObserverRegister()//will contain all observer function registration
{
Observer.OnFuncToBeImplemented += Observer_OnFuncToBeImplemented;
/*and more observer function registration............*/
}
void Observer_OnFuncToBeImplemented(string signal)//the function that will occur when FuncToBeImplemented(signal) will call
{
MessageBox.Show("Signal "+signal+" received!", "Atention!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
所以基本上:第一行说会有一个其他人将实现的函数
第二行是创建一个事件,该事件在委托函数调用时发生
第三行是创建调用事件的函数
因此,在要从外部更改的表单(FormCtrl)中,应添加如下函数:
1 public delegate void dlFuncToBeImplemented(string signal);
2 public static event dlFuncToBeImplemented OnFuncToBeImplemented;
3 public static void FuncToBeImplemented(string signal)
4 {
5 OnFuncToBeImplemented(signal);
6 }
private void ObserverRegister()//will contain all observer function registration
{
Observer.OnFuncToBeImplemented += Observer_OnFuncToBeImplemented;
/*and more observer function registration............*/
}
void Observer_OnFuncToBeImplemented(string signal)//the function that will occur when FuncToBeImplemented(signal) will call
{
MessageBox.Show("Signal "+signal+" received!", "Atention!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
在你的其他表格(表格A,表格B)中,你应该做如下事情:
public static int signal = 0;
public void button1_Click(object sender, EventArgs e)
{
Observer.FuncToBeImplemented(signal);//will call the event in the user control
}
现在,你可以把这个函数注册到一大堆其他的控件和容器中,它们都会得到信号
我希望这会有所帮助:)为什么不将
fc
实例作为构造函数注入参数传递,只要FormCtrl存在于应用程序中的某个位置,就可以使用FormCtrl fc=application.OpenForms.OfType().FirstOrDefault()代码>以在应用程序中的任何位置访问它。对象意大利面是危险的。你需要一个控制器,MVC中的C。Winforms不强制任何特定的MVC模式。标准方法是使主窗体也成为控制器。它在这里起作用,因为它引用了这两种形式。使用FormB中的事件通知主窗体它需要C.ok。我设法从FormB显示/隐藏FormCtrl@Rahul我将实例表单FormA传递给FormB。一个问题是我无法从FormB访问FormCtrl上的Panel1或Panel2。FormCtrl上的“每个对象”修改器都设置为“公共”。@HansPassant您能帮我制作控制器吗?URL也很好:)