我在FlowLayoutPanel中使用UserControl作为按钮,因为它包含文本和其他信息。通过单击FlowLayoutPanel内的UserControl,它应该在我的Main表单中显示表单。
这是UserControl示例:
private void SelectuCControl_MouseClick(object sender, MouseEventArgs e)
{
Main OP = new Main();
OP.ucControl.Show();
OP.ucControl.BringToFront();
}
在Main
Form中,ucControl
(UserControl)的访问修饰符设置为Public。
但是当我点击时,没有任何反应,每次点击都会使CPU和内存越来越高。在背景中发生了一些事情,但我看不到它。
我也尝试过不同的方式。 以下是另一种不成功尝试方式的示例。
主要形式:
public void ShowUC()
{
ucControl.Show();
ucControl.BringToFront();
}
并在UserControl中:
private void SelectContacts_MouseClick(object sender, MouseEventArgs e)
{
Main OP = new Main();
OP.ShowUC();
}
但是,同样的事情。 您知道如何通过单击位于FlowLayoutPanel中的另一个UserControl来显示隐藏在主窗体中的UserControl吗?
如果您编写以下代码:
Main x = new Main();
Main y = new Main();
...然后x
和y
不是相同的Main
- 它们是两个独立的Main
实例。
如果你互动或改变任何关于x
的东西,那么它对y
没有任何影响。
如果你确实希望y
是同一个实例,那么你需要做这样的事情:
Main x = new Main();
Main y = x;
在您的代码中,您需要找到一种方法来执行以下操作:
public Main OP;
private void SelectuCControl_MouseClick(object sender, MouseEventArgs e)
{
OP.ucControl.Show();
OP.ucControl.BringToFront();
}
现在,您可以在设置OP
时将Main
设置为UserControl
的相同实例。
我找到了解决方案我所要做的就是在我用作按钮的UserControl中添加此命令。
Main OP = (Main)ParentForm;
OP.ShowUC();