This tutorial was created with Microsoft's LINQ Community Technology Preview release, which can be downloaded from here
I just signed up at Server Intellect and couldn't be more pleased with my Windows Server! Check it out and see for yourself.
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 > C# LINQ Windows Application.
You will notice the following assemblies have been referenced, amongst others:
using System.Query;
using System.Xml.XLinq;
using System.Data.DLinq; |
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 void button1_Click(object sender, EventArgs e)
{
foreach (TextBox myChildTextBox in this.Controls.OfType<TextBox>())
{
myChildTextBox.Enabled = false;
}
} |
This code searches for all controls on the page that are TextBoxes, then it loops through all of them, disabling them one by one.
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:
Server Intellect offers Windows Hosting Dedicated Servers at affordable prices. I'm very pleased!
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Query;
using System.Xml.XLinq;
using System.Data.DLinq;
namespace LINQ_Intro2_cs
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
foreach (TextBox myChildTextBox in this.Controls.OfType<TextBox>())
{
myChildTextBox.Enabled = false;
}
}
private void button5_Click(object sender, EventArgs e)
{
foreach (TextBox myChildTextBox in this.Controls.OfType<TextBox>())
{
myChildTextBox.Enabled = true;
}
}
private void button2_Click(object sender, EventArgs e)
{
if (textBox1.Enabled == true)
{
textBox1.Enabled = false;
button2.Text = "Enable >>";
}
else
{
textBox1.Enabled = true;
button2.Text = "Disable >>";
}
}
private void button3_Click(object sender, EventArgs e)
{
if (textBox2.Enabled == true)
{
textBox2.Enabled = false;
button3.Text = "Enable >>";
}
else
{
textBox2.Enabled = true;
button3.Text = "Disable >>";
}
}
private void button4_Click(object sender, EventArgs e)
{
if (textBox3.Enabled == true)
{
textBox3.Enabled = false;
button4.Text = "Enable >>";
}
else
{
textBox3.Enabled = true;
button4.Text = "Disable >>";
}
}
}
} |
Try Server Intellect for Windows Server Hosting. Quality and Quantity!