Click here to Skip to main content
15,878,748 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
I want all my control to have new field so that I can use it in for loop.
I mean that I just want to add some meta and common data to that controls so that I can operate each control depending upon that meta data(without individually).


Like this

MyCustomType mct = new MyCustomType();
...
textBox1.MyCustomField=mct;

button1.MyCustomField=mct;
.
.


//on some event handler

for(int i =0; i<this.controls.count;i++){>
  MyCustomType mct=this.Controls[i].MyCustomField;
  if(mct.Status){
     this.Controls[i].Visible=false;
         ...
   }
}
Posted
Updated 8-Jul-11 17:23pm
v5
Comments
Prerak Patel 8-Jul-11 5:39am    
Elaborate more.
Sergey Alexandrovich Kryukov 8-Jul-11 6:08am    
You can be surprised, but this is about Reflection.Emit. See previous question by OP. I provides some answer.
--SA
OriginalGriff 8-Jul-11 5:55am    
Sorry, but that doesn't tell us what you are trying to do. A "new field" of what type? How will you use it in a for loop?
Use the "Improve question" widget to edit your question and provide better information.
Sergey Alexandrovich Kryukov 8-Jul-11 6:08am    
You can be surprised, but this is about Reflection.Emit. See previous question by OP. I provides some answer.
--SA
whitehaker 23-Jul-11 5:10am    
Thanks alot.

Please see the answers to your previous question: How to allow My class to add custom fields and properties by other class on the fly[^].

Let me tell you that you have no idea what you're trying to do. The field would be untyped and very hard to use. I would like to see how you cope with the task.

Let's simplify. First step: make all your controls custom. Yes, do not inject anything. Create some interface(s) you may need for loops and have implement it (or them) in all control classes you want to participate in you loops. When you need to do those loops, obtain an interface reference from each control and use it.

Next step: To help with the next step, I want to know what do you want to do in those loops. I bet you don't need anything special. Please make sure you not just explain it in terms of "I want", but explain your ultimate goals.

—SA
 
Share this answer
 
Comments
OriginalGriff 8-Jul-11 6:16am    
I see what you mean - I hadn't seen the earlier question. That is a very odd thing for what sounds like a newbie to want to do. I think he may well be trying to do something completely different...5!
Sergey Alexandrovich Kryukov 8-Jul-11 6:44am    
Thank you, Griff.
I suspect this is just a fantasy, nothing serious. Let's see.
--SA
Espen Harlinn 8-Jul-11 6:46am    
Good points, my 5
Sergey Alexandrovich Kryukov 8-Jul-11 6:47am    
Thank you, Espen.
--SA
whitehaker 8-Jul-11 6:55am    
I tried this way of creating custom controls, It's working to an extent.But I want it dynamically woking for all controls that implement Control Class.
This may be a silly question, but you do realize that you don't have to do anything drastic, or even complex to add information to any control at run time (or design time, though run time is more flexible).

All controls have a Tag property, which you are free to use. It is an object reference, so you can put any type of value in it, as long as you cast it back when you want to use it. Since it is part of the Control, there is a different one available for each instance.

Won't that do for your app?

C#
Button b = new Button();
b.Tag = "b";
Controls.Add(b);
TextBox t = new TextBox();
t.Tag = new List<string>() { "Hello", "goodbye" };
Controls.Add(t);
...
foreach (Control c in Controls)
   {
   TextBox t = c as Textbox;
   if (t != null)
      {
      List<string> list = t.Tag as List<string>;
      ...
      }
   }
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 8-Jul-11 6:45am    
Well, we don't know what exactly OP meant to do with the loops, but you general idea is right, my 5. (Also, usually it needs recursion, to involve all those nested panels, tab controls, etc.)
--SA
Espen Harlinn 8-Jul-11 6:48am    
Nice and simple solution, my 5
whitehaker 8-Jul-11 6:57am    
Thanks for your Answer, what I mean is like that.
my5

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