give dynamic items a value in a listbox PLEASE..

cwfontan

Active member
Joined
Jan 9, 2009
Messages
35
Programming Experience
1-3
Main Issue :confused:
I have a listbox that I am dynamically populating but I cant seem to add a value to the items.. the code is below


Small Issue
also it would be nice to put both these tables into 1 dataset instead of the 2 I am using

Thanks!!



VB.NET:
 'fill dataset with dupont and lanxess tables
            Dim da As OleDbDataAdapter = New OleDbDataAdapter("SELECT hostname, Transmitnum, recid, transtype, workordernum, aptmntdate, aptmnttime, location, state, Orderrefnum, PlantRefnum, apptreas, status FROM tbldriver", cn)
            Dim la As OleDbDataAdapter = New OleDbDataAdapter("Select '2', Wrkordrnum, aptmntdate, aptmnttime, refnum, tanknum, billdngnum,  ontime, shipper, shppraddr, shpprcty, shpprst, shpprzip, consgnee, consgnaddr, consgncty, consgnst, consgnzip, shpstatus, recid From tbllogistics", cn)
            Table0 = "dupont"
            Table1 = "lanxess"
            da.Fill(ds, Table0)
            la.Fill(ds, Table1)

            'loop through the dataset and populate the listbox1 with TransmitNum & Location cwf 4-19-09
            If ds.Tables(0).Rows.Count > 0 Then
                Dim i As Integer = 0
                For IntCounter = 0 To ds.Tables("dupont").Rows.Count - 1
                    varAlpha = ""
                    varAlpha = ds.Tables(Table0).Rows(i).Item(1).ToString() & ", " & ds.Tables(Table0).Rows(i).Item(7).ToString()
                    ListBox1.Items.Add(UCase(varAlpha))
                    i = i + 1
                Next IntCounter
            End If

            If ds.Tables(1).Rows.Count > 0 Then
                Dim i As Integer = 0
                For IntCounter = 0 To ds.Tables("lanxess").Rows.Count - 1
                    varAlpha2 = ""
                    varAlpha2 = ds.Tables(Table1).Rows(i).Item(4).ToString() & ", " & ds.Tables(Table1).Rows(i).Item(15).ToString()
                    ListBox1.Items.Add(UCase(varAlpha2))
                    i = i + 1
                Next IntCounter
            End If
 
Last edited:
I would look into databinding.

Here is some pseudocode you might want to try.
VB.NET:
sqlText = "SELECT Transmitnum + ' ' + Location FROM ..."
ds = execute sql
listbox1.datasource = ds
listbox1.databind

Also take a look at your loops. You have the following:

VB.NET:
Dim i As Integer = 0
For IntCounter = 0 To ds.Tables(0).Rows.Count - 1
        varAlpha = ""
        varAlpha = ds.Tables(Table1).Rows(i).Item(1).ToString() & ", " & ds.Tables(Table1).Rows(i).Item(7).ToString()
        ListBox1.Items.Add(UCase(varAlpha))

         i = i + 1
Next IntCounter

Your IntCounter and i variables are serving the same purpose. They both start at zero and get increased each iteration.
 
thanks, I get different results when I use just the intCounter,.

I have to add the values to the listbox in code because some will have values from a different table etc..


there has to be a way to specify listbox1 item value = "mycol"
or something close to that huh?
 
When using IntCounter you shouldnt get any different results...

Sorry I thought you were using two ListBoxes.

You can still use my method by using a union in the sql query.

VB.NET:
SELECT Transmitnum + ', ' + Location FROM ...
UNION
SELECT aptmnttime + ', ' + consgncty FROM ...

You can also assign both the value and the text properties of the item.

VB.NET:
ListBox1.DataTextField = "dbField"
ListBox1.DataValueField = "dbField"

Also, when you come across a time when you have to add items to a list box manually you can do the same thing this way.

VB.NET:
Dim item as new ListItem()
item.Value = "itemValue"
item.Text = "itemText"
ListBox1.Items.Add(item)
 
thank you for your help.. when I try listbox1.datavaluefield OR the item.value intellisence doesnt see it and it says not a member of system.windows.form.listbox

I added reference.. am i missing something? .net 3.5 did they take it away for the valuemember? And the value member doesnt work unless you databind if I remember correctly..??
 
Sorry, I dont work with ListBoxes too often. The properties are DisplayMember and ValueMember.

The ListItem class is apparently not in forms development. I'm not sure of the equivalent in forms. Maybe someone else can help out there.

What is the Forms equivalent to the ListItem Class?
 
Back
Top