Solved

Setting up variables for controls

Posted on 2003-10-30
5
195 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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

724 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