Solved

Setting up variables for controls

Posted on 2003-10-30
5
189 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
Comment Utility
yes, and you code would look something like this:

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


AW
0
 

Author Comment

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

Assisted Solution

by:tgannetts
tgannetts earned 25 total points
Comment Utility
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
Comment Utility
"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
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

728 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now