C # usercontrol how to access all child controls

I defined a custom panel with a table layout panel inside. However, when I used this control on winform, I do not have access to the properties of the table layout pane. (For example, I want to add a column or attach another control in a cell). I am trying to change the modifier property to public, but it still does not work. What can I do to see and change panel layout properties?

In fact, the question may be more general: how to access / change / move the controls contained in user user control?

thanks

+3
source share
1 answer

You need to open the properties that you want to change in your user control. For example, to change the column counting property of a table table control, from your user control you must open the ColumnCount property:

public partial class UserControl1 : UserControl
{
    public UserControl1()
    {
        InitializeComponent();
    }

    public int ColumnCount
    { 
        get
        {
            return this.tableLayoutPanel1.ColumnCount;
        }

        set
        {
            this.tableLayoutPanel1.ColumnCount = value;
        }
    }
}

Then you can also use some attributes to control the display of the user control in Visual Studio, for example, the above can be changed as follows:

[DefaultProperty("ColumnCount")]
public partial class UserControl1 : UserControl
{
    public UserControl1()
    {
        InitializeComponent();
    }

    [Description("Gets or sets the column count of the table layout.")]
    [Category("TableLayout")]
    [DefaultValue(2)]
    public int ColumnCount
    { 
        get
        {
            return this.tableLayoutPanel1.ColumnCount;
        }

        set
        {
            this.tableLayoutPanel1.ColumnCount = value;
        }
    }
}

"ColumnCount" count , 2 , . , .

+7

Source: https://habr.com/ru/post/1716302/


All Articles