Solved

Setting up variables for controls

Posted on 2003-10-30
5
193 Views
Last Modified: 2010-04-16
Is this Possible and how do you do it?
Lets say I have 2 TextBoxes named TextBox1 and TextBox2.  Is it possible to setup a variable say txtBox and somehow define it as a textbox so when I hit period after txtBox the attributes of a textbox show up.  Then I could assign TextBox1 or TextBox2 to txtBox.

So something like this,
txtBox as TextBox
if(x == true)
{
    txtBox = TextBox1;
}
else
{
   txtBox = TextBox2;
}
txtBox.Text = "Hello";

Hope this makes sense.
0
Comment
Question by:lyptus
5 Comments
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 9650715
yes, and you code would look something like this:

TextBox txtTest = textBox1 ;
txtTest.Text = "Test";


AW
0
 

Author Comment

by:lyptus
ID: 9650747
ahhh
Don't I need to say
TextBox txtText = new TextBox();
first.
0
 
LVL 5

Assisted Solution

by:tgannetts
tgannetts earned 25 total points
ID: 9650750
Make sure you have a reference to System.Windows.Forms.dll in your project - if its a Windows application then it should already have it.

System.Windows.Forms.Textbox newtextbox = textbox1;
newtextbox = "This is a test";

If you include a reference to the Forms namespace at the top of your code ('using System.Windows.Forms;') then you can omit the 'System.Windows.Forms' part of the declaration.

Tom
0
 
LVL 44

Accepted Solution

by:
Arthur_Wood earned 75 total points
ID: 9651204
"ahhh
Don't I need to say
TextBox txtText = new TextBox();
first. "  No, you simply need to declare that txtTest is a TextBox type, and then assign the reference directly:

if you try this code:

TextBox txtTest = textBox1 ;
txtTest.Text = "Test";


you will see that it works exactly as you want it to.

you can also do this:

TextBox txtTest = textBox1 ;
txtTest.Text = "Test";
txtTest = textBox2;
txtTest.Text = "Another Text Box";

and the text will show up in the appropriate textboxes.

AW
0
 
LVL 2

Assisted Solution

by:ericsDev
ericsDev earned 25 total points
ID: 9651393
the only time you need to use:

TextBox txtBox = new TextBox();

is when you are going to define a previously undefined txtBox, I don't really see the point of using:

TextBox txtBox = TextBox1;

however it is valid because TextBox1 has already been defined and initiated (probably by the forms designer).
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question