Solved

Setting up variables for controls

Posted on 2003-10-30
5
194 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
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…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

732 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