C# usercontrol如何访问所有子控件

发布于 2024-08-03 16:37:06 字数 194 浏览 3 评论 0原文

我定义了一个自定义面板,里面有一个表格布局面板。但是,当我在 winform 上使用此控件时,我无法访问表布局面板属性。 (例如,我想在单元格中添加一列或停靠其他控件)。我尝试将修饰符属性更改为public,但仍然不起作用。我该怎么做才能查看和更改面板布局属性?

事实上,问题可以更通用:如何访问/修改/移动自定义用户控件中包含的控件?

谢谢

I defined a custom panel with a table layout panel inside. However when I used this control on a winform I do not have access to the table layout panel properties. (I want for instance add a column or dock an other control in a cell). I try to changed the modifier property to public, but it still does not work. What can I do in order to see and change the panel layout properties ?

In fact, the question can be more generic : how to access/modify/move controls contained in a custom usercontrol ?

Thx

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

天煞孤星 2024-08-10 16:37:06

您需要在用户控件中公开要修改的属性。例如,要更改表布局控件的列计数属性,您必须从用户控件公开 ColumnCount 属性:

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

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

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

然后您还可以开始使用一些属性来控制用户控件在 Visual Studio 中的显示方式,例如例如,上面可以这样修改:

[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”,并给列计数属性一个描述,默认值为2,并设置它应该显示在什么类别中设计器的属性窗口。用户控件还可以做很多事情来添加设计时支持。

You need to expose the properties you want to modify in your user control. For example, to change the column count property of the table layout control, from your user control, you have to expose the ColumnCount property:

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

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

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

You can also then start to use some attributes to control how your user control is displayed in Visual Studio, for example, the above can be modified like so:

[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;
        }
    }
}

This sets the default property of the entire user control to "ColumnCount", and gives the column count property a description, a default value of 2, and sets in what category it should be displayed in the designer's properties window. There is a lot more that can do with a user control to add design time support.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文