Link to home
Start Free TrialLog in
Avatar of adamtrask
adamtrask

asked on

Using Tag property of a textBox in C#

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
ASKER CERTIFIED SOLUTION
Avatar of Scott McDaniel (EE MVE )
Scott McDaniel (EE MVE )
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of UnifiedIS
UnifiedIS

What is the error and which line does it occur on?
>>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.
What is the error...
^^^
Avatar of adamtrask

ASKER

Thank you guys, but Scott McDanie comment seem to be sufficient for my purposes.
Thank you
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.
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.
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.
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.
Thanks Scott. It's very nice of you.
Thanks for both of you.