Solved

Using Tag property of a textBox in C#

Posted on 2015-02-16
12
521 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
 
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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Not showing JavaScript in the list 5 38
Error in script 11 47
Facial recognition to look through the whole database for a person 3 43
Variable Event ? 3 23
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

920 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

15 Experts available now in Live!

Get 1:1 Help Now