Solved

Using Tag property of a textBox in C#

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
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…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

809 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