在WinForm中显示UserControl

时间:2013-02-13 18:44:27

标签: c#

我正在开发一个继承自Form类的类库,然后在Gui应用程序中显示此表单。现在我需要将类库更改为UserControl

如何在另一个项目的表单中显示它?提前感谢您的建议。

IFaceForm是InterfaceLibrary中的用户控件。

using InterfaceLibrary;

namespace MxlInterface
{  
    public partial class IFaceConn : Form
    { 
        iFaceForm Interface = new iFaceForm();

        //size of the parent form is 881,514
        //use these variables to change the position of the "Interface Form"
        int xlocation = 0;
        int ylocation = 0;

        public IFaceConn()
        {
            InitializeComponent();

            //Interface.StartPosition = FormStartPosition.Manual;
            Interface.Location = new Point(xlocation, ylocation);
            //Interface.MdiParent = this;

            Interface.Show();
        }
    }
}

1 个答案:

答案 0 :(得分:4)

您可以通过执行以下操作直接将其添加到表单中:

MyUserControl myControl = new MyUserControl();
Controls.Add(myControl);

在表单上放置一个Panel作为用户控件的占位符也很有用,然后将用户控件添加到该面板:

MyUserControl myControl = new MyUserControl();
myControlWrapperPannel.Controls.Add(myControl;