User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Visual Basic 4 / 5 / 6 section within the Software Development category of DaniWeb, a massive community of 426,424 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,384 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Visual Basic 4 / 5 / 6 advertiser: Programming Forums
Views: 1798 | Replies: 4
Reply
Join Date: Apr 2008
Posts: 19
Reputation: mansi sharma is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
mansi sharma mansi sharma is offline Offline
Newbie Poster

Copy Items from one listbox to another

  #1  
Apr 22nd, 2008
I want to copy the selected items from listbox1 to listbox2.
I know how to copy all the items but not selected.
Private Sub Command3_Click()
For i = 1 To List1.ListCount
List2.AddItem i
Next
End Sub

Above Code will copy all the items,But I want to copy selected items.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Nov 2007
Location: � Jogja �
Posts: 2,585
Reputation: Jx_Man is a glorious beacon of light Jx_Man is a glorious beacon of light Jx_Man is a glorious beacon of light Jx_Man is a glorious beacon of light Jx_Man is a glorious beacon of light Jx_Man is a glorious beacon of light 
Rep Power: 11
Solved Threads: 235
Jx_Man's Avatar
Jx_Man Jx_Man is offline Offline
Posting Maven

Re: Copy Items from one listbox to another

  #2  
Apr 22nd, 2008
you want to copy selected only, this will answer your question:
Private Sub Command3_Click()
List2.AddItem (List1.ListIndex)
End Sub
hope this helps...
Never tried = Never Know
So, Please do something before post your thread.
* PM Asking will be ignored *
Reply With Quote  
Join Date: Apr 2008
Posts: 19
Reputation: mansi sharma is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
mansi sharma mansi sharma is offline Offline
Newbie Poster

Re: Copy Items from one listbox to another

  #3  
Apr 22nd, 2008
hi.Jx_man, ur code is copying just 1 item, i want to copy multiple selected items.
I have tried the foll. code,Check it out where I m wrong-

Private Sub Command9_Click()
For i = List1.ListCount - 1 To 0 Step -1
If List1.Selected(i) Then
List2.AddItem (List1.ListIndex)
End If
Next
End Sub
Reply With Quote  
Join Date: Feb 2008
Location: Sivakasi, Tamilnadu, India
Posts: 458
Reputation: selvaganapathy is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 79
selvaganapathy's Avatar
selvaganapathy selvaganapathy is offline Offline
Posting Pro in Training

Re: Copy Items from one listbox to another

  #4  
Apr 25th, 2008
Your Mistake is
List2.AddItem (List1.ListIndex)

replace the above with
List2.AddItem (List1.List(i))
Reply With Quote  
Join Date: May 2007
Location: India
Posts: 494
Reputation: choudhuryshouvi is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 45
choudhuryshouvi's Avatar
choudhuryshouvi choudhuryshouvi is offline Offline
Posting Pro in Training

Re: Copy Items from one listbox to another

  #5  
Apr 26th, 2008
Originally Posted by mansi sharma View Post
I want to copy the selected items from listbox1 to listbox2.
I know how to copy all the items but not selected.
Private Sub Command3_Click()
For i = 1 To List1.ListCount
List2.AddItem i
Next
End Sub

Above Code will copy all the items,But I want to copy selected items.


hi mansi, this is the completely code for you. make sure you have set the multiselect property of your listbox control to 2-Extended.

Option Explicit

Private Sub Command1_Click()
Dim i As Integer, c As Integer

If List1.ListCount = 0 Then
    MsgBox "No items found."
    Exit Sub
End If

c = 0
For i = 0 To List1.ListCount - 1 Step 1
    If List1.Selected(i) = True Then
        c = 1
        Exit For
    End If
Next i

If c = 0 Then
    MsgBox "Please select some items from the list."
    List1.SetFocus
    Exit Sub
ElseIf c = 1 Then
    For i = 0 To List1.ListCount - 1 Step 1
        If List1.Selected(i) = True Then
            List2.AddItem List1.List(i)
        End If
    Next i
End If
End Sub

Private Sub Form_Load()
Dim i As Integer

For i = 1 To 12 Step 1
    List1.AddItem MonthName(i)
Next i
End Sub

regards
Shouvik
Shouvik_The_Expert_Coder
Have a problem? Don't worry just give me a call and I'll fix it for you.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Visual Basic 4 / 5 / 6 Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the Visual Basic 4 / 5 / 6 Forum

All times are GMT -4. The time now is 2:15 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC