TableLayoutPanel GetControlFromPosition 未获取不可见控件。如何访问指定位置的不可见控件?
我正在使用 TableLayoutPanel
,并且希望在 TableLayoutPanel
中的特定位置获取控件。我想迭代 TableLayoutPanel
的行和列,但如果我只想在特定行和列上使用单个控件,这个问题同样适用。
不幸的是,GetControlFromPosition(int column, int row)
仅检索可见的控件(即它们的 Visible 属性设置为 True)。这对我来说没有好处,因为有时我想访问不可见的特定位置的控件,然后使其可见。
我不得不求助于迭代 TableLayoutPanel.Controls
集合,然后使用 GetPositionFromControl(Control control)
或 GetCellPosition(Control control) 获取每个控件的位置)
直到找到我想要的位置。
(我不确定 GetPositionFromControl
和 GetCellPosition
方法之间的区别,因为 MS 文档很少,但是 我会单独问这个问题)。
有没有更简单或更干净的方法可以让我做到这一点?
I'm using a TableLayoutPanel
and I want to get the control at a specific position in the TableLayoutPanel
. I want to iterate over the rows and columns of TableLayoutPanel
but this question applies equally if I just wanted a single control at a specific row and column.
Unfortunately GetControlFromPosition(int column, int row)
only retrieves controls that are visible (that is their Visible property is set to True). This is no good to me as sometimes I want to access a control at a specific position that is not visible and then make it visible.
I have had to resort to iterating over the TableLayoutPanel.Controls
collection, and then get the position of each control using GetPositionFromControl(Control control)
or GetCellPosition(Control control)
until I find the position I want.
(I'm not sure of the difference between GetPositionFromControl
and GetCellPosition
methods as MS documentation is meagre, but I'll ask that question separately).
Is there a simpler or cleaner way for me to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我能想到的最好办法就是创建一个扩展方法。创建一个名为“Extensions.vb”的新模块并添加:
现在您可以使用以下内容来访问该方法:
扩展方法将该方法添加到作为第一个参数列出的类中,在本例中
Panel As TableLayoutPanel,并随机排列其余参数。
The best I can come up with is creating an extension method. Create a new module named "Extensions.vb" and add:
Now you can use the following to access the method:
Extension methods add the method to the class that's listed as the first parameter, in this case
Panel As TableLayoutPanel
, and shuffle the rest of the parameters along.更好的c#方法:
better c# method:
对于 c#:
And for c#:
我找到了一种解决方法/黑客,可以使用 GetControlFromPosition 来设置visible=false,首先将控件添加到表格布局面板,然后将visible 设置为 false
示例:
I found a workaround/hack to use GetControlFromPosition for visible=false, first add the control to the table layout panel and then set visible to false
example: