我想编写一个程序,一旦单击 Button3,它将创建一个新的 TextBox。由于某种原因,C# 无法识别 txtRun。它说名称 txtRun 在当前上下文中不存在。这是我的代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
textBox1.Text += "a";
}
private void button2_Click(object sender, EventArgs e)
{
textBox1.Text += "b";
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void button3_Click(object sender, EventArgs e)
{
txtRun = new TextBox();
txtRun.Name = "txtDynamic";
txtRun.Location = new System.Drawing.Point(20, 18);
txtRun.Size = new System.Drawing.Size(200, 25);
// Add the textbox control to the form's control collection
this.Controls.Add(txtRun);
}
}
}
}
}