Hi, I would like to know how get all the controls in a panel, I use the panel1.Controls.GetEnumerator(); but then I can't get the attributes (name, location, text) of that control. Is there any way that I can put all the Controls in a list/array wherein I can access the control's properties

How to get control properties?
John Minkjan
Hi,
panel1.Controls already serves as a collection of controls, put on the panel1. To run through them you could use foreach statement, something in a way of:
foreach (Control control in panel1.Controls){
if (control is TextBox)
{
TextBox textBoxControl = (TextBox)control;
string text = textBoxControl.Text;
}
else if (control is CheckBox)
{
CheckBox checkBoxControl = (CheckBox)control;
bool isChecked = checkBoxControl.Checked;
}
}
You have to cast the control to the correct control type to get access to all of its properties.
Andrej
Troy Lundin
TextBox textControl = control as TextBox;
if (textControl != null)
{
//here you can use textControl
}
Bollwerk
pmarreddy
You should read the posts and think a little before write something.
stevieT01
Hello boban, Its not matter that I wanted to pin point you. You are my senior over here so I respect you as much as I can. And Yes I read the post first then I wrote a comment.
The "is keyword" checks if an object is compatible with a specific type.
Means Andrej is check if a control in Form is check box then pasrse it. is keyword doesnot parse it but it only checks the compatibilty.
so there is only 1 casting which is in the If statement.
Next, if Andrej doesnot use is keyword then u must know what can happen.
A check box may be tried to be converted in TextBox and Exception.............
So its a s safetly measure just like 1/0 = infinity which cant be handled by any programming language so its better to check that wheter or not an object can be casted to a specific type before you try to cast it.
I hope you understand and if I'm not wrong. Just open MSDN and see what "is keyword" means.
Thanks and Best Regards,
ParanoidX2
No Andrej may be right in what hes doing, He is checking if current control is of Type CheckBox......
Joel Triemstra
See both thing together now and tell me which one is valid both are from Microsoft:
http://msdnwiki.microsoft.com/en-us/mtpswiki/scekt9xw(VS.80).aspx
http://msdnwiki.microsoft.com/en-us/mtpswiki/b79a010a-6627-421e-8955-6007e32fa808.aspx
Thanks and no Hard Feelings, You are My Senior
Cheers ;-)
DCosta
Ahhhhhhhhhhhhh, OK! Thanks a lot for explaination and teaching something to me. I took it wrong.
Right! My mind is Crystal Clear now
.
Thanks! Once again,
Best Regards,
LynnOoi
Both articles are for diferent purpose. First one shows how to use is keyword and it shows that really simple. Second one shows how to avoid duplicate casts. I see that first one did violate this rule but they are pointing of how to use is keyword.
as keyword is simular to normal cast.
TextBox textBox = (TextBox)control;
is almost equal to:
TextBox textBox = control as TextBox;
So what is the difference. Diference is that if control isn't textbox control first line will raise InvalidCastException exception, but the second one will not fail and in that case textBox will have null value.
That way we will avoid duplicate casts, because if we use first is keyword that is a cast as it is described in article i post. The second line of making cast to text box will be avoided.
At the end you can decide what is better, to have one or two casts.
If you have a posibility to get C# cookbook from O'Reilly, there is great explination when to use normal cast operator, as operator or is operator.
Kind Regards
BobInIndy
If you read posts, than why you don't read mine also
. Check this link then compare this information with what i wrote and maybe you will see that i just wanted to help as always: http://msdnwiki.microsoft.com/en-us/mtpswiki/b79a010a-6627-421e-8955-6007e32fa808.aspx
By the way i know what is keyword means but also i know what as keyword means.