问题描述
这可能听起来很愚蠢,但我很难弄明白这一点;任何帮助将不胜感激:
我有两个用户控件,名为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();
}
推荐答案
// 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;
这篇关于在usercontrol单击事件 - winform中切换mainform中的用户控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!