473,387 Members | 1,711 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Drawing controls problem

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
Sep 10 '07 #1
3 1246
aji wrote:
[...]
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
Sep 10 '07 #2
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]
- mv*@spam.guard.caspershouse.com
"aji" <ne*********@cmandt.co.ukwrote in message
news:l-*********************@brightview.com...
>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


Sep 10 '07 #3
aji
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 [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote in
message news:Oi*************@TK2MSFTNGP06.phx.gbl...
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]
- mv*@spam.guard.caspershouse.com
"aji" <ne*********@cmandt.co.ukwrote in message
news:l-*********************@brightview.com...
>>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



Sep 10 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Hadar | last post by:
Hi, I'm getting "object is currently in use elsewhere" when I use System.Drawing.Graphics.MesureString. This is what I do: My controls use a utility class the helps it to mesure strings. To...
10
by: tshad | last post by:
I have a problem setting the background color of textbox on the fly. I tried using: applicantID.backcolor = "F6F6F6" and applicantID.backcolor = "#F6F6F6"
8
by: Benoit Martin | last post by:
I had to draw my own control because I couldn't find any control doing what I wanted it to do. This control has a grid that I need to have control over. To do that, I draw each line of the grid...
4
by: Billy Bob | last post by:
Hi, Is it possible like Visual Basic that when you are in Design mode so you can add controls etc to a windows form that you can draw on it? like draw a line or a shape? Bob *** Sent via...
9
by: davetelling | last post by:
I am not a programmer, I'm an engineer trying to make an interface to a product I'm designing. I have used C# to make a form that interrogates the unit via the serial port and receives the data. I...
3
by: kamleshgk | last post by:
Hi, I have a requirement to draw a rectangle and a line on a the container control and sometimes as i move the mouse the drawing must occur on top of user controls and other controls, which are...
0
by: Olie | last post by:
I am writting a program that uses allot of images to build up the form. It has quite allot of fader and LED controls which update live. Originally the problem I had was that it would take ages to...
1
by: kelvin.koogan | last post by:
In my application I'm doing some drawing in my OnPaint routine using Graphics->DrawRectangle. The rect bounds are constant and not scaled in anyway. One of our Chinese customers is seeing the...
2
by: Nathan Sokalski | last post by:
I am attempting to create icons for controls I have created using VB.NET by using the System.Drawing.ToolboxBitmap attribute. I have managed to do this in C# by specifying the path to the *.ico...
7
by: raylopez99 | last post by:
I have a logical drawing space much bigger than the viewport (the screen) and I'd like to center the viewport (the screen) to be at the center of the logical drawing space. After following the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.