none
Copy items for a listbox to another RRS feed

  • Question

  • As you can see from the title, i have to copy my Items from a listbox to another my moving the mouse on the item I want, but if that item is already in the new listbox, it doesn't have to show a duplicate.

    Help pls

    Friday, January 18, 2019 1:40 PM

Answers

  •     Private Sub ButtonMoveItem_Click(sender As Object, e As EventArgs) Handles ButtonMoveItem.Click
            If ListBox2.Items.Contains(ListBox1.SelectedItem) Then
                MsgBox("Item in LB2 nothing happend")
            Else
                ListBox2.Items.Add(ListBox1.SelectedItem)
                ListBox1.Items.Remove(ListBox1.SelectedItem)
            End If
        End Sub


    Live as if you were going to die today, learn as if you were going to live forever -Mahatma Gandhi

    Friday, January 18, 2019 2:37 PM

All replies

  • How are these LisBoxes populated e.g. DataSource set or via the .Items collection?

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Friday, January 18, 2019 2:16 PM
    Moderator
  • .Items
    Friday, January 18, 2019 2:17 PM
  • The following example when clicking a button (could be done with or without a button) checks to see if ListBox2 items has the selected item in ListBox1, if not add it to ListBox2, otherwise don't add it.

    The language extension Contains is done this way a) makes it reusable b) keeps code clean where otherwise would clutter the code in the click event.

    Public Class Form1
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            If Not ListBox2.Items.Contains(ListBox1.Text) Then
                ListBox2.Items.Add(ListBox1.Text)
            End If
        End Sub
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            ListBox1.Items.AddRange(MonthNames())
            ListBox1.SelectedIndex = Now.Month - 1
        End Sub
        ''' <summary>
        ''' Used to populate ListBox1
        ''' </summary>
        ''' <returns></returns>
        Public Function MonthNames() As String()
            Return (From month In Globalization.CultureInfo.CurrentCulture.
                DateTimeFormat.MonthNames Where Not String.IsNullOrEmpty(month)).ToArray
        End Function
    End Class
    ''' <summary>
    ''' This can reside here or in a separate code module.
    ''' </summary>
    Public Module ListBoxExtensions
        <Runtime.CompilerServices.Extension()>
        Public Function Contains(sender As ListBox.ObjectCollection, value As String) As Int32
            Dim result = (From T In sender.Cast(Of String).
                    Select(Function(item, ordinal) New With {.Item = item.ToLower, .Index = ordinal})
                          Where T.Item.Contains(value)).FirstOrDefault
    
            If result IsNot Nothing Then
                Return result.Index
            Else
                Return -1
            End If
        End Function
    End Module
    

    So to be clear it doesn't need to be a button click event, here I use on DoubleClick

    Public Class Form1
        Private Sub ListBox1_DoubleClick(sender As Object, e As EventArgs) Handles ListBox1.DoubleClick
            If Not ListBox2.Items.Contains(ListBox1.Text) Then
                ListBox2.Items.Add(ListBox1.Text)
            End If
        End Sub
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            ListBox1.Items.AddRange(MonthNames())
            ListBox1.SelectedIndex = Now.Month - 1
        End Sub
        ''' <summary>
        ''' Used to populate ListBox1
        ''' </summary>
        ''' <returns></returns>
        Public Function MonthNames() As String()
            Return (From month In Globalization.CultureInfo.CurrentCulture.
                DateTimeFormat.MonthNames Where Not String.IsNullOrEmpty(month)).ToArray
        End Function
    
    
    End Class
    ''' <summary>
    ''' This can reside here or in a separate code module.
    ''' </summary>
    Public Module ListBoxExtensions
        <Runtime.CompilerServices.Extension()>
        Public Function Contains(sender As ListBox.ObjectCollection, value As String) As Int32
            Dim result = (From T In sender.Cast(Of String).
                    Select(Function(item, ordinal) New With {.Item = item.ToLower, .Index = ordinal})
                          Where T.Item.Contains(value)).FirstOrDefault
    
            If result IsNot Nothing Then
                Return result.Index
            Else
                Return -1
            End If
        End Function
    End Module
    


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Friday, January 18, 2019 2:32 PM
    Moderator
  •     Private Sub ButtonMoveItem_Click(sender As Object, e As EventArgs) Handles ButtonMoveItem.Click
            If ListBox2.Items.Contains(ListBox1.SelectedItem) Then
                MsgBox("Item in LB2 nothing happend")
            Else
                ListBox2.Items.Add(ListBox1.SelectedItem)
                ListBox1.Items.Remove(ListBox1.SelectedItem)
            End If
        End Sub


    Live as if you were going to die today, learn as if you were going to live forever -Mahatma Gandhi

    Friday, January 18, 2019 2:37 PM