TableLayoutPanel GetControlFromPosition 未获取不可见控件。如何访问指定位置的不可见控件?

发布于 2024-11-30 11:35:25 字数 767 浏览 1 评论 0原文

我正在使用 TableLayoutPanel,并且希望在 TableLayoutPanel 中的特定位置获取控件。我想迭代 TableLayoutPanel 的行和列,但如果我只想在特定行和列上使用单个控件,这个问题同样适用。
不幸的是,GetControlFromPosition(int column, int row) 仅检索可见的控件(即它们的 Visible 属性设置为 True)。这对我来说没有好处,因为有时我想访问不可见的特定位置的控件,然后使其可见。

我不得不求助于迭代 TableLayoutPanel.Controls 集合,然后使用 GetPositionFromControl(Control control)GetCellPosition(Control control) 获取每个控件的位置) 直到找到我想要的位置。
(我不确定 GetPositionFromControlGetCellPosition 方法之间的区别,因为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

轮廓§ 2024-12-07 11:35:25

我能想到的最好办法就是创建一个扩展方法。创建一个名为“Extensions.vb”的新模块并添加:

Imports System.Runtime.CompilerServices

Public Module Extensions

    <Extension()>
    Public Function GetAnyControlAt(Panel As TableLayoutPanel, Column As Integer, Row As Integer) As Control
        For Each PanelControl As Control In Panel.Controls
            With Panel.GetCellPosition(PanelControl)
                If Column = .Column AndAlso Row = .Row Then Return PanelControl
            End With
        Next
        Return Nothing
    End Function
End Module

现在您可以使用以下内容来访问该方法:

Dim MyControl As Control = TableLayoutPanel1.GetAnyControlAt(Column, Row)

扩展方法将该方法添加到作为第一个参数列出的类中,在本例中 Panel As TableLayoutPanel,并随机排列其余参数。

The best I can come up with is creating an extension method. Create a new module named "Extensions.vb" and add:

Imports System.Runtime.CompilerServices

Public Module Extensions

    <Extension()>
    Public Function GetAnyControlAt(Panel As TableLayoutPanel, Column As Integer, Row As Integer) As Control
        For Each PanelControl As Control In Panel.Controls
            With Panel.GetCellPosition(PanelControl)
                If Column = .Column AndAlso Row = .Row Then Return PanelControl
            End With
        Next
        Return Nothing
    End Function
End Module

Now you can use the following to access the method:

Dim MyControl As Control = TableLayoutPanel1.GetAnyControlAt(Column, Row)

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.

又怨 2024-12-07 11:35:25

更好的c#方法:

public static Control GetAnyControlAt(this TableLayoutPanel panel, int column, int row)
{
    foreach (Control control in panel.Controls)
    {
        var cellPosition = panel.GetCellPosition(control);
        if (cellPosition.Column == column && cellPosition.Row == row)
            return control;
    }
    return null;
}

better c# method:

public static Control GetAnyControlAt(this TableLayoutPanel panel, int column, int row)
{
    foreach (Control control in panel.Controls)
    {
        var cellPosition = panel.GetCellPosition(control);
        if (cellPosition.Column == column && cellPosition.Row == row)
            return control;
    }
    return null;
}
一身骄傲 2024-12-07 11:35:25

对于 c#:

public static class ExtensionMethods
        {
            public static Control GetAnyControlAt(TableLayoutPanel pp, int col, int row)
            {
            bool fnd = false;
            Control sendCC = null;
            foreach (Control cc in pp.Controls)
            {
                if (pp.GetCellPosition(cc).Column == col)
                {
                    if (pp.GetCellPosition(cc).Row == row)
                    {
                        sendCC = cc;
                        fnd = true;
                        break;
                    }
                }
            }

            if (fnd == true)
            {
                return sendCC;
            }
            else
            {
                return null;
            }
        }
    }

And for c#:

public static class ExtensionMethods
        {
            public static Control GetAnyControlAt(TableLayoutPanel pp, int col, int row)
            {
            bool fnd = false;
            Control sendCC = null;
            foreach (Control cc in pp.Controls)
            {
                if (pp.GetCellPosition(cc).Column == col)
                {
                    if (pp.GetCellPosition(cc).Row == row)
                    {
                        sendCC = cc;
                        fnd = true;
                        break;
                    }
                }
            }

            if (fnd == true)
            {
                return sendCC;
            }
            else
            {
                return null;
            }
        }
    }
人事已非 2024-12-07 11:35:25

我找到了一种解决方法/黑客,可以使用 GetControlFromPosition 来设置visible=false,首先将控件添加到表格布局面板,然后将visible 设置为 false

示例:

CheckBox Chk_Tbl_exist = new CheckBox();
Chk_Tbl_exist.Text = "This is a checkbox"; 
TableLayoutPanel.Controls.Add(Chk_Tbl_exist, 0, 1);
Chk_Tbl_exist.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:

CheckBox Chk_Tbl_exist = new CheckBox();
Chk_Tbl_exist.Text = "This is a checkbox"; 
TableLayoutPanel.Controls.Add(Chk_Tbl_exist, 0, 1);
Chk_Tbl_exist.Visible = false;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文