C#用代码生成一个窗体在其中有一个按钮和一个文本框,如何在按钮的Click事件中访问文本框的属性
publicpartialclassForm1:Form{publicForm1(){InitializeComponent();}privatevoidbutton1_...
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)//----------主窗体中的按钮单击事件
{
Form fm1 = new Form();
fm1.StartPosition = FormStartPosition.CenterScreen;
fm1.Size = new System.Drawing.Size(400, 160);
fm1.Text = "测试";
Button button = new Button();
button.Name = "button";
button.Text = "登陆";
button.Location = new Point(10, 10);
button.Size = new Size(24, 57);
button.Click += button_Click;
TextBox tbd = new TextBox();
tbd.Name = "TextBox";
tbd.Text = "测试文本";
tbd.Location = new Point(50, 10);
tbd.Size = new Size(24, 57);
fm1.Controls.Add(tbd);
fm1.Controls.Add(button);
}
void button_Click(object sender, EventArgs e)
{
//这里如何访问文本框的text属性
}
} 展开
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)//----------主窗体中的按钮单击事件
{
Form fm1 = new Form();
fm1.StartPosition = FormStartPosition.CenterScreen;
fm1.Size = new System.Drawing.Size(400, 160);
fm1.Text = "测试";
Button button = new Button();
button.Name = "button";
button.Text = "登陆";
button.Location = new Point(10, 10);
button.Size = new Size(24, 57);
button.Click += button_Click;
TextBox tbd = new TextBox();
tbd.Name = "TextBox";
tbd.Text = "测试文本";
tbd.Location = new Point(50, 10);
tbd.Size = new Size(24, 57);
fm1.Controls.Add(tbd);
fm1.Controls.Add(button);
}
void button_Click(object sender, EventArgs e)
{
//这里如何访问文本框的text属性
}
} 展开
展开全部
因为不在同一个实例中.也没关联起来.
最简单的解决方案就是定义为全局.
例如:
private Form fm1;
private Button button;
private TextBox tbd;
private void button1_Click(object sender, EventArgs e)
{
fm1 = new Form();
fm1.StartPosition = FormStartPosition.CenterScreen;
fm1.Size = new System.Drawing.Size(400, 160);
fm1.Text = "测试";
button = new Button();
button.Name = "button";
button.Text = "登陆";
button.Location = new Point(10, 10);
button.Size = new Size(24, 57);
button.Click += button_Click;
tbd = new TextBox();
tbd.Name = "TextBox";
tbd.Text = "测试文本";
tbd.Location = new Point(50, 10);
tbd.Size = new Size(24, 57);
fm1.Controls.Add(tbd);
fm1.Controls.Add(button);
fm1.Show();
}
void button_Click(object sender, EventArgs e)
{
string AA = tbd.Text;
}
当然还有别的方法.例如反射.
不知道需求.这只提供一个简单的解决方案.仅供产考.
最简单的解决方案就是定义为全局.
例如:
private Form fm1;
private Button button;
private TextBox tbd;
private void button1_Click(object sender, EventArgs e)
{
fm1 = new Form();
fm1.StartPosition = FormStartPosition.CenterScreen;
fm1.Size = new System.Drawing.Size(400, 160);
fm1.Text = "测试";
button = new Button();
button.Name = "button";
button.Text = "登陆";
button.Location = new Point(10, 10);
button.Size = new Size(24, 57);
button.Click += button_Click;
tbd = new TextBox();
tbd.Name = "TextBox";
tbd.Text = "测试文本";
tbd.Location = new Point(50, 10);
tbd.Size = new Size(24, 57);
fm1.Controls.Add(tbd);
fm1.Controls.Add(button);
fm1.Show();
}
void button_Click(object sender, EventArgs e)
{
string AA = tbd.Text;
}
当然还有别的方法.例如反射.
不知道需求.这只提供一个简单的解决方案.仅供产考.
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询