This tutorial was created with Microsoft's LINQ Community Technology Preview release, which can be downloaded from here
We migrated our web sites to Server Intellect over one weekend and the setup was so smooth that we were up and running right away. They assisted us with everything we needed to do for all of our applications. With Server Intellect's help, we were able to avoid any headaches!
In this introduction, we will introduce you to using LINQ in a Windows Form to find all controls on the form of a certain type, and then interact with them by disabling them. In the example, we will have three textboxes, a button to disable them all and a button to enable them all.
The first thing we do after installing the LINQ release above, is to start a new project > Visual Basic LINQ Windows Application.
We will start by adding the controls to the form. In this example, we will have a total of 5 buttons, and three textboxes.
The form should look something like this:

Now we have our form, we can add the logic to the code-behind. Under the Disable Button click event we add the following:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
For Each myChildTextBox As TextBox In Me.Controls.OfType(Of TextBox)()
myChildTextBox.Enabled = False
Next myChildTextBox
End Sub |
This code searches for all controls on the page that are TextBoxes, then it loops through all of them, disabling them one by one.
Need help with Windows Dedicated Hosting? Try Server Intellect. I'm a happy customer!
We can also add logic to the Enable Button, and also to buttons to disable and enable each individual button, but we don't need to use LINQ for those. The code-behind will look something like this:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
For Each myChildTextBox As TextBox In Me.Controls.OfType(Of TextBox)()
myChildTextBox.Enabled = False
Next myChildTextBox
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
For Each myChildTextBox As TextBox In Me.Controls.OfType(Of TextBox)()
myChildTextBox.Enabled = True
Next myChildTextBox
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
If TextBox1.Enabled = True Then
TextBox1.Enabled = False
Button3.Text = "Enable >>"
Else
TextBox1.Enabled = True
Button3.Text = "Disable >>"
End If
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
If TextBox2.Enabled = True Then
TextBox2.Enabled = False
Button4.Text = "Enable >>"
Else
TextBox2.Enabled = True
Button4.Text = "Disable >>"
End If
End Sub
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
If TextBox3.Enabled = True Then
TextBox3.Enabled = False
Button5.Text = "Enable >>"
Else
TextBox3.Enabled = True
Button5.Text = "Disable >>"
End If
End Sub
End Class |
I just signed up at Server Intellect and couldn't be more pleased with my Windows Server! Check it out and see for yourself.