Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Generic Record Selector With Filter

0.00/5 (No votes)
10 Nov 2009 1  
A Generic Record Selector Windows Form with Data Filter
Record_Selector_Demo_Application

Introduction

I've never been a big fan of the combobox as a method for selecting records, so I came up with the Record Selector. By passing a set of parameters (Dataset, column headings, etc.) to the Record Selector class library, you get a new Windows Form displaying all of the data passed into it. Multiple record selections are available, and the filter textbox filters every item as you type.

Using the Code

I have included a demo application, but basically, you need to add a reference to class library SZ-CL-0003 (I know this is not the best or most descriptive naming convention, but it's what I always use!), and declare a string array variable to assign the selected records(s) to.

The following parameters are used by the Record Selector.

dataset

A standard system.data.dataset. Although the current version only allows two columns of data to be displayed on screen, any extra columns found will be added to the tooltiptext for the record. The tooltiptext is included in the filter.

strtitle

A string variable containing the title for the selection screen.

strcolumns

A string array containing the two columns to display on screen (I may increase this at a later date).

strfixed (Opt (Default: Nothing))

A string array of fixed items that will appear in the list for selection, but will always appear at the top of the list and will not be affected by the filter.

Each fixed item consists of a 3-bound array:

  • (0) = 1st Fixed Item 1st Column Text
  • (1) = 1st Fixed Item 2nd Column Text
  • (2) = 1st Fixed Item ToolTipText
  • (3) = 2nd Fixed Item 1st Column Text
  • (4) = 2nd Fixed Item 2nd Column Text
  • (5) = 2nd Fixed Item ToolTipText

blnmultiple (Opt (Default: True))

A boolean value to determine if multiple selections will be available. If more than one item in the list is selected, a select button will appear on the toolstrip, if only one record is required, then simply double-clicking the item will select it.

Sample Code

The following code allows the user to select a single user from a user dataset, or add a new user by selecting the <NEW> fixed line item and should be placed on a button click event.

In this example, a reference has already been added to SZ-CL-0003 and the dataset has already been created and filled.

Dim selector As New SZ_CL_0003.Selector

Private Sub btnSelectSingleAddNew_Click(ByVal sender As System.Object, _
	ByVal e As System.EventArgs) Handles btnSelectSingleAddNew.Click

   Dim strtitle As String = "Select Users"
   Dim strcolumns(2) As String
   Dim strfixed(3) As String

   strcolumns(0) = "User Code"
   strcolumns(1) = "Name"

   strfixed(0) = "<NEW>" 
   strfixed(1) = "Add New User"
   strfixed(2) = "Add a new user record to the database"

   Dim struser() As String = selector.Select_Records_
				(dataset, strtitle, strcolumns, strfixed, False)

   If struser IsNot Nothing Then

      For intloop As Integer = 0 To (UBound(struser) - 1)

         If struser(intloop) = "<NEW>" Then
            Add_New_User
         Else
            MessageBox.Show(struser(intloop), "User Selected", _
			MessageBoxButtons.OK, MessageBoxIcon.Information)
         End If

      Next

   End If

End Sub     

History

  • 16th April 2009 - 13:25 - Added code to allow selection using the <RTN> key, or exit without selection using the <ESC> key

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here