隐藏和显示 TableLayoutPanel 的单元格
我的表格布局面板有一列三行。 (一个停靠在每个单元格中的“填充”面板。)
现在我希望能够隐藏/显示行。我希望任何时候都只显示一行(基于用户对某些单选按钮的选择),并且我希望调整大小以使其填充 TableLayoutPanel 的所有区域。
我怎样才能做到这一点?有什么想法吗?
My tablelayout panel has one column and three rows. (one docked to Fill panel in each cell.)
Now I would like to be able to hide/show the rows . I want only one row to be visible at any time ( based on a user selection of some radio buttons) and I want to to get resized so it fills all the area of the TableLayoutPanel.
How can I do that? Any thoughts?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
如果 TableLayoutPanel 中的行自动调整大小,则隐藏内容面板也将隐藏放置面板的单元格。
If rows in your TableLayoutPanel is autosized then hiding content panel will hide cell where panel placed too.
我建议将其他行高度设置为 0 是最简单的方法:
第一行:
I would suggest setting the other rows heights to 0 is the easiest way:
Row one:
试试这个
Try this
那么为什么要使用
TableLayoutPanel
呢?只需在表单上放置三个
Panel
,填写每行的内容,并将所有三个面板的Dock
属性设置为Fill
。将两个面板设置为Visible = false
,并将一个面板设置为true
。如果您想查看另一个面板,只需使其可见并隐藏其他两个面板(根据您的单选按钮设置)。
So why did you use a
TableLayoutPanel
?Just put three
Panel
s on your form, fill in everyone the content of each row and set theDock
property of all three panels toFill
. Set two panelsVisible = false
and one totrue
.If you like to see another panel, just make it visible and hide the other two (based on your radio button settings).
我的情况类似。我需要一个包含 4 行的 TableLayoutPanel,每行都需要根据复选框选择可见。因此,我可以显示 1 - 4,而不是一次只显示一行。
设计 1 列 4 行的布局后,添加控件并将每个控件的 Dock 设置为 Fill。
然后,在复选框的单个 CheckedChanged 事件处理程序中,我进行了如下编码。这是一种蛮力方法,但是,嘿......它有效!
My scenario is similar. I needed a TableLayoutPanel with 4 rows each of which needed to be visible according to a checkbox selection. So instead of only showing one row at a time, I can show 1 - 4.
After designing the layout with 1 column and 4 rows, the controls were added and Dock set to Fill for each one.
Then in a single CheckedChanged event handler for the checkboxes, I coded as shown below. It's kind of a brute force method, but, Hey...it works!
要隐藏行试试这个!!
To hide row try this!!
我有类似的任务要做,我的解决方案如下:
将 TableLayoutPanel 添加到您的表单(或任何容器)。
将 TableLayoutPanel 的列数和行数设置为 1,并将大小设置为 100%。
将 Dock 设置为填充。
将 GrowStyle 设置为固定大小。
将自动调整大小设置为 true。
然后以编程方式添加所有三个表单/控件,您必须根据单选按钮选择显示其中之一。确保只有其中之一可见。这可以通过初始 FirstControl.Show(); 来完成然后在每个 RadioButton 事件上隐藏当前的并显示另一个。您可以在局部变量中“记住”(例如:“currentlyVisibleControl”当前可见的引用)
注意:如果您一次会 .Show() 多个。那么 TableLayoutPanel 将引发异常,它已满并且无法添加更多项目。
PS 在我自己的示例中,我在 MDI 窗口中有 TableLayoutPanel 以及三种表单,它们在单击按钮时相互替换,因此我认为复制我的源代码将使“口头”示例变得复杂。
PPS 根据我的经验,Visual Studio 有时会在设计模式下做一些奇怪的事情。我必须删除并重新添加 TableLayoutPanel 才能正确设置属性并在设计器和运行时获得结果。因此,如果自动调整大小或绝对/百分比值未在设计器屏幕上显示,则可能是设计器的问题而不是您的问题。只需删除它并重试即可。
I had similar task to do and my solution is following:
Add a TableLayoutPanel to your form (or any container).
Set TableLayoutPanel's columns and rows count to 1 and size to 100%.
Set Dock to Fill.
Set GrowStyle to fixedSize.
Set AutoSize to true.
Then programmatically add all of three forms/controls, one of which you have to show depending on radio button choice. Be sure that only one of them is visible. That could be done with initial FirstControl.Show(); and then on each RadioButton event hide the current one and show another. you may "remember" in local variable (say: "currentlyVisibleControl" the reference which is currently visible)
note: if you will .Show() more than one at time. then TableLayoutPanel wil fire the exception that it is full and can't add any more item.
P.S. In My own example I have TableLayoutPanel in MDI window and three forms which substitute each other on button clicks on them so I think copying my source code will complicate the "verbal" example.
P.P.S. From my experience Visual Studio does some weird things in design mode sometimes. I had to remove and re-add the TableLayoutPanel to set properties correctly and get the results both in designer and in runtime. So if either autosize or absolute/percent values are not depicted on designer screen it may be designers problem rather that yours. JUST DELETE IT AND RETRY.
我尝试使用
Height
和SizeType
属性,但它给了我奇怪的结果。例如,目标行上的标签被隐藏,但文本框未被隐藏。这是我使用 @arbiter 隐藏行的子控件的建议提出的扩展类。
I tried fooling around with the
Height
andSizeType
properties, but it was giving me odd results. For example, the Labels on the target row were being hidden, but the TextBoxes were not.Here is an extension class that I came up with using @arbiter's suggestion of hiding the children Controls of the row.