在usercontrol单击事件 - winform中切换mainform中的用户控件 [英] Switch usercontrols in mainform within a usercontrol click event - winform
问题描述
这可能听起来很愚蠢,但我很难弄明白这一点;任何帮助将不胜感激:
我有两个用户控件,名为S afety_Check 和 OEE_Track 。在我的 MainForm 中,我有一个名为 pnl_main_controller 的面板,这是我显示两个用户控件的地方。我的主表单上有两个按钮,我在两者之间动态切换,没有任何问题。
Safety_Check用户控制;
This may sound stupid, But I am having hard time to figure this out; any help would be appreciated:
I have two user controls called "Safety_Check" and "OEE_Track". In my MainForm I have a panel called "pnl_main_controller" this is where I am displaying both my user controls. I have two buttons on my main form and I am dynamically switching between both without any issue.
Safety_Check User control;
public partial class Safety_Check : UserControl
{
private static Safety_Check _instance;
public static Safety_Check instance
{
get
{
if (_instance == null)
_instance = new Safety_Check();
return _instance;
}
}
public Safety_Check()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
///////------------------------
}
}
OEE_Track User control
public partial class OEE_Track : UserControl
{
private static OEE_Track _instance;
public static OEE_Track instance
{
get
{
if (_instance == null)
_instance = new OEE_Track();
return _instance;
}
}
public OEE_Track()
{
InitializeComponent();
}
}
MainForm:
public partial class MainForm : Form
{
private void btn_reg_Click(object sender, EventArgs e)
{
if (!pnl_main_controller.Contains(Safety_Check.instance))
{
pnl_main_controller.Controls.Add(Safety_Check.instance);
Safety_Check.instance.Dock = DockStyle.Fill;
Safety_Check.instance.BringToFront();
}
else
{
Safety_Check.instance.BringToFront();
} }
private void btn_OEE_Click(object sender, EventArgs e)
{
if (!pnl_main_controller.Contains(OEE_Track.instance))
{
pnl_main_controller.Controls.Add(OEE_Track.instance);
OEE_Track.instance.Dock = DockStyle.Fill;
OEE_Track.instance.BringToFront();
}
else
{
OEE_Track.instance.BringToFront();
}
}
我要做的是在我的我有一个名为 Button1 的按钮 Safety_Check Usercontrol,每当我按下此按钮,我希望 pnl_main_controller 消失,并带上 OEE_Track 到面板
我尝试过:
What I am trying to do is I have a button called "Button1" on my "Safety_Check" Usercontrol, whenever I press this , I want "Safety_Check" to be disappear on "pnl_main_controller" and bring "OEE_Track" to the panel
What I have tried:
if (!main.pnl_main_controller.Contains(Safety_Check.instance))
{
Safety_Check.instance.Hide();
main.pnl_main_controller.Controls.Add(OEE_Track.instance);
OEE_Track.instance.Dock = DockStyle.Fill;
OEE_Track.instance.BringToFront();
}
else
{
OEE_Track.instance.BringToFront();
}
推荐答案
这是我在WPF中的事件处理程序中执行此操作的方式(当然,您没有告诉我们你正在使用什么框架,所以我只是猜测一个解决方案):
This is how I might do it in your event handler in WPF (of course, you didn't tell us what framework you're working with, so I'm just guessing at a solution):
// toggle the first item
Safety_Check.Visibility = (Safety_Check.Visibility == Visibility.Collapsed) ? Visibility.Visible
: Visibility.Collapsed;
// and then toggle the second item, based on the first item's visibility
OEE_Track.Visibility = (Safety_Check.Visibility == Visibility.Collapsed) ? Visibility.Visible
: Visibility.Collapsed;
不要这样做。
这是一个糟糕的解决方案,因为它违背了OOP原则:子控件不仅必须意识到它们的父(这会损害设计),还必须意识到彼此的存在(和类型) - 这使得它无法重用,而且意味着你不能在不考虑其他类别中其他代码的影响的情况下进行任何更改。
这里有很多糟糕的设计理念!
相反,使用以父表格处理的事件来控制发生的事情。
见这里:在两种形式之间传递信息,第3部分:儿童与儿童 [ ^ ] - 它是关于表单(但用户控件以完全相同的方式工作,表单是控件,毕竟)和通信但是相同的原则和机制适用。
Don't do it like that.
That's a poor solution, because it's against OOPs principles: the "child" controls have to not only be aware of their "parent" (which compromises design) but also of the existence (and type) of each other - which makes it impossible to reuse, and also means you can't make any changes without considering the effect of other code in other classes.
That's a lot of bad design ideas going on here!
Instead, use Events which are handled in the "parent form" to control what is going on.
See here: Transferring information between two forms, Part 3: Child to Child[^] - it's about forms (but user controls work in exactly the same way, forms are controls, after all) and communications but the same principles and mechanisms apply.
这篇关于在usercontrol单击事件 - winform中切换mainform中的用户控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!