hi for my project what i am trying to do is:
i have two listboxes and in between the two boxes i have two buttons.

Btn1 is to send data from listbox1 to listbox 2
Btn2 is to send data from listbox2 to listbox1

below is my code could you please suggest improvements help me improve the method i am using thanks

Private Sub m_buttonAddUserRole_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles m_buttonAddUserRole.Click
        connecta()

    End Sub
    Sub connecta()
        acsconn.ConnectionString = "Provider=Microsoft.jet.oledb.4.0;data source=C:\Users\HP Sajad\Documents\Visual Studio 2010\Projects\login1\login1\security.mdb"
        acsconn.Open()

        If acsconn.State = ConnectionState.Open Then

        End If
        MsgBox("Connected")

        Dim roleIndex As Integer = ListBox1.SelectedIndex
        If (roleIndex = -1) Then
            Return
        End If
        ' Get the current user index.
        Dim userIndex As Integer = ListBox2.SelectedIndex
        ' Should we ignore the the event?
        If (userIndex = -1) Then
            Return
        End If


        ' Get the identifiers.
        Dim userID As Integer = CType(ListBox1.SelectedItem, Int32)
        Dim rightID As Integer = CType(ListBox2.SelectedItem, Int32)

        ' Search for an existing row.
        Dim rows As DataRow() = "Select right_id from cg_security_role_right where = '" & rightID & "' AND user_id = '" & userID & ;


        ' Is there already an association
        If (rows.Length > 0) Then
            Return
        End If


    End Sub

1. Tell me one thing, how do you populate these 2 listBoxes?
Because so far I havent seen this code (and you actually didnt tell us).

2. And tell me one more thing, when some item is selected in listbox1, when you wanna transfer to listbox2, On which index do you wanna put it? On the same as it was in listBox1, or on last place?

Edited 4 Years Ago by Mitja Bonca: n/a

If the two listboxes have Sorted=True then

Private Sub btnAdd_Click(sender As System.Object, e As System.EventArgs) Handles btnAdd.Click

        If ListBox1.SelectedIndex >= 0 Then
            ListBox2.Items.Add(ListBox1.SelectedItem)
            ListBox1.Items.RemoveAt(ListBox1.SelectedIndex)
        End If

    End Sub

    Private Sub btnRemove_Click(sender As System.Object, e As System.EventArgs) Handles btnRemove.Click

        If ListBox2.SelectedIndex >= 0 Then
            ListBox1.Items.Add(ListBox2.SelectedItem)
            ListBox2.Items.RemoveAt(ListBox2.SelectedIndex)
        End If

    End Sub

You can add enhancements. For example, when an item is transfered, you might want to automatically select the next item so the user can repeatedly click "ADD" or "REMOVE" to transfer consecutive items. Or you could enable multiple selection.

Edited 4 Years Ago by Reverend Jim: n/a

Comments
Explained very easily...
This article has been dead for over six months. Start a new discussion instead.