Solved

Using Tag property of a textBox in C#

Posted on 2015-02-16
12
478 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 74

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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
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…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

707 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