本文介绍了Winforms - 在面板内填充用户控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 winforms 的新手,我想做的是一个简单的表单:

I am new to winforms, what i am trying to do is a simple form:

  • 在我的表单中有一个面板.

  • In my form there is a panel.

面板链接到用户控件

用户控件应该填满面板.

The user control should fill the panel.

这是我的表格:

public Form1()
    {
        InitializeComponent();

        UserControl1 userControl = new UserControl1();

        panel1.Controls.Add(userControl);
        userControl.Anchor = ((AnchorStyles)((((AnchorStyles.Top | AnchorStyles.Bottom)
     | AnchorStyles.Left) | AnchorStyles.Right)));
        userControl.Dock = DockStyle.Fill;
    }

不工作 -> 控件不拉伸

Not working -> the control doest stretch

推荐答案

试试这个,

    public Form1()
    {
        InitializeComponent();
        panel1.Dock = DockStyle.Fill;
        UserControl1 userControl = new UserControl1();
        userControl.Dock = DockStyle.Fill;
        panel1.Controls.Add(userControl);

    }

userControl.Dock = DockStyle.Fill; 应该在添加到面板 1 之前调用.

userControl.Dock = DockStyle.Fill; should call before Add to panel1.

您还需要在 UserControl 中设置控件的 Anchor 属性,使其根据面板中的 usercontrol 拉伸进行拉伸.

You also need to set Anchor property of controls inside UserControl to stretch it based on usercontrol stretch in panel.

喜欢.

Public Sub UserControl1()

        //This call is required by the designer.
        InitializeComponent();

        //Add any initialization after the InitializeComponent() call.
        Label1.Anchor = AnchorStyles.Top;
        Label2.Anchor = AnchorStyles.Right;
        Label4.Anchor = AnchorStyles.Bottom;
        Label3.Anchor = AnchorStyles.Left;
    End Sub

注意: userControl.Dock = DockStyle.Fill; 只拉伸用户控件本身,而不是用户控件内部的控件.要拉伸用户控件的控件,您需要相应地设置 Dock 以及 Anchor 属性.

Note: userControl.Dock = DockStyle.Fill; stretch only usercontrol it self not controls inside user control. To stretch controls of user control you need to set Dock as well as Anchor property accordingly.

这篇关于Winforms - 在面板内填充用户控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-03 23:30
查看更多