![](/img/trans.png)
[英]adding User control / Page to windows xaml programmatically in WPF
[英]Adding new user control programmatically in windows forms
嘿,首先,我想指出,我知道關於此主題還有其他幾個問題,我什至以前也做過這件事。 我在這里問是因為我不知道我的問題是什么。
這是我嘗試顯示新用戶控件的代碼
private void ValidationLabel_Click(object sender, EventArgs e)
{
EntrySuggestion t_ES = new EntrySuggestion();
t_ES.Show();
MainScreen home = new MainScreen();
home.Show();
}
我試圖讓t_ES顯示(不是),但是主屏幕卻顯示了。 這兩個都是用戶控件。
這是我的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);
}
}
}
}
我確實想使用第二個構造函數,但是我無法使用它。 任何幫助都會很棒
您需要將用戶控件添加到主表單(或已經存在的另一個容器)的顯示表面上
MainScreen home = new MainScreen();
home.Show();
EntrySuggestion t_ES = new EntrySuggestion();
home.Controls.Add(t_ES);
make進入建議可以繼承表單類,也可以使用form.Controls.Add將其添加到現有表單中。 請記住,它是一個用戶控件,而不是用戶窗體,因此它無法自我支持,它需要一個容器,最終它必須具有在某處包含它的窗體。
將您的用戶控件添加到表單:
home.Controls.Add(t_ES);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.