To clear enable/disable textboxes on form using LINQ, see the following code:
In VB.NET
Private Sub EnableTextboxes(ByVal blnenable As Boolean)
'Retrieve all textbox controls using LINQ
Dim myChildTextBoxes = From myChildControl As Control In Me.Controls _
Where TypeOf myChildControl Is TextBox _
Select myChildControl
'Loop through my TextBoxes.
For Each myChildTextBox As TextBox In myChildTextBoxes
'Clear the TextBox.
myChildTextBox.Enable = blnenable
Next
End Sub
C#
private void enabletextbox(bool blnenable)
{
//Retrieve all textbox controls using LINQ
var myChildTextBoxes = from Control myChildControl in this.Controls
where myChildControl is TextBox
select myChildControl;
// Loop through my TextBoxes.
foreach (TextBox myChildTextBox in myChildTextBoxes)
{
// enable the TextBox.
myChildTextBox.Enabled = blnenable;
}
}
For more LINQ coding tips, subscribe now
1 comments:
Hi,
Another way to do the same thing(easier IMHO) would be to just use a few chaining methods and a lambda expression
// Loop through the form TextBoxes and enable or disable each one
this.Controls
.OfType<TextBox>()
.ToList()
.ForEach(t => t.Enabled = blnenable);
Post a Comment