For Programmers: Free Programming Magazines  


Home > Archive > C# > October 2005 > Controls Collection - Why can't I access the control's value???









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Controls Collection - Why can't I access the control's value???
amyathome@comcast.net

2005-10-15, 6:57 pm

I am trying to loop through all my controls on my form and grab the
value of each. I understand that controls contains other things
besides the typical textboxes, etc. but, for some reason, even if I
verify that the current control is a textbox, I cannot reference it's
text value????

Here is my code...

foreach(Control c in this.Controls[1].Controls)
{
switch(c.GetType().ToString())
{
case "Textbox":
c.Text = "test";
break;

case "DropDown":
break;
}
}

I find all kinds of examples for this in VB but not C#. Any ideas?

Alex Koltun

2005-10-16, 6:57 pm

Hi,

Just cast the control to TextBox, like that:

foreach(Control c in this.Controls[1].Controls)
{
switch(c is TextBox)
{
((TextBox)c).Text = "test";
}
}

Hope it helps.

amyathome@comcast.net

2005-10-16, 9:56 pm


Alex Koltun wrote:
> Hi,
>
> Just cast the control to TextBox, like that:
>
> foreach(Control c in this.Controls[1].Controls)
> {
> switch(c is TextBox)
> {
> ((TextBox)c).Text = "test";
> }
> }
>
> Hope it helps.


Thank you Alex, I will give this a shot!

Bruce Wood

2005-10-17, 6:59 pm

....which is why some of us prefer:

foreach(Control c in this.Controls[1].Controls)
{
Textbox t = c as TextBox;
if (t != null)
{
t.Text = "test";
}
}

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com