【发布时间】:2015-09-10 13:27:00
【问题描述】:
我制作了一个非常简单的应用程序,其中有 4 个单选按钮和复选框,如下图所示:
选中一个复选框时,将触发单选按钮位置。
示例:radiobutton1处于40、40位,而RadioButton2在100、40 ..检查第一个复选框时(Checkbox1),则RadioButton1将消失,而RadioButton2将以RadioButton1的位置为位置。其他单选按钮也是如此。
请看下图:
这是我正在使用的代码:
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (checkBox1.Checked)
{
radioButton1.Visible = false;
radioButton2.Location = new Point(57, 40);
radioButton3.Location = new Point(242, 40);
radioButton4.Location = new Point(435, 40);
}
else
{
radioButton1.Visible = true;
radioButton1.Location = new Point(57, 40);
radioButton2.Location = new Point(242, 40);
radioButton3.Location = new Point(435, 40);
radioButton4.Location = new Point(619, 40);
}
}
如你所见,我以硬编码的方式修改了radioButtons的位置。
我的问题是:如何使单选按钮的位置动态化?所以当radioButton1 消失时,radioButton2 将取代radioButton1 的位置,其余的由它自己代替,而不是通过硬编码的方式。
非常感谢您的回答!
谢谢
【问题讨论】:
-
你可以从
radioButton2.Location = radioButton1.Location等开始... -
这个问题可以用健康剂量的“为什么”来解决。这似乎是一个非常笨拙的 UI 设计,所以在不知道为什么要这样做的情况下,我的第一个想法是“不要”。
-
如果您未绑定到 WinForms,则可能值得研究 WPF 并使用
StackPanel以及Visibility属性。 -
我不确定他们是否有免费版本,但 DevExpress 库有一个 LayoutControl,它将根据容器的可见性在其中动态移动控件。因此,如果您隐藏一项,则下一项将“折叠”到它的位置。这可能是一种选择。