在Program.cs中,我添加了:
public void displayControls()
{
TextBox tb = new TextBox();
tb.Text = "Enter";
tb.Location = new Point(300, 300);
tb.Size = new Size(300, 300);
tb.Visible = true;
tb.Show();
tb.BringToFront();
}
Run Code Online (Sandbox Code Playgroud)
然后在:
static void Main(string[] args)
{
this.displayControls();
}
Run Code Online (Sandbox Code Playgroud)
但这不起作用.我是否需要从Form.cs访问表单实例并添加到该实例?我需要做什么才能使文本框在表单中可见?
谢谢.
您似乎正在瞄准WinForm应用程序.你需要在你的Form.cs身上做到这一点,你可以在以下Form_Load事件中做到这一点:
private void Form1_Load(object sender, EventArgs e)
{
TextBox tb = new TextBox();
tb.Text = "Enter";
tb.Location = new Point(300, 300);
tb.Size = new Size(300, 300);
tb.Visible = true;
this.Controls.Add(tb); //here add it to the current form instance
}
Run Code Online (Sandbox Code Playgroud)