Reputation: 1544
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?
Upvotes: 3
Views: 5968
Reputation: 445
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;
Upvotes: 1
Reputation: 1346
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;
}
Upvotes: 3
Reputation: 11
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;
}
}
}
Upvotes: 1
Reputation: 12794
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.
Upvotes: 3