Listbox help

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi all

I have tried several methods to do as follows

I have a form with two listboxes and a few buttons. The listbox on the left is databound to a table, the other is loaded based on what items from the left table are tied to records in another table

I want to be able to select items in the two listboxes and move them back and forth between the two listboxes

For example, user clicks on box A, and the move button, and it moves the item(s) to box B. User could change their mind, and move items in box B back to box A

I have tried many different ways to do this, and every one seems to end in error

Anyone know of an example that does this?

Thank

Art
 
Art,

In general if you have a databound control and you want to change it's
contents, you should update the underlying dataset and then refresh the
control.

HTH,
Dan


Art DeBuigny said:
Hi all;

I have tried several methods to do as follows.

I have a form with two listboxes and a few buttons. The listbox on the
left is databound to a table, the other is loaded based on what items from
the left table are tied to records in another table.
I want to be able to select items in the two listboxes and move them back
and forth between the two listboxes.
For example, user clicks on box A, and the move button, and it moves the
item(s) to box B. User could change their mind, and move items in box B
back to box A.
 
Hi Art,

Show us some code than we can maybe see where you sticked and what we can
add to help you, we do not know your knowledge do you know?

Cor
 
Hi Srt,

Very easy. Let's say the listboxes are named 'frombox' and 'tobox'. Place
the following code in the tobox selectedindexchanged method:
Dim itm As String

itm = tobox.SelectedItem

If tobox.SelectedIndex <> -1 Then

tobox.Items.Remove(itm)

frombox.Items.Add(itm)

End If

In the selectedindexchanged method for the frombox, this:

Dim itm As String

itm = frombox.SelectedItem

If frombox.SelectedIndex <> -1 Then

frombox.Items.Remove(itm)

tobox.Items.Add(itm)

End If

HTH,

Bernie Yaeger

Hi all;

I have tried several methods to do as follows.

I have a form with two listboxes and a few buttons. The listbox on the
left is databound to a table, the other is loaded based on what items from
the left table are tied to records in another table.
I want to be able to select items in the two listboxes and move them back
and forth between the two listboxes.
For example, user clicks on box A, and the move button, and it moves the
item(s) to box B. User could change their mind, and move items in box B
back to box A.
 
Hi Bernie,

I think that that is not true, from one he need to delete it from the
underlaying table and from the orther I suspect as you said.

For that table he can probably use something as a dataview to select it.

Cor
 
Hi Cor,

My code works perfectly to move from one listbox to the other. Art didn't
say he wishes to update the datatables in question, but if he does, it's a
simple matter once the moves have been made, by accessing the contents of
the listboxes.

Bernie
 
Hi Bernie,
My code works perfectly to move from one listbox to the other. Art didn't
say he wishes to update the datatables in question, but if he does, it's a
simple matter once the moves have been made, by accessing the contents of
the listboxes.

I could not get it to work, I made a sample that did work, can you change
that the way you said, so I can try it?

Cor

Private dt As New DataTable("Cor")
Private lb As New Boolean
Private lc As New Boolean
Private dv As DataView
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs)
_
Handles MyBase.Load
Dim dc As New DataColumn("Art")
dt.Columns.Add(dc)
For i As Integer = 0 To 5
dt.Rows.Add(dt.NewRow)
Next
dt.Rows(0)(0) = "Armin"
dt.Rows(1)(0) = "Jay"
dt.Rows(2)(0) = "Herfried"
dt.Rows(3)(0) = "OHM"
dt.Rows(4)(0) = "Eric"
dt.Rows(5)(0) = "Charles"
dv = New DataView(dt)
Me.ListBox1.DataSource = dv
dv.Sort = "Art"
Me.ListBox1.DisplayMember = "Art"
Me.ListBox2.Items.Add("Tom L")
Me.ListBox2.Items.Add("Bernie")
Me.ListBox2.Items.Add("CJ")
Me.ListBox2.Items.Add("Tom S")
Me.ListBox2.Items.Add("Ken")
Me.ListBox2.Items.Add("Cor")

lb = True
lc = True
End Sub
'
Private Sub ListBox1_SelectedIndexChanged(ByVal _
sender As System.Object, ByVal e As System.EventArgs) _
Handles ListBox1.SelectedIndexChanged
If lb = True Then
If ListBox1.SelectedIndex <> -1 Then
lc = False
Dim itm As String = dv(ListBox1.SelectedIndex)(0).ToString
ListBox2.Items.Add(itm)
dv(ListBox1.SelectedIndex).Delete()
End If
lc = True
End If
End Sub
'
Private Sub ListBox2_SelectedIndexChanged(ByVal sender _
As System.Object, ByVal e As System.EventArgs) _
Handles ListBox2.SelectedIndexChanged
If lc = True Then
If ListBox2.SelectedIndex <> -1 Then
lb = False
Dim itm As String = ListBox2.SelectedItem.ToString
Dim dr As DataRow = dt.NewRow
dr(0) = itm
dt.Rows.Add(dr)
ListBox2.Items.RemoveAt(ListBox2.SelectedIndex)
End If
lb = True
End If
End Sub
 
Hi Cor,

Your using itm as a string; in my code it is an integer that represents the
selecteditem of the clicked listbox.

Bernie
 
Hi Bernie,

My code is totaly different from yours, this is doing a datatable I do only
do things with the items from the selectedtextbox 2

However I am curious to see that working with yours code it did not do it
and you did not send the declarations from the items.

Cor
 
Back
Top