在 Windows 窗体中以编程方式添加新的用户控件 [英] Adding new user control programmatically in windows forms

查看:24
本文介绍了在 Windows 窗体中以编程方式添加新的用户控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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();
    }

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

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.

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

Here is the code for my EntrySuggestion User control

 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

推荐答案

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

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);

这篇关于在 Windows 窗体中以编程方式添加新的用户控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆