Introduction to LINQ: Interacting with Controls using LINQ in C#


Server Intellect


Introduction to LINQ: Interacting with Controls using LINQ in C#

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!

Download Project Source - Enter your Email to be emailed a link to download the Full Source Project used in this Tutorial!



100% SPAM FREE! We will never sell or rent your email address!
 


Comments
louis vuitton bags & lv handbags & louis vuitton & lv said:

}

else

{

textBox3.Enabled = true;

button4.Text = "Disable >>";

Posted 08/26/2010 at 8:13 AM
louis vuitton bags & lv handbags & louis vuitton & lv said:

else

{

textBox3.Enabled = true;

button4.Text = "Disable >>";

Posted 08/26/2010 at 8:13 AM
UGG Bailey Button & UGG Bailey Button Boots said:

handlers by clicking on the Events button in the Properties window, and then double-clicking on both of the MouseHover and MouseLeave events.

Let's start with the statusstrip label.Thanks

Posted 08/26/2010 at 8:17 AM
UGG Bailey Button Triplet said:

handlers by clicking on the Events button in the Properties window, and then double-clicking on both of the MouseHover and MouseLeave events.

Posted 08/26/2010 at 8:20 AM
ugg classic boots & womens ugg boots said:

the MouseHover and MouseLeave events.

Posted 08/26/2010 at 8:22 AM
ugg classic boots & ugg boots said:

the MouseHover and MouseLeave events.

Posted 08/26/2010 at 8:24 AM
ugg classic boots & ugg boots said:

Let's start with the statusstrip label.Thanks

Posted 08/26/2010 at 8:25 AM
ugg classic boots & ugg boots & ugg classic tall & ugg classic tall boots said:

with the statusstrip label.Thanks

Posted 08/26/2010 at 8:27 AM
ugg classic boots & ugg boots & ugg classic tall & ugg classic tall boots said:

textBox3.Enabled = true;

button4.Text = "Disable >>";

Posted 08/26/2010 at 8:13 AM

Posted 08/26/2010 at 8:28 AM
coach outlet said:

coach outlet

Posted 08/26/2010 at 1:55 PM
louis vuitton bags said:

louis vuitton bags

Posted 08/26/2010 at 1:56 PM
coach outlet store online said:
Posted 08/26/2010 at 9:17 PM
nisha said:

if i hve visual studio 2008 with me then LINQ Preview (May 2006).msi this file is

needed or not.. if yes then what should i do...

caz it is showing me error like Visual studio 2005 is required..

Posted 09/03/2010 at 3:30 AM

Leave a Comment