Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

loop through all textboxes?

Status
Not open for further replies.

litton1

Technical User
Apr 21, 2005
584
GB
hi all, i am trying to loop through all textboxes on form with
Code:
foreach (Control thisForm in this.Controls)
{
    string test = thisForm.ToString();
              
    if(test == "System.Windows.Forms.TextBox")
    {
         MessageBox.Show(test);
    }
}

where am i going wrong?
Thx

Age is a consequence of experience
 
foreach (Object c in this.Controls)
if (c is TextBox)
MessageBox.Show( (TextBox)c.Text );

 
Excellent thx, worked like a treat.

Age is a consequence of experience
 
Ok sorry but although that worked, I couldn’t actually disable a text box but did get closer by using the is keyword as you showed. Please could someone show me how to not enable all textboxes on the form. Probably should have said this in the first place. Although my code goes into the if and reaches the line "thisForm.Enabled = false;" No texboxes are disabled?
Thx

Code:
foreach (Control thisFormObject in this.Controls)
{
     string test = thisForm.ToString();

    if (thisForm is  TextBox)//"System.Windows.Forms.TextBox")
    {
       thisForm.Enabled = false;
       //MessageBox.Show(test);
    }
 }

Age is a consequence of experience
 
sorry the code was this
Code:
foreach (Control thisForm in this.Controls)
{    
    if (thisForm is TextBox)//"System.Windows.Forms.TextBox")
    {
        thisForm.Enabled = false;
    }
}

Age is a consequence of experience
 
I'm not sure of the exact c# syntax but in vb

ctype(thisform, textbox).enabled = false, so I guess something like

(textbox)thisform.enabled = false

but you will need to check that.

Basically, you need to cast the variable thisform to a tectbox before using it.


Hope this helps.

[vampire][bat]
 
just in case you have controls containing controls
public void DisableTextBoxes(Control ctrl)
{
foreach (Control childCtrl in ctrl.Controls)
{
if (childCtrl is TextBox)
{
((TextBox)childCtrl).Enabled = false;
}
DisableTextBoxes(childCtrl);
}
}
Marty
 
JurkMonkey, I wasn't that far out then [smile]. I knew it was something like that.

[vampire][bat]
 
thx guys! sweet

Age is a consequence of experience
 
So did really

Code:
thisForm.Enabled = false

didn't work, but

Code:
((TextBox)thisForm).Enabled = false

worked ??
I thought that overriding takes care of this, i.e. calling TextBox's Enabled property for the Controls of type TextBox. Please correct me if I am wrong..

------------------
When you do it, do it right.
 
You are wrong. Witch "enabled" should be called if I say

thisForm.Enabled = false;

? From Control, contorls child class, its child class....

so you have to say something about the type, since I guess there is no default method to desable control (like results do show)


NOTE TO litton1
Your thisForm name is really a bad choice for this variable name...
 
Thanks for the replies, yes on reflection this is a bad name. Thanks for pointing that out and I will amend.

Age is a consequence of experience
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top