删除隐藏的控件(Remove hidden controls)

编程入门 行业动态 更新时间:2024-10-23 18:24:55
删除隐藏的控件(Remove hidden controls)

在运行时,我必须决定是否显示一堆控件。 我正在处理这个表单: 我的表单

在某些情况下,我想要隐藏所有控件,但“行字体大小”标签,其Combobox和“关闭”按钮。 这样做时,我想要移动剩余的组件并调整主窗体的大小,使其看起来不错。

正如评论中所建议的那样,我尝试在面板中使用FlowLayoutPanel。 CheckedBoxList的一个面板,每个对的一个面板(标签,Combobox),因为我想保持标签和组合框在同一行。

有了这个解决方案,我有两个问题:

更多要管理的组件:我必须现在隐藏面板而不是其他初始组件 自动调整大小不再工作:如果我调整主窗体的大小,内部组件不会再更改大小。

另外,我查看了TableLayoutPanel,并没有为我选择一个选项,因为我在每一行中有不同数量的列。 第一行,我只有一个CheckedBoxList,第二行有一个标签和一个组合框...

任何建议来解决这个问题,并保持所有控件的自动调整大小功能?

At run time, I have to decide whether to display a bunch of controls or not. I'm working on this form: My form

In some cases, i would like to hide all controls but "Row font size" label, its Combobox and "Close" button. When doing so, i want to move the remaining components up and resize the main form so it looks nice.

As suggested in the comments, i tried to use FlowLayoutPanel with panels. One panel for CheckedBoxList, One panel for each pair (label, Combobox) as i want to keep Labels and ComboBoxes in the same line.

With this solution, i have two issues:

More components to manage: I have to hide the panel now instead of other initial components Auto-resize is not working any more: If I resize the main form, inner components doesn't change size any more.

Also, I looked at the TableLayoutPanel, and it doesn't look an option for me as i have different number of columns in each row. First row, I have only a CheckedBoxList and in the second row a have a label and a combobox...

Any suggestion to address this issue and keep the Auto-resize feature for all controls?

最满意答案

在Winforms中,它在后台使用网格类型的布局。 所以你必须自己控制布局。 最简单的方法是创建一个名为RefreshLayout()的方法,并在您隐藏或显示按钮时调用此方法。 所以你的情况看起来像这样:

if (condition) { buttonDown.Hide(); RefreshLayout(); }

在RefreshLayout()方法中,您需要确定该按钮是隐藏的还是可见的,然后通过按钮大小向上或向下移动按钮下的所有控件。 此外,您可能需要更改表格本身的高度。 所以它看起来像这样:

public void RefreshLayout() { int offset; if (buttonDown.Visible) { // The button is visible offset = buttonDown.Height; } else { // The button is hidden offset = buttonDown.Height * -1; } // For each control under the button... control1.Location.Y = control1.Location.Y + offset; control2.Location.Y = control2.Location.Y + offset; // and so forth, for each control }

In Winforms, it uses a grid kind of layout in the background. So you have to take control of the layout yourself. The easiest way to do this is to create a method called RefreshLayout() and call this method anytime you hide or show the button. So your condition will look like this:

if (condition) { buttonDown.Hide(); RefreshLayout(); }

In the RefreshLayout() method, you need to determine if the button is hidden or visible and then move all the controls below the button up or down by the button size. Also, you may want to change the height of the form itself. So it will look something like this:

public void RefreshLayout() { int offset; if (buttonDown.Visible) { // The button is visible offset = buttonDown.Height; } else { // The button is hidden offset = buttonDown.Height * -1; } // For each control under the button... control1.Location.Y = control1.Location.Y + offset; control2.Location.Y = control2.Location.Y + offset; // and so forth, for each control }

更多推荐

本文发布于:2023-08-07 17:45:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1465068.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:控件   Remove   controls   hidden

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!