简体   繁体   中英

Showing user control in panel

I am trying to use UserControl in panel of windows form. But it is not being showed.

Method of the form:

public void SetContent(UserControl control) {
        contentPanel.Controls.Clear();
        control.Dock = DockStyle.Fill;
        contentPanel.Controls.Add(control);
        control.Show();
        contentPanel.Show();
        control.Refresh();
        contentPanel.Refresh();
        Refresh();
}

Initialization

[STAThread]
static void Main()
{
    Application.EnableVisualStyles();  
    Application.SetCompatibleTextRenderingDefault(false);
    MyForm form;
    Application.Run(form=new MyForm());
    form.SetContent(new MyControl());
}

If you want to use UserControl in panel of windows form, you can refer to the following code:

private void Form1_Load(object sender, EventArgs e)
    {
        var myControl = new UserControl1();
        contentPanel.Controls.Add(myControl);
    }

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM