请问在C#中,如何调用通过动态方法添加的textbox的text值?
publicForm1(){InitializeComponent();}publicvoidTXT(){TextBoxtxt1;txt1=newTextBox();tx...
public Form1()
{
InitializeComponent();
}
public void TXT()
{
TextBox txt1;
txt1 = new TextBox();
txt1.Name = "txt1";
txt1.Location = new Point(20, 20);
txt1.Size=new Size(50,20);
this.Controls.Add(txt1);
TextBox txt2;
txt2 = new TextBox();
txt2.Name = "txt2";
txt2.Text = txt1.Text;
txt2.Location = new Point(100, 20);
txt2.Size = new Size(50, 20);
Console.WriteLine(txt2.Text);
this.Controls.Add(txt2);
TextBox txt3;
txt3 = new TextBox();
txt3.Name = "txt3";
txt3.Location = new Point(180, 20);
txt3.Size = new Size(50, 20);
this.Controls.Add(txt3);
Label lab1;
lab1 = new Label();
lab1.Location = new Point(75,25);
lab1.Size = new Size(50, 25);
lab1.Text = "+";
this.Controls.Add(lab1);
Label lab2;
lab2 = new Label();
lab2.Location = new Point(160, 25);
lab2.Size = new Size(50, 25);
lab2.Text = "=";
this.Controls.Add(lab2);
}
public void addbtn()
{
Button btncaculate;
btncaculate = new Button();
btncaculate.Location = new Point(50, 50);
btncaculate.Text = "计算";
btncaculate.Size = new Size(50, 50);
this.Controls.Add(btncaculate);
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
TXT();
addbtn();
/*在这儿如何取通过TXT方法添加的文本框里的值
而且在此处运行之前已经在相应文本框里输入了text值
我想要进行加法计算*/
}
一下是点击开始运行口的窗口 展开
{
InitializeComponent();
}
public void TXT()
{
TextBox txt1;
txt1 = new TextBox();
txt1.Name = "txt1";
txt1.Location = new Point(20, 20);
txt1.Size=new Size(50,20);
this.Controls.Add(txt1);
TextBox txt2;
txt2 = new TextBox();
txt2.Name = "txt2";
txt2.Text = txt1.Text;
txt2.Location = new Point(100, 20);
txt2.Size = new Size(50, 20);
Console.WriteLine(txt2.Text);
this.Controls.Add(txt2);
TextBox txt3;
txt3 = new TextBox();
txt3.Name = "txt3";
txt3.Location = new Point(180, 20);
txt3.Size = new Size(50, 20);
this.Controls.Add(txt3);
Label lab1;
lab1 = new Label();
lab1.Location = new Point(75,25);
lab1.Size = new Size(50, 25);
lab1.Text = "+";
this.Controls.Add(lab1);
Label lab2;
lab2 = new Label();
lab2.Location = new Point(160, 25);
lab2.Size = new Size(50, 25);
lab2.Text = "=";
this.Controls.Add(lab2);
}
public void addbtn()
{
Button btncaculate;
btncaculate = new Button();
btncaculate.Location = new Point(50, 50);
btncaculate.Text = "计算";
btncaculate.Size = new Size(50, 50);
this.Controls.Add(btncaculate);
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
TXT();
addbtn();
/*在这儿如何取通过TXT方法添加的文本框里的值
而且在此处运行之前已经在相应文本框里输入了text值
我想要进行加法计算*/
}
一下是点击开始运行口的窗口 展开
3个回答
展开全部
你的计算按钮为什么不用而要用ListBox的事件呢?用按钮来触发计算事件,取2个文本框内容和ListBox的选项,然后判断后把结果放在第三个文本框里
你的addbtn方法如果无限次的用,那窗体不是会重复添加无数个文本框了吗
你的addbtn方法如果无限次的用,那窗体不是会重复添加无数个文本框了吗
追问
我写这个程序的目的是想实现当点击listbox里的选项时(加法 减法),再显示出文本框和“计算”按钮,然后再在前两个文本框里输入正确的数值,再点击“计算”按钮进行相应计算,结果显示在第三个文本框里。(当然我还没写这些代码,因为我不知道该如何取动态添加的文本框里的Text值)。要是可以的话,请您帮忙写一下。谢谢
追答
在addbtn代码中添加动态的TextBox(第三个txtbox) name属性设置为txtResult,给按钮注册click事件,内容为TextBox txtResult= (TextBox)this.Controls.Find("txtResult", true);就可以获取到动态添加的文本框对象了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
public void TXT(string txt1Value,string txt2Value,string lab1Value)
{
Control[] controls = this.Controls.Find("txt1", false);
if (controls.Length != 0)
{
this.Controls.RemoveByKey("txt1");
}
controls = this.Controls.Find("txt2", false);
if (controls.Length != 0)
{
this.Controls.RemoveByKey("txt2");
}
controls = this.Controls.Find("txt3", false);
if (controls.Length != 0)
{
this.Controls.RemoveByKey("txt2");
}
controls = this.Controls.Find("lab1", false);
if (controls.Length != 0)
{
this.Controls.RemoveByKey("lab1");
}
TextBox txt1;
txt1 = new TextBox();
txt1.Name = "txt1";
txt1.Location = new Point(20, 20);
txt1.Size = new Size(50, 20);
txt1.Text = txt1Value;
this.Controls.Add(txt1);
TextBox txt2;
txt2 = new TextBox();
txt2.Name = "txt2";
txt2.Text = txt1.Text;
txt2.Location = new Point(100, 20);
txt2.Size = new Size(50, 20);
txt2.Text = txt2Value;
Console.WriteLine(txt2.Text);
this.Controls.Add(txt2);
TextBox txt3;
txt3 = new TextBox();
txt3.Name = "txt3";
txt3.Location = new Point(180, 20);
txt3.Size = new Size(50, 20);
this.Controls.Add(txt3);
Label lab1;
lab1 = new Label();
lab1.Name = "lab1";
lab1.Location = new Point(75, 25);
lab1.Size = new Size(50, 25);
lab1.Text = lab1Value;
this.Controls.Add(lab1);
Label lab2;
lab2 = new Label();
lab2.Location = new Point(160, 25);
lab2.Size = new Size(50, 25);
lab2.Text = "=";
this.Controls.Add(lab2);
}
public void addbtn()
{
Button btncaculate;
btncaculate = new Button();
btncaculate.Location = new Point(50, 50);
btncaculate.Text = "计算";
btncaculate.Size = new Size(50, 50);
this.Controls.Add(btncaculate);
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
string item = this.listBox1.SelectedItem as string;
if ("加法" == item)
{
TXT("1", "2", "+");
}
else
{
TXT("3", "4", "-");
}
addbtn();
}
给个采纳吧,代码都给你写了
{
Control[] controls = this.Controls.Find("txt1", false);
if (controls.Length != 0)
{
this.Controls.RemoveByKey("txt1");
}
controls = this.Controls.Find("txt2", false);
if (controls.Length != 0)
{
this.Controls.RemoveByKey("txt2");
}
controls = this.Controls.Find("txt3", false);
if (controls.Length != 0)
{
this.Controls.RemoveByKey("txt2");
}
controls = this.Controls.Find("lab1", false);
if (controls.Length != 0)
{
this.Controls.RemoveByKey("lab1");
}
TextBox txt1;
txt1 = new TextBox();
txt1.Name = "txt1";
txt1.Location = new Point(20, 20);
txt1.Size = new Size(50, 20);
txt1.Text = txt1Value;
this.Controls.Add(txt1);
TextBox txt2;
txt2 = new TextBox();
txt2.Name = "txt2";
txt2.Text = txt1.Text;
txt2.Location = new Point(100, 20);
txt2.Size = new Size(50, 20);
txt2.Text = txt2Value;
Console.WriteLine(txt2.Text);
this.Controls.Add(txt2);
TextBox txt3;
txt3 = new TextBox();
txt3.Name = "txt3";
txt3.Location = new Point(180, 20);
txt3.Size = new Size(50, 20);
this.Controls.Add(txt3);
Label lab1;
lab1 = new Label();
lab1.Name = "lab1";
lab1.Location = new Point(75, 25);
lab1.Size = new Size(50, 25);
lab1.Text = lab1Value;
this.Controls.Add(lab1);
Label lab2;
lab2 = new Label();
lab2.Location = new Point(160, 25);
lab2.Size = new Size(50, 25);
lab2.Text = "=";
this.Controls.Add(lab2);
}
public void addbtn()
{
Button btncaculate;
btncaculate = new Button();
btncaculate.Location = new Point(50, 50);
btncaculate.Text = "计算";
btncaculate.Size = new Size(50, 50);
this.Controls.Add(btncaculate);
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
string item = this.listBox1.SelectedItem as string;
if ("加法" == item)
{
TXT("1", "2", "+");
}
else
{
TXT("3", "4", "-");
}
addbtn();
}
给个采纳吧,代码都给你写了
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询