Introduction to LINQ: Interacting with Controls using LINQ in VB


Server Intellect


Introduction to LINQ: Interacting with Controls using LINQ in VB

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.

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
Henrik said:

Where is teh LINQ, and whats with the annoying texts about server intellect that messes up the text?

Posted 02/16/2009 at 3:57 AM
Poker News said:

I noticed that you arrived here from Google looking for Introduction to LINQ: Interacting with Controls using LINQ in VB..LINQ training classes teaches attendees how to use Language Integrated Query to query arrays, enumberable classes, relational databases, and a wide variety of other relational and object-based data sources

Posted 02/27/2010 at 5:40 AM
Buy Property said:

The ADO.NET Entity Framework handily plays the role of data access layer, letting you focus on the solution rather than a database schema. The Entity Framework converts the database's view of the data, the logical schema, normalized for the database's needs into a conceptual model representing your problem-space's needs.

Posted 06/04/2010 at 2:08 AM
classified ads said:

the most popular to use. Not many developers enjoy working with SQL statements - having to translate between that and the scripting language. But now, with ASP.NET 3.5, we have been introduced to LINQ. LINQ provides us with a way of communicating directly with a database in the code - no need to translate into SQL. LINQ also comes in a variety of flavors, allowing us to connect to numerous data sources, not only databases.

Posted 06/08/2010 at 6:30 AM
Indian Recepies said:

the previous lesson, Creating LINQ to SQL Classes: Using the O/R Designer, you learned how to use the O/R Designer to map LINQ to SQL classes to tables in a database. In this lesson, you will learn how to use a LINQ query to bind data to a control.

Posted 06/11/2010 at 1:16 AM
Agile Dinesh said:

Many readers of the second tutorial have written asking how they can use DataBinding to bind to data in their SQL Database. This tutorial will walk through accessing SQL Data by creating a Web Service and then using LINQ to create a data source you can bind to.

Posted 06/11/2010 at 1:34 AM
kuber infotek said:

I have been using LINQ for a while and I’m still amazed at how easy it is to perform tasks that otherwise would have required numerous lines of code to achieve the same result. Recently I was faced with a request on sorting items that were in an ASP.NET DropDownList control.

Posted 06/11/2010 at 5:22 AM
Waterproof Jacket said:

Awesome blog. I enjoyed reading your articles. This is truly a great read for me. I have bookmarked it and I am looking forward to reading new articles. Keep up the good work!

Posted 06/11/2010 at 10:47 AM
registry cleaners said:

This was the wonderful most movie i ever watched

Posted 06/17/2010 at 2:50 AM
limo party bus los angeles said:

The demonstration project included with the article is a simple contact manager which may be used to capture and store information about a person’s contacts in address book format. This demonstration application uses LINQ to Objects to manage, query, and order the list of contacts maintained by the application. The demonstration application also includes a dummy contact file with a collection of test contacts.

Posted 06/25/2010 at 7:09 AM
online casino said:

I was very pleased to find this site.I wanted to thank you for this great read!! I definitely enjoying every little bit of it and I have you bookmarked to check out new stuff you post.

Posted 08/03/2010 at 9:40 PM
simulation assurance auto said:

Thanks for taking the time to discuss this, I feel strongly about it and love learning more on this topic. If possible, as you gain expertise, would you mind updating your blog with extra information? It is extremely helpful for me.

Posted 08/04/2010 at 3:53 AM
search engine placement marketing said:

LINQ provides us with a way of communicating directly with a database in the code - no need to translate into SQL. LINQ also comes in a variety of flavors, allowing us to connect to numerous data sources, not only databases.

Posted 08/13/2010 at 1:45 PM
phentermine 37.5 said:

training classes teaches attendees how to use Language Integrated Query to query arrays, enumberable classes, relational databases, and a wide variety of other relational and object-based data sources

Posted 08/21/2010 at 6:08 AM
coach outlet said:

We will start by designing our form with Four buttons and a label. The first button will be to display all the numbers in our array, which we will hard-code for this example.

Posted 08/26/2010 at 8:12 AM
louis vuitton bags said:

thank you for sharing

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:14 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
xiaopohai said:
Posted 08/26/2010 at 9:14 PM
cheap carole king tickets said:

Useful information shared..I am very happy to read this article..thanks for giving us nice info.Fantastic walk-through. I appreciate this post.

Posted 08/30/2010 at 6:34 AM
fico score said:

ASP.NET provides ample ways for us to connect to a database and manipulate database data. ASP.NET with VS.NET helps greatly when creating a system to allow adding, editing and deleting of database data. However, there are times when you want more control over this than the built-in controls and methods provide.

Posted 09/07/2010 at 2:55 AM

Leave a Comment