Page 1
Page 2
Now let's use some class
One of the first issues VB programmers
have with .NET combo-boxes is where has my ItemData
array gone? How can I fill the combo-box with list items
specifiying a display parameter and a key
to return when the item is clicked.
Well the uber-cool feature of .NET list
containers is that you can fill them with any type of object.
This allows you to set up a collection of objects; Orders,
Products, Clients what ever you have and then add each object
directly to the list or combo-box. To top it all
using this technique is actually pretty fast and from a
development perspective it's great to have performance and
programming logic coincide!
This is the code we use to fill the
combo-box:
Dim oTimeIt
As New TimeIt()
If sql.ceConnect Then
mFilling = True
cbo2.Items.Clear()
cbo2.Visible = False
sbar.Text = "Filling combobox using class." :
sbar.Refresh()
oTimeIt.Start()
Dim tmpReader As SqlCeDataReader
Dim sqlCmd As New SqlServerCe.SqlCeCommand("SELECT
DevbuzzID, TestName FROM DevbuzzTable order by TestName",
sql.connCE)
tmpReader = sqlCmd.ExecuteReader()
If tmpReader.Read = True Then
While tmpReader.Read()
cbo2.Items.Add(New Test(tmpReader.Item("TestName"),
tmpReader.Item("DevbuzzID"), tmpReader.Item("TestName")))
End While
End If
oTimeIt.Finish()
Label2.Text = oTimeIt.Elapsed() & " seconds"
cbo2.Visible = True
sbar.Text = "Finished." : sbar.Refresh()
sql.Disconnect()
tmpReader = Nothing
mFilling = False
End If
Firstly we grab the DevbuzzTable
test data using a SQLCEDataReader:
Dim tmpReader
As SqlCeDataReader
Dim sqlCmd As New SqlServerCe.SqlCeCommand("SELECT
DevbuzzID, TestName FROM DevbuzzTable order by TestName",
sql.connCE)
tmpReader = sqlCmd.ExecuteReader()
Then we loop through the results, and
this is the important part; creating an instance of the
Test class for each row and adding it direcly to
the combo-box; as follows:
While tmpReader.Read()
cbo2.Items.Add(New Test(tmpReader.Item("TestName"),
tmpReader.Item("DevbuzzID"), tmpReader.Item("TestName")))
End While
The Test class definition
Public Class
Test
Public txtShow As String
Public DevbuzzID As Integer
Public TestName As String
Sub New(ByVal pText As String, ByVal pDevbuzzID As Integer,
ByVal pTestName As String)
Me.txtShow = pText
Me.DevbuzzID = pDevbuzzID
Me.TestName = pTestName
End Sub
Overrides Function ToString() As String
Return txtShow
End Function
End Class
We have Overrided the constructor of
the Test class with our own New() definition.
This allows us to create an instance of the Test class and
add it to the cbo2 combo-box in one command:
cbo2.Items.Add(New
Test(tmpReader.Item("TestName"), tmpReader.Item("DevbuzzID"),
tmpReader.Item("TestName")))
Flexible
The above class method is much more
flexible than the old ItemData approach since it
opens the door allowing us to return any property of the
Test class. In most instances you will be returning a key
of sorts using code such as:
If Not mFilling Then
Dim thisTest As Test
thisTest = CType(cbo2.SelectedItem, Test)
MessageBox.Show(thisTest.DevbuzzID)
End If
But there is nothing to stop you, and
more importantly no performance overhead to returning
any other property of the thisTest
object, for example:
thisTest.Testname
Performance
Typically on my test device (Toshiba
e570) I see an elapsed time of
about 2-4 seconds to fill a combo-box with 500
rows using the class technique.

Previous Page