C# 如何让 WinForms UserControl 填充其容器的大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10871565/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 15:37:01  来源:igfitidea点击:

How to make WinForms UserControl fill the size of its container

c#.netwinformsmdimultipage

提问by codingbiz

I am trying to create a multilayout main screen application. I have some buttons at the top that link to the main section of the application (e.g. management window for each entity in the Model)

我正在尝试创建一个多布局主屏幕应用程序。我在顶部有一些按钮链接到应用程序的主要部分(例如模型中每个实体的管理窗口)

Clicking any of these button displays the associated UserControl in a Panel. The Panel holds the UserControls that in turn holds the UI.

单击这些按钮中的任何一个都会在面板中显示关联的 UserControl。面板包含用户控件,而用户控件又包含 UI。

The WinForms UserControl does not have the Anchoror Dockproperty.

WinForms UserControl 没有AnchororDock属性。

I have tried setting property of UserControl

我试过设置 UserControl 的属性

AutoSize=True

And

private void ManageUsersControl_Load(object sender, EventArgs e)
{
        this.Width = this.Parent.Width;
        this.Height = this.Parent.Height;
}

But these did not work.
Note: I load this control dynamically at runtime

但这些都不起作用。
注意:我在运行时动态加载这个控件

采纳答案by IAbstract

Try setting the Dockproperty to Fill:

尝试将Dock属性设置为Fill

private void ManageUsersControl_Load(object sender, EventArgs e)
{
        this.Dock = DockStyle.Fill;
}

I would also set AutoSizeto the default, I believe is False. See how that works ...

我也会设置AutoSize为默认值,我相信是False. 看看它是如何工作的......

回答by rahuldm

UserControl1 myusercontrol = new UserControl1();
            myusercontrol.Dock = DockStyle.Fill;//Dock Prope. Fill user Control Contrainer
            TabPage myTabPage = new TabPage();//New Tab Create
            myTabPage.Text = "Wel-Come Page";//Tab Header Txt
            myTabPage.Controls.Add(myusercontrol);
            tabControl1.TabPages.Add(myTabPage);

回答by user3754071

In the resize event user control .

在调整大小事件用户控件中。

 private void MyTextBox_Resize(object sender, EventArgs e)
        {
            this.Width = textBox1.Width;
            this.Height = textBox1.Height;
        }