C# 在 Windows 窗体中以编程方式添加新的用户控件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17530323/
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-10 09:45:45  来源:igfitidea点击:

Adding new user control programmatically in windows forms

c#winformsuser-controls

提问by lamilambkin

Hey so first off i would like to point out that I know that there are several other questions about this topic up here, I have even done this exact thing myself before. I am asking on here because I do not know what my problem is.

嘿,首先我想指出的是,我知道这里还有其他几个关于这个主题的问题,我以前什至自己也做过这件事。我在这里问是因为我不知道我的问题是什么。

Here is the code where I attempt to display the new user control

这是我尝试显示新用户控件的代码

private void ValidationLabel_Click(object sender, EventArgs e)
    {
        EntrySuggestion t_ES = new EntrySuggestion();
        t_ES.Show();
        MainScreen home = new MainScreen();
        home.Show();
    }

I was trying to get the t_ES to display (which it does not) but the main Screen does. Both of these are User Controls.

我试图让 t_ES 显示(它没有)但主屏幕显示。这两个都是用户控件。

Here is the code for my EntrySuggestion User control

这是我的 EntrySuggestion 用户控件的代码

 using System;
using System.Collections;
using System.Windows.Forms;

namespace TeamManagementSystem
{
    public partial class EntrySuggestion : UserControl
    {
        private ArrayList items = new ArrayList();

        public EntrySuggestion()
        {
            InitializeComponent();
        }

        public EntrySuggestion(ArrayList i)
        {
            InitializeComponent();
            items = (ArrayList)i.Clone();
        }

        private void EntrySuggestion_Load(object sender, EventArgs e)
        {
            foreach (string item in items)
            {
                RadioButton t_RB = new RadioButton();
                t_RB.Text = item;
                ItemSuggestionTable.Controls.Add(t_RB);
            }
        }
    }
}

I do want to use the second constructor but I cannot get this to work with either. Any help would be great

我确实想使用第二个构造函数,但我也无法使用它。任何帮助都会很棒

采纳答案by Steve

You need to add your user control to the display surface of the main form (or another container already present)

您需要将您的用户控件添加到主窗体(或已经存在的另一个容器)的显示表面

    MainScreen home = new MainScreen();
    home.Show();
    EntrySuggestion t_ES = new EntrySuggestion();
    home.Controls.Add(t_ES);

回答by Pharap

Either make entry suggestion inherit the form class or add it to an existing form using form.Controls.Add. Remember, it's a user control, not a user form, thus it cannot support itself, it needs a container and ultimately it has to have a form containing it somewhere.

要么使条目建议继承表单类,要么使用 form.Controls.Add 将其添加到现有表单。请记住,它是一个用户控件,而不是一个用户窗体,因此它不能支持自己,它需要一个容器,最终它必须在某个地方有一个包含它的窗体。

回答by rajeemcariazo

Add your user control to the form:

将您的用户控件添加到表单中:

home.Controls.Add(t_ES);