I’m having problem understanding in how to read data from textbox, that was dynamically created at the run time. I get an error,and here is the code.I’m sure there is just a minor change needs to be added, but I cant find what exactly. Thanks
private void button2_Click(object sender, EventArgs e)
{
//*************************************TEXTBOX***********************************************//
TextBox tbox1 = new TextBox();
tbox1.Name = "textBox8";
tbox1.Location = new System.Drawing.Point(54 + (0), 55);
tbox1.Size = new System.Drawing.Size(53, 20);
this.Controls.Add(tbox1);
tbox1.BackColor = System.Drawing.SystemColors.InactiveCaption;
tbox1.TextChanged += new EventHandler(tbox1_TextChanged);
//*************************************BUTTON***********************************************//
Button button3 = new Button();
button3.BackColor = System.Drawing.SystemColors.Highlight;
button3.Location = new System.Drawing.Point(470, 55);
button3.Name = "button3";
button3.Size = new System.Drawing.Size(139, 23);
button3.TabIndex = 0;
button3.Text = "Calculate";
this.Controls.Add(button3);
button3.UseVisualStyleBackColor = false;
button3.Click += new System.EventHandler(button3_Click);
}//button2_click
//here i want to store into variable data that I enter into textbox
double var8;
private void tbox1_TextChanged(object sender, EventArgs e)
{
TextBox tbox = sender as TextBox;
var8 = Convert.ToDouble(tbox.Text);
}
//once the button3 is clicked, i want to display calculated data into textbox
double result2;
private void button3_Click(object sender, EventArgs e)
{
result2 = var8 * 2;
//get an error saying tbox does not exist in current context(what needs to be changed?)
tbox.Text = result2.ToString();
}
within your
private void button3_Click(object sender, EventArgs e)there is no definition of tbox var! You may choose to define a global var and assigning the dynamically created textbox to it, or iterate through your Controls collection and find the according textbox!So the first possible solution is