Solved

Listbox selections

Posted on 2009-04-06
7
124 Views
Last Modified: 2012-05-06
Hi, I have this listbox1 populated by a database and next to it another empty one (listbox2). Between the two, 2 buttons "-->" & "<--". You guessed it I need those buttons to add and remove items from listbox1 to listbox2. One they have been removed from listbox1, I do not want those values to still be in it, it's really to move items between the two listboxes. What would be code to achieve this ?
Thanks
0
Comment
Question by:GPSAddict
  • 4
  • 3
7 Comments
 
LVL 20

Expert Comment

by:informaniac
ID: 24077215
Try this code.

Hope it's clear enough for u.
protected void btnAdd_Click(object sender, EventArgs e)

    {

        for (int i = lstFrom.Items.Count - 1; i >= 0; i--)

        {

            if (lstFrom.Items[i].Selected)

            {

                if (lstTo.Items.FindByValue(lstFrom.Items[i].Value) != null)

                {

                    continue;

                }

                else

                {

                    lstTo.Items.Add(lstFrom.Items[i]);

                    lstFrom.Items.Remove(lstFrom.Items[i]);

                }

            }

        }
 

    }

        
 

   

    protected void btnRemove_Click(object sender, EventArgs e)

    {

        try

        {

            for (int i = lstTo.Items.Count - 1; i >= 0; i--)

            {

                if (lstTo.Items[i].Selected)

                {

                    lstFrom.Items.Add(lstTo.Items[i]);

                    lstTo.Items.Remove(lstTo.Items[i]);

                }

            }

        }

        catch (Exception ex)

        {

            throw ex;

        }

    } 

Open in new window

0
 

Author Comment

by:GPSAddict
ID: 24077244
I'll try, the only problem is I don't know C, I use VB. Thanks
0
 
LVL 20

Accepted Solution

by:
informaniac earned 500 total points
ID: 24077298
Well sorry for the same.
But In situations like this use this link

http://www.developerfusion.com/tools/convert/csharp-to-vb/
Protected Sub btnAdd_Click(ByVal sender As Object, ByVal e As EventArgs)

    For i As Integer = lstFrom.Items.Count - 1 To 0 Step -1

        If lstFrom.Items(i).Selected Then

            If lstTo.Items.FindByValue(lstFrom.Items(i).Value) IsNot Nothing Then

                Continue For

            Else

                lstTo.Items.Add(lstFrom.Items(i))

                lstFrom.Items.Remove(lstFrom.Items(i))

            End If

        End If

        

    Next

End Sub
 
 
 

Protected Sub btnRemove_Click(ByVal sender As Object, ByVal e As EventArgs)

    Try

        For i As Integer = lstTo.Items.Count - 1 To 0 Step -1

            If lstTo.Items(i).Selected Then

                lstFrom.Items.Add(lstTo.Items(i))

                lstTo.Items.Remove(lstTo.Items(i))

            End If

        Next

    Catch ex As Exception

        Throw ex

    End Try

End Sub

Open in new window

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:GPSAddict
ID: 24077418
Alright, let me try this at home and I'll get back to you asap. Thanks !
0
 

Author Comment

by:GPSAddict
ID: 24082922
2 things if I may, if you do a batch add, it adds the items but in reverse order ie. I select 1,2,3,4 but it adds in this order; 4,3,2,1. Also, when I remove an item from the 2nd listbox, how can I make it so that re-appears where it was before ? Thanks
0
 

Author Comment

by:GPSAddict
ID: 24139418
Hello ?
0
 
LVL 20

Assisted Solution

by:informaniac
informaniac earned 500 total points
ID: 24144653
For the add in proper order, this code should work...

For making it to appear where it was, I don't think it might be possible. U'll have to try out some other trick...
Protected Sub btnAdd_Click(ByVal sender As Object, ByVal e As EventArgs)

    For i As Integer = 0 To lstFrom.Items.Count - 1

        If lstFrom.Items(i).Selected Then

            If lstTo.Items.FindByValue(lstFrom.Items(i).Value) IsNot Nothing Then

                Continue For

            Else

                lstTo.Items.Add(lstFrom.Items(i))

            End If

        End If

    Next

    

    

    For i As Integer = lstFrom.Items.Count - 1 To 0 Step -1

        If lstFrom.Items(i).Selected Then

            lstFrom.Items.Remove(lstFrom.Items(i))

        End If

        

        

    Next

End Sub

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now