Click here to Skip to main content
15,895,772 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I used Tag property to store an ArrayList of controls, it worked well. But I tried using Controls property instead, with it I can add a control to without needing an ArrayList, I wanted to use Tag property to store others. After changing I can't make my application run as before. I think Controls property may have some special notes to learn. Could you please tell me if it has something noticeable!

Plus, I am suspecting that there is some case that Controls.Add(some control) can't be done successfully!?
Thank you so much!
Posted
Updated 7-Apr-11 8:29am
v3
Comments
Sergey Alexandrovich Kryukov 7-Apr-11 14:11pm    
OP commented:

I don't think I made some error, simply I have an arraylist named a, after adding needed controls to it by a.Add(...) I do the next step by control1.Tag = a. When changing to using Controls, simply I use control1.Controls.Add(...).
I may not know how it really works!
Sergey Alexandrovich Kryukov 7-Apr-11 14:13pm    
@King Boy: please stop posting non-answers as Answer: will be deleted, no one gets e-mail notification.
Thank you.
--SA
[no name] 7-Apr-11 14:21pm    
I'm sorry, I am using my mobile phone editing and maybe I'm being so tired of what errors I have made, I intended to type it in comment.
Sergey Alexandrovich Kryukov 7-Apr-11 15:43pm    
No problem, I fixed it. Try to post accurately next time, that's it.
--SA

You can add any control to the Controls collection in a form or container.

Chances are that you made some critical errors when you changed over from use the Tag property/ArrayList to using the more appropriate collection (Controls).
 
Share this answer
 
v2
Comments
[no name] 7-Apr-11 14:13pm    
Could you please show me some the most possible error that I could make?
Thank you so much!
Sergey Alexandrovich Kryukov 7-Apr-11 14:27pm    
I think you can find what you did wrong if your read my Answer. You did not provide any code (so how any one can answer what's wrong!), but looking at the text of your Question I can tell: probably ***everything*** is wrong. Well don't provide the code, try to understand my Answer -- it is easy.
--SA
#realJSOP 7-Apr-11 15:13pm    
You zigged when you should have zagged. How the hell should I know what you messed up? You're going to have to actually be a programmer (you can google the term if you like), and run your code under the debugger to find out what's wrong.
Sergey Alexandrovich Kryukov 7-Apr-11 14:28pm    
John, you see, our friend did not get it (my 5, because you Answer is anyway correct), but by this moment I added my explanation, please see.
--SA
There is no "special notes"! You're trying to misuse properties fundamental to System.Windows.Forms.

The property System.Windows.Forms.Constrol.Controls has most fundamental side effect: the added controls actually appear on the UI (this is a way to add controls to the UI during run time). The added controls become children of the control which Controls property you're using; and the property Parent of each added control is modified accordingly as a result of adding.

You can do it the other way around: if you modify the property Parent of any control, it will immediately jump to a different parent control! If previous value of Parent was null, it will make a control appearing on screen. Automatically, the list of child controls of new parent's Controls property will be modified accordingly.

In contrast, System.Windows.Forms.Constrol.Tag does nothing. It is used just as a container for any objects, very useful as such. Typically, it is used to store some data to be looked up during some event handling. But it does not have to be data; it can be even object, for example, the delegate instance or something as sophisticated :-).

—SA
 
Share this answer
 
v2
Comments
[no name] 7-Apr-11 14:51pm    
I have just coded it back using Tag, and it is still good. The code is so long and sophisticated. But as I said, the main command I use is "Controls.Add(...)". The problem may be in there. Could you please let me ask more a question here? That is, suppose I have control1, control2, control3. First, I do control1.Controls.Add(control2), then control3.Controls.Add(control2) . Hasn't control2 been no longer in control1.Controls? (it should be only in control3.Controls, shouldn't it?)
Sergey Alexandrovich Kryukov 7-Apr-11 15:08pm    
Good question, thank you. You are right. As I explained in my Answer, it will immediately jump. Pretty cool, isn't it?

Look, why not doing the following. Spend several minutes on that: create a tiny Forms application, put 2-3 controls on the form and play with Control.Parent and Control.Controls. Useful. Use Debugger. You will see everything at once.

--SA
Sergey Alexandrovich Kryukov 7-Apr-11 15:09pm    
And then please formally accept my Answer -- it is comprehensive.
--SA
[no name] 7-Apr-11 15:15pm    
OK! What a pity when I can't use Controls instead of using Tag! It really can't help me if it acts like as you have confirmed (jump...).
Thank you!
Sergey Alexandrovich Kryukov 7-Apr-11 15:22pm    
Maybe your design is wrong that's why.
Anyway, you're welcome, thank you for accepting the Answer.
Good luck,
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900