Wednesday, July 15, 2009

Get all controls inside window form and set its value

This code cleans/set the value of textbox control to space("") inside the formwindow and inside the groupbox control.

VB.NET
For Each octl As Control In Me.Controls
If (TypeOf octl Is GroupBox) Then
Dim gbox As GroupBox = DirectCast(octl, GroupBox)
For Each ictl As Control In gbox.Controls
If (TypeOf ictl Is TextBox) Then
Dim itxt As TextBox = DirectCast(ictl, TextBox)
itxt.Text = ""
End If
Next
End If

If (TypeOf octl Is TextBox) Then ' textbox outside the groupbox
Dim txt As TextBox = DirectCast(octl, TextBox)
txt.Text = ""
End If

Next

C#
foreach (Control octl in this.Controls)
{
if ((octl is GroupBox))
{
GroupBox gbox = (GroupBox)octl;
foreach (Control ictl in gbox.Controls)
{
if ((ictl is TextBox))
{
TextBox itxt = (TextBox)ictl;
itxt.Text = "";
}
}
}
if ((octl is TextBox))
{
TextBox txt = (TextBox)octl;
txt.Text = "";
}
}

No comments:

Post a Comment