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

Adding new user control programmatically in windows forms(在 Windows 窗体中以编程方式添加新的用户控件)
本文介绍了在 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 窗体中以编程方式添加新的用户控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)