PDA

View Full Version : Fast access for your controls (C# code).



MrSeanKon
03-13-2007, 12:16 AM
The original thread is here (http://www.aoaforums.com/forum/programming-and-assembly-language/39988-gkr-how-can-i-use-loop.html)
There you can see the source code and tweak it!
Of course it is not the only solution; post other one here.
Moreover you may post questions/comments etc....
Hope it helps :)

ahmad
03-13-2007, 12:47 AM
Wouldn't it be many times better to just keep textboxes in a textbox list and just iterate through that.

I don't know if it is possible to modify the generated code, otherwise you would have to do it manually.

MrSeanKon
03-14-2007, 01:54 AM
WOW thanks for tip man!
I will play with this in the future versions just to reduce the code size.

otherwise you would have to do it manually.This will be too boring for some forms but if the result is a faster and more managed code IMHO it is necessary. :)

kromosto
03-15-2007, 03:28 AM
every control added to the form by designers is added in to the controlcollection of the form named Controls. you can get every control of the form by this collection. now there are labels buttons textbox are all in this control so if you want to get only textboxes you can use try catch of object classes GetType method to get only textboxes.

EX: this codes itterates over all the controls in the form but inside the try block temp is a text box of the form.



foreach (object xxx in [FormsInstaceName].Controls)
{
try
{
TextBox temp = (TextBox)xxx;
}
catch(Exception ex){}
}

ahmad
03-16-2007, 12:57 AM
every control added to the form by designers is added in to the controlcollection of the form named Controls. you can get every control of the form by this collection. now there are labels buttons textbox are all in this control so if you want to get only textboxes you can use try catch of object classes GetType method to get only textboxes.

EX: this codes itterates over all the controls in the form but inside the try block temp is a text box of the form.

But this is extremely inefficient if you have just a few hundred controls. You go through a few hundred just to get 10 or 20.

kromosto
03-16-2007, 01:09 AM
But this is extremely inefficient if you have just a few hundred controls. You go through a few hundred just to get 10 or 20.

yes if you have lots of controls this is totally inefficient. actually best way making an array of the controls that you want to reach faster (not using designer) or design a custom control that registers it self automatically to a collection (it is also ineffient to register all the controls that you want if you have again lots of them) as you said. but if there are not much controls this is the best way i think.

MrSeanKon
03-16-2007, 01:36 AM
kromosto I can create dynamically at runtime all necessary textboxes, so I could easily at creation put them in an array and use this for reading these textboxes.
But I am not a professional programmer like you I will tweak the code in the future versions.
BTW thanks for the tip.

kromosto
03-16-2007, 04:58 AM
i would be happy to help you as much as i can. good luck on your application.