我是新来Visual C#和我目前坚持如何创建一个新的窗体(与代码,而不是设计),并添加东西(即标签和文本框)到这个新的窗体。这里是我现在所拥有的:Visual C#:如何将控件添加到使用代码创建的窗体?
namespace AccountInfo
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
profileForm profile = new profileForm(); // Make new form
profile.Name = "newProfile";
profile.Text = "Add a new profile";
profile.LabelText = "test";
profile.Show(); // Display form
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
public class profileForm : Form
{
// Controls
Label label1 = new Label();
public profileForm()
{
}
public string LabelText
{
set { label1.Text = value; }
}
private void profileForm_Load(object sender, EventArgs e)
{
}
}
}
当我运行此代码时,我得到默认窗体,然后单击button1。它带来了一种新的形式,但没有任何关系。我希望有一个标签出现,但不会。我尝试了多种不同的方式(这是我最近使用的方法),我无法得到任何东西显示出来。我浏览过StackOverflow,还有一个话题出现了,但是它的解决方案对我来说并不适用。我会很感激任何洞察力:)谢谢!
编辑:我也尝试过使用构造函数。它没有帮助。
右键点击'的InitializeComponent();'和Goto定义(或F12)。你会看到表单设计器生成的代码。 – 2012-07-22 07:17:46