Click here to Skip to main content
15,888,802 members
Please Sign up or sign in to vote.
3.00/5 (3 votes)
See more:
I want to assign some object to a Tag property (such as of a checkbox) at design time, I typed something like "object name" in the Tag field in the properties window, but it seems that the compiler understands it as a string not the real object I want to assign to that Tag.
For example, if I want to assign "label1" to checkBox1.Tag, by coding I can do easily as "checkBox1.Tag = label1".
That makes me do the same when designing, I type exactly "label1" in the field "Tag" of the checkBox1 properties window. But the compiler sees it as the string "label1". Haha, it's so funny. Is there any way to do what I want at design time. It's so important to me, if I have to do that by coding, I think I don't need to assign anything to the Tags because I have so many checkBoxes and other controls that need to be assigned some objects to their Tag. The difficulty is I can know what object should be assigned to what control's Tag easily in design windows but not easily when typing assigning code lines to do instead.
I have about 200 controls and all is mixed. (there is no certain order in their names).
Could you please help me!
Thank you so much!
Posted
Updated 4-Apr-11 3:07am
v2
Comments
Toli Cuturicu 4-Apr-11 12:17pm    
Very bad design practice. Don't do it anymore.
Sergey Alexandrovich Kryukov 4-Apr-11 22:20pm    
Toli, you're absolutely right.
--SA

I don't think you can.

If your UI cross-dependencies are that complicated, though, you probably want to look at data binding and the MVC pattern, or at least a variant of it. What you are doing will become a maintenance nightmare for you or the person after you.
 
Share this answer
 
Comments
[no name] 4-Apr-11 10:05am    
Thank you, I'm such a newbie that I don't care about what called "maintenance", so there won't be any nightmare for anyone even me, hihi!
I think I should tell more about my idea, suppose that I have control1, control2, control3, when clicking on control1, control11 and control12 should be invisible. When clicking on control2, control21 should be invisible, when clicking on control3, control31 ... control35 should be invisible. I can simply do that by writing code for each control among control1, control2, control3. But infact I have more than 3 controls, so I want to associate their Click events to only method over eventhandler, in that method, I will browse all Tags (where contain control11, control12, ... control35 which need to be hidden) by using foreach(control c in ...), in the foreach's body, I simply use a command like "c.Visible = false".
Is there any better way than mine?
Could you please tell me more about how to use "data binding" to contain an object or an array of objects?
Thank you so much!
BobJanova 4-Apr-11 10:19am    
This reply makes more sense of your original question.

What I have done in a similar situation is create a single method that updates the visibility (actually, in my case, enabled, but the idea is the same) of all the controls:

void UpdateVisibility(){
control11.Visible = checkbox1.Checked;
control12.Visible = checkbox1.Checked;

control21.Visible = checkbox2.Checked;
control21.Visible = checkbox2.Checked;

// ... etc
}

... and then set the CheckedChanged event handler of all the check boxes to a passthrough method that calls this one:
private void CheckBoxCheckedChanged(object sender, EventArgs e){ UpdateVisibility(); }

This puts all the dependencies in one clear and obvious place so you won't lose them.

Data binding allows you to bind properties of UI objects to data-layer controls, so that you don't mix business and UI logic. As you progress in programming you'll learn why that is a good idea! It sounds as though the visibility stuff here is just a UI thing though and therefore it's okay to have code directly bound to event handlers.

Remember that if you're changing visibility, you might have issues with resizing a form or panel. Look at the AutoSize property if this is a concern.

(Edit: it appears <pre> tags don't work in these.)
[no name] 4-Apr-11 11:13am    
Thank you, but I have many controls need to be hidden.
The normal way may like this:
public void oncheckboxclick(object sender, EventArgs e)
{
CheckBox chk = (CheckBox) sender;
if(chk.name == "checkBox1")
control11.Visible = control12.Visible = checkBox1.Checked;
if(chk.name == "checkBox2")
control21.Visible = checkBox2.Checked;
if(chk.name == "checkBox3")
{
control31.Visible = checkBox3.Checked;
...
}
}
The above codes work well if I have only some controls. But it needs so much effort to do for many controls. The following codes are what I want to use:
public void oncheckboxclick(object sender, EventArgs e)
{
foreach(Control c in ((Control[]) ((CheckBox) sender).Tag))
c.Visible = ((CheckBox) sender).Checked;
}
That will work well even I have thousands of controls like checkBox1, checkBox2...
So if I don't store control need to be hidden in Tags, I must to do that by using another instead.
Could you please post here a concrete code about using Data Binding as you forementioned? I don't need it right now but whenever you have, please post it here as an example to help me understand what called "Data Binding".
Thank you so much!
BobJanova 4-Apr-11 12:21pm    
Thousands of controls? Yeah, there's your problem. Group them into logical groups as Albin suggests below, or put them on separate forms or panels.

Here is a good article about data binding from a WinForms perspective.
[no name] 4-Apr-11 11:20am    
Omg, all "public"s above should be "private". I'm using mobile phone editing comments so I can't highlight what should be highlighted to make it more legible. So please pardon me and try to read and understand it.
Thank you!
Putting anything in a Tag by the designed is the bad abuse if this property. The intended purpose of this property is run-time. Over-using of the Designer is a very bad style and would be a big mistake, strategically. Don't do it!

—SA
 
Share this answer
 
Comments
Albin Abel 4-Apr-11 23:06pm    
:)
[no name] 5-Apr-11 1:43am    
If Tag is intended to use at runtime, why .NET developers place it in the properties window and almost things in that window can be edited at design time?
Thank you!
Sergey Alexandrovich Kryukov 5-Apr-11 3:15am    
Simple. Because PropertyGrid is universal control; it shows all. Also you "why" question is not a correct justification for using anything at all. There are many accessible things which should not be used. For example, there is Registry class, but using Registry for application settings is highly discourage. Your case is some abuse: there are many cases when Tag is useful. In code, not Designer code.

Also, consider this. Designer is visual. It's good for rapid build of layouts. Everything beyond should be coded. For example, I advice avoid setting any events by clicking in Designer: the resulting code a very inconvenient for support.

Thank you for accepting this Answer.
Good luck, call again,
--SA
[no name] 5-Apr-11 3:32am    
No, you have deserved it! I accept all answers which are helpful for me! Thank you!
Sergey Alexandrovich Kryukov 5-Apr-11 13:14pm    
Thank you for accepting this Answer and your understanding.
--SA
Why can't you group your controls using panels or other container controls. Then assign that panel name to your master (controller) control. In the event of the controller control (checkbox in your case) you can simply make invisible the container control. You may use the FindControl method to get the control from the name. No need to go through each control.

This is what we generally do. if there are many number of controls group it in to logic groups which helps maintainability and help is good presentation.
 
Share this answer
 
Comments
[no name] 4-Apr-11 11:28am    
What a good idea! But can I use that way if I don't want any visible border between groups?
Plus, in fact, I don't only want to change the visiblity but also I want to do more other actions such as initializing the list of items for a comboBox!
What else can I do?
Thank you so much!
BobJanova 4-Apr-11 12:23pm    
To the first part, yes, you can set the BorderStyle of a panel to be None.
[no name] 4-Apr-11 11:39am    
For initializing a new item list for combobox, for example, when control1 being clicked, it should initialize a new item list for comboBox1, the same for control2 and comboBox2, control3 and comboBox3...
I can write code for each one, but I have many ones with various corresponding comboBoxes. If I contain these comboBoxes somewhere (that I want is "Tag"), I won't have to care the names of comboBoxes and using "if" statements to check if "sender" is control1 or control2 or any other.
Maybe I'm wrong in some points but I hope you understand my idea. It seems to like polymorphism but,in fact, it doen't.
Thank you!
Albin Abel 4-Apr-11 14:45pm    
Of course you have to write codes :). But again I suggest grouping here as well in the form of user controls. You encapsulate each group and its business logics to an user control. In that way you can achieve a hierarchical list of groups. Now you may have xml file describing this grouping structure. That will help you identify the controls in the group also the hierarchical structure. Don't you think these kind of meta data helps you to manage things once they are well organized.
Sergey Alexandrovich Kryukov 4-Apr-11 22:23pm    
Albin, unfortunately I cannot see how can it help. Tag has it uses.
Please see my answer.
--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