Solved

Setting up variables for controls

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
About delegates in c sharp 3 47
Replace &lt; with < 14 59
SQL Exceptions 3 46
Firing an ASP.NET event on selection of a JQuery datepicker 4 20
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

777 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