Geeks With Blogs
Code.Blog Code Talk for the Game Developer

Here's an example of looping through Form textboxes using VB LINQ and disabling the boxes:

Dim formBoxes = From formControls In Form.Controls _
Where formControls.GetType() Is GetType(TextBox) _
Select formControls

 

For Each oBox In formBoxes

CType(oBox, TextBox).Enabled = False

Next

Posted on Friday, June 20, 2008 10:34 AM Tutorials | Back to top


Comments on this post: LINQ to Objects: VB TextBox Edition

# re: LINQ to Objects: VB TextBox Edition
Requesting Gravatar...
While that is nice, you can eliminate the additional cast by slightly changing your query using "OfType" in the from clause rather than "Is" in the Where clause:

Dim formBoxes = From formControls In Form.Controls.OfType(Of TextBox)
Select formControls

For Each oBox In formBoxes
oBox.Enabled = False
Next

Notice we don't need to do the additional cast in the iteration because we already took care of strongly typing the results in the OfType method. If you compare the two, your version infers the formBoxes type as IEnumberable(Of Control) where mine infers it as IEnumerable(Of TextBox).
Left by Jim Wooley on Jun 20, 2008 11:51 AM

# re: LINQ to Objects: VB TextBox Edition
Requesting Gravatar...
Thanks. I was thinking of doing that but I'm just learning LINQ and I was trying to apply the cast to the oBox definition in the For Each line but it's great to know I can do that in the LINQ statement.
Left by Matt on Jun 20, 2008 12:52 PM

Your comment:
 (will show your gravatar)


Copyright © Matthew Christian | Powered by: GeeksWithBlogs.net