如何在 C# 中显示一个用户控件并隐藏其余控件
本文关键字:控件 用户 隐藏 余控件 一个 显示 | 更新日期: 2023-09-27 18:32:58
我是C#的新手,我正在使用Windows表单。
我有 20 buttons
和 20 user controls
Form1
.我想做的是:
当我单击"显示button1
user control1
user controls
其余部分时,单击button2
user control2
显示,其余部分隐藏user controls
单击button3
user control3
显示,其余部分user controls
隐藏,依此类推。我可以使用UserControl.visible = True
来显示一个user control
并通过设置其余部分来隐藏其余user controls visibility = False
但我不想编写太多代码。
因此,我使用以下简单的代码在单击相关button
时仅显示一个user control
并隐藏user controls
的其余部分,但是此代码不起作用,它运行没有错误,但user controls
仍然可见。
有谁知道为什么这段代码没有显示one user control
并隐藏其余部分?我也很乐意听到其他想法。谢谢
private void button1_Click(object sender, EventArgs e)
{
foreach (Control ctrl in this.Controls)
{
if (ctrl .GetType() == typeof(UserControl))
{
ctrl .Visible = false;
}
}
UserControl1.visible = True;
}
代码未按预期工作的原因是以下条件:
if (ctrl.GetType() == typeof(UserControl))
GetType()
返回永远不会UserControl
的实际类型,但从中派生了一些类型。
请改用以下内容
if (ctrl is UserControl)
Button
具有属性Tag
。将所有按钮的 tag 属性中保存UserControl
引用:
button1.Tag = UserControl1;
button2.Tag = UserControl2;
button3.Tag = UserControl3;
//and so on...
//you can do it FormLoad event handler
现在将所有按钮订阅到单个Click
事件:
button1.Click += button1_Click; //I am using "button1_Click" for all buttons
button2.Click += button1_Click;
button3.Click += button1_Click;
现在主要的事情在这里:
private void button1_Click(object sender, EventArgs e)
{
Button btn = sender as Button; //cast the sender
//hide all user controls
foreach (Control ctrl in this.Controls)
{
if (ctrl .GetType() == typeof(UserControl))
{
ctrl .Visible = false;
}
}
//show the usercontrol saved in "Tag" of the current button
(btn.Tag as UserControl).Visible = True;
}
注意:如果您收到任何错误,我尚未编译此代码。请让我知道。
这段代码对我来说是一种享受:
var pairs = new[]
{
new { Button = button1, UserControl = userControl1 },
new { Button = button2, UserControl = userControl2 },
new { Button = button3, UserControl = userControl3 },
new { Button = button4, UserControl = userControl4 },
new { Button = button5, UserControl = userControl5 },
new { Button = button6, UserControl = userControl6 },
new { Button = button7, UserControl = userControl7 },
new { Button = button8, UserControl = userControl8 },
new { Button = button9, UserControl = userControl9 },
new { Button = button10, UserControl = userControl10 },
new { Button = button11, UserControl = userControl11 },
new { Button = button12, UserControl = userControl12 },
new { Button = button13, UserControl = userControl13 },
new { Button = button14, UserControl = userControl14 },
new { Button = button15, UserControl = userControl15 },
new { Button = button16, UserControl = userControl16 },
new { Button = button17, UserControl = userControl17 },
new { Button = button18, UserControl = userControl18 },
new { Button = button19, UserControl = userControl19 },
new { Button = button20, UserControl = userControl20 },
};
foreach (var pair in pairs)
{
pair.UserControl.Visible = false;
pair.Button.Click += (s, e2) =>
{
foreach (var pair2 in pairs) { pair2.UserControl.Visible = false; }
pair.UserControl.Visible = true;
};
}
我特意创建了一个按钮/用户控件对的列表,以便它们是硬编码的,我不必按名称查找它们。这种方式更适合重构和验证代码。
然后,第一个 foreach
循环只是连接 click 事件,这使得除配对用户控件之外的所有用户控件都不可见。
只需在 Form_Load
事件处理程序中弹出此代码即可。