Using values in non-databound listboxes

S

Scott McNair

Hi,

I have two listboxes (we'll call them LB1 and LB2). LB1 and LB2 are both
populated from data, with the valuemember bound to the ID, and the
displaymember bound to the data's text.

I would like to be able to move the items back and forth between LB1 and
LB2 by using "<-" and "->" buttons, so databinding is ruled out (since you
can't add or remove items to a bound listbox).

My alternative is to populate LB1 manually, by iterating through the
datarows and adding an item for each row. The problem with this is that I
lose the ability to tie an ID to the valuemember (since a non-databound
listbox is indexed [0,1,2,...] rather than value-bound).

Does anybody have a suggestion as to how I can go about achieving this?
I'm not married to the listbox at this point, and any comparable control
will do.
 
C

Cor Ligthert[MVP]

Scott,

Why can you not use a dataview with a rowfilter to solve your problem.

Cor
 
J

Jack Jackson

Hi,

I have two listboxes (we'll call them LB1 and LB2). LB1 and LB2 are both
populated from data, with the valuemember bound to the ID, and the
displaymember bound to the data's text.

I would like to be able to move the items back and forth between LB1 and
LB2 by using "<-" and "->" buttons, so databinding is ruled out (since you
can't add or remove items to a bound listbox).

But you can add and remove to/from the underlying data source.

I think I might use two BindingList(Of T), one for each listbox. Add
the appropriate records to each. You would probably need to
implements sorting in the BindingLists.

You could also use two DataTables instead.
 
S

Scott McNair


Follow-up:

It dawned on me last night that the two listboxes were unwieldy, so I
decided to go with a single checked listbox control instead... items which
would normally be populated in the second listbox are now simply checked in
the first one.

I also needed to have the ability to alter the order in which the items
appeared (with up and down arrow buttons), which I accomplished with a
little creative coding, by creating a new datacolumn called sortorder,
binding my data to a dataview with .Sort="SortOrder", and then having the
button event swap the value with it's parent or child.
 
S

SurturZ

I like making a specific class to hold the data you want to associate with
items in the listbox. If you override the ToString() method, that is what
will appear in the listbox. You can then add and remove them willy nilly and
have lots of related data associated with each entry without needing to
synchronise arrays outside the listbox.

It's brilliant really, Microsoft really nailed it with that one. I think it
would also solve your problem.
 
S

SurturZ

sample code:

Create a Windows Forms App with a single form that has two listboxes and a
button between them.

Then add the following code (N.B. the TestClass definition is inside the
form, YMMV):

Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Button1.Text = "->"
For i As Integer = 0 To 4
Dim o As New TestClass(i, "Item " & i.ToString, i.ToString)
ListBox1.Items.Add(o)
Next i
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
If ListBox1.SelectedIndex = -1 Then MsgBox("You must select an item
first") : Exit Sub
Dim o As TestClass = CType(ListBox1.SelectedItem, TestClass)
ListBox2.Items.Add(o)
ListBox1.Items.Remove(o)
End Sub

Public Class TestClass
Sub New(ByVal id As Integer, ByVal prop1 As String, ByVal prop2 As
String)
Me.ID = id
Me.Prop1 = prop1
Me.Prop2 = prop2
End Sub
Public ID As Integer
Public Prop1 As String
Public Prop2 As String

Public Overrides Function ToString() As String
Return Me.Prop1
End Function
End Class
End Class


--
David Streeter
Synchrotech Software
Sydney Australia


SurturZ said:
I like making a specific class to hold the data you want to associate with
items in the listbox. If you override the ToString() method, that is what
will appear in the listbox. You can then add and remove them willy nilly and
have lots of related data associated with each entry without needing to
synchronise arrays outside the listbox.

It's brilliant really, Microsoft really nailed it with that one. I think it
would also solve your problem.

--
David Streeter
Synchrotech Software
Sydney Australia


Scott McNair said:
Hi,

I have two listboxes (we'll call them LB1 and LB2). LB1 and LB2 are both
populated from data, with the valuemember bound to the ID, and the
displaymember bound to the data's text.

I would like to be able to move the items back and forth between LB1 and
LB2 by using "<-" and "->" buttons, so databinding is ruled out (since you
can't add or remove items to a bound listbox).

My alternative is to populate LB1 manually, by iterating through the
datarows and adding an item for each row. The problem with this is that I
lose the ability to tie an ID to the valuemember (since a non-databound
listbox is indexed [0,1,2,...] rather than value-bound).

Does anybody have a suggestion as to how I can go about achieving this?
I'm not married to the listbox at this point, and any comparable control
will do.
 
C

Cor Ligthert[MVP]

Jack,

Be aware that with 2 datatables you don't benefit of the binding.
However this can be because of the question an alternative for the OP

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top