Drawing controls problem

  • Thread starter Thread starter aji
  • Start date Start date
A

aji

I hope this is the place for a newbie question. If not, could someone
re-direct me please?

My problem is this:
You can put text into a textbox on a form with a simple TextBox.Text =
"Hello";

But what if you programatically draw the textbox ?

I have two buttons on a form The first draws a text box on the form. This
works fine. The second is supposed to put text into that box.
But it doesn't. I get various error messages as I try all sort of tricks to
get it to work.
EG.
Error 1 The name 'cellBox' does not exist in the current context

What am I missing ?

The code is:
private void button1_Click(object sender, EventArgs e)

{

TextBox cellBox = new TextBox();

Form1.ActiveForm.Controls.Add(cellBox);

}

private void button2_Click(object sender, EventArgs e)

{

string s = "Hello";

cellBox.Text = s;

}

Many thanks

aji
 
aji said:
[...]
private void button1_Click(object sender, EventArgs e)

{

TextBox cellBox = new TextBox();

Form1.ActiveForm.Controls.Add(cellBox);

}

You store the reference in the local variable "cellBox" here.
private void button2_Click(object sender, EventArgs e)

{

string s = "Hello";

cellBox.Text = s;

}

There is no variable "cellBox" here. That variable was local to the
previous method, and can only be used there.

Now, the TextBox instance itself does still exist. So the question is,
how to get at it.

The easiest way would be to move the declaration of "cellBox" out of the
button1_Click() method and put it into the class:

TextBox cellBox;

private void button1_Click(object sender, EventArgs e)

{

cellBox = new TextBox();

Form1.ActiveForm.Controls.Add(cellBox);

}

private void button2_Click(object sender, EventArgs e)

{

string s = "Hello";

cellBox.Text = s;

}

Alternatively, you could provide a value for the Name property when you
instantiate the TextBox, and then use the Controls.Find() method to look
up the instance later based on that name.

Note, of course, that in the code above if you click the first button
more than once, you get new instances of the control, but only the
most-recently-created instance is affected by the code in the second
method. All of this code looks more like "getting to know .NET" sort of
code rather than actual application code, so presumably you would
address that issue when moving to an actual application scenario.

Pete
 
aji,

Right, beause cellBox is a local variable in the button1_Click method.
You need to store it in a field on the class level, like so:

private Button cellBox = null;

private void button1_Click(object sender, EventArgs e)
{
cellBox = new TextBox();

// I am assuming that this code is in the form that the button is hosted
on.
this.Controls.Add(cellBox);
}

private void button2_Click(object sender, EventArgs e)
{
string s = "Hello";
cellBox.Text = s;
}

Just be careful, if you click on button1 another time, it will add a
second textbox, and you will not have access to the first one through the
cellBox field (although you can still get it from the Controls collection).
 
Thanks guys.
Acutally I had written quite a large program that all appears to work except
this bit. It never ocurred to me that a control could be a variable.
Most grateful to you.
Thanks again
Nicholas Paldino said:
aji,

Right, beause cellBox is a local variable in the button1_Click method.
You need to store it in a field on the class level, like so:

private Button cellBox = null;

private void button1_Click(object sender, EventArgs e)
{
cellBox = new TextBox();

// I am assuming that this code is in the form that the button is
hosted on.
this.Controls.Add(cellBox);
}

private void button2_Click(object sender, EventArgs e)
{
string s = "Hello";
cellBox.Text = s;
}

Just be careful, if you click on button1 another time, it will add a
second textbox, and you will not have access to the first one through the
cellBox field (although you can still get it from the Controls
collection).


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


aji said:
I hope this is the place for a newbie question. If not, could someone
re-direct me please?

My problem is this:
You can put text into a textbox on a form with a simple TextBox.Text =
"Hello";

But what if you programatically draw the textbox ?

I have two buttons on a form The first draws a text box on the form. This
works fine. The second is supposed to put text into that box.
But it doesn't. I get various error messages as I try all sort of tricks
to get it to work.
EG.
Error 1 The name 'cellBox' does not exist in the current context

What am I missing ?

The code is:
private void button1_Click(object sender, EventArgs e)

{

TextBox cellBox = new TextBox();

Form1.ActiveForm.Controls.Add(cellBox);

}

private void button2_Click(object sender, EventArgs e)

{

string s = "Hello";

cellBox.Text = s;

}

Many thanks

aji
 
Back
Top