Solved

Using Tag property of a textBox in C#

Posted on 2015-02-16
12
657 Views
Last Modified: 2015-02-17
Hi,

I am trying to use the Tag property of  a TextBox in a Windows form to store the data in the text property of that  TextBox.

I understand that the Tag property is an Object that needs an instance created object before using it.
So I tried the following code:

control myNewControl = new control();
myNewControl .Tag = TextBox.Text;

string str = myNewControl. Tag.ToString();
MessageBox.Show(str);

The above code produces an error. Can you please show me how to use the Tag correctly to implement the above?

Thank you
0
Comment
Question by:adamtrask
  • 4
  • 3
  • 2
  • +3
12 Comments
 
LVL 84

Accepted Solution

by:
Scott McDaniel (Microsoft Access MVP - EE MVE ) earned 500 total points
ID: 40612560
If you want to only show Text, you can refer to it directly:

NameOfYourControl.Tag = "Text Here"

If you want to associate an Object with the Tag property, you'd have to do that differently:

'/ assuming you have a class named "cCustomer"
customer as New cCustomer
'/ load the class
customer.load "CustomerID"
NameOfYourControl.Tag = customer
0
 
LVL 18

Expert Comment

by:UnifiedIS
ID: 40612586
What is the error and which line does it occur on?
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 40612591
>>I am trying to use the Tag property of  a TextBox in a Windows form to store the data in the text property of that  TextBox.

Reading that is isn't clear just what you are attempting to do.  The Text property and the Tag property are two different properties.
0
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 40612706
What is the error...
^^^
0
 

Author Comment

by:adamtrask
ID: 40612879
Thank you guys, but Scott McDanie comment seem to be sufficient for my purposes.
0
 

Author Closing Comment

by:adamtrask
ID: 40612881
Thank you
0
 
LVL 40
ID: 40612903
C# is case sensitive, so you need an uppercase C on the type Control.

Also, by itself, TextBox is a class, it is not an object and thus does not have properties. You should work with an existing control: TextBox1.Text, txtName.Text

Your code works perfectly once these 2 changes are made.
0
 

Author Comment

by:adamtrask
ID: 40612946
Thank you Jacques Bourgeois (James Burger).... I was working while trying to check the comments left by the experts  and in my haste did not see your comment which is very enlightening to someone with limited experience like myself.

I retried using the code I started with taking into account your observation and it worked fine like you said.
I don't know how to award you points at this late stage, but I do appreciate your comment. Thanks a lot.
0
 
LVL 40
ID: 40613404
I am not really there for the points, they simply give a little boost of pride.

And if you gave your point to a solution that suits you, then they were given to somebody who was helpful.

But you could always wait at least a day before awarding your points.

We are not paid to help you, so we are not here all day long. And we are all from different time zones. So, very often, if you give your points right after finding something that works, the best answer or the answer that suits you the most comes a little later. Or does not come at all because many experts will not take the time to present their view on a problem if points have already been awarded.

I have often been in the opposite situation, where I got the points because it happened that I was there and answered in 5 minutes, while somebody who lives in Poland or Tokyo gave a far better answer than mine half a day later.

So do not feel bad about the points. I'm glad when I can help. I also had to learn C# coming from another language. I know where one can easily tumble.
0
 
LVL 84
ID: 40613770
You can post a Request for Attention to ask the Moderators to reopen the question, and you can then award points as you wish. To do that, use the "Request Attention" link just below the original question text.
0
 
LVL 40
ID: 40613782
Thanks Scott. It's very nice of you.
0
 

Author Comment

by:adamtrask
ID: 40614345
Thanks for both of you.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to use spatial data types in SQL Server Database project 2 44
C# Service FileSystemwatcher isse 9 47
TFS 2015 Access denied. 1 30
Header Font Size in Grid View 6 27
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
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…

696 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