none
Moving specific selected Items on one ListBox to the other and Refresh them RRS feed

  • Question

  • Hi,

    I am currently working on a project that allow users to select which folder they want to take files from as source and where to as the destination.

    Both these textboxes to select source and destinations have ListBox below that shows my csv files available to the selected folders.

    Between these ListBoxes there's button as >> that I click to move files from source to destination side.

    Here is my problem; I want so move files that I selected in the source ListBox not just everything as it is doing right now. Then update both ListBox after this movement across them.

    Thanks you,

    Mncedi

    Wednesday, June 17, 2020 8:54 PM

All replies

  • Hello,

    Try the following for single items

    if (listBox1.SelectedItem != null)
    {
        listBox2.Items.Add(listBox1.Text);
        listBox1.Items.Remove(listBox1.SelectedItem);
    }

    Multiple selected items

    if (listBox1.SelectedItems != null)
    {
        for (int index = 0; index < listBox1.SelectedItems.Count; index++)
        {
            listBox2.Items.Add(listBox1.SelectedItems[index]);
        }
        ListBox.SelectedObjectCollection selectedItems = new ListBox.SelectedObjectCollection(listBox1);
        selectedItems = listBox1.SelectedItems;
    
        if (listBox1.SelectedIndex != -1)
        {
            for (int i = selectedItems.Count - 1; i >= 0; i--)
                listBox1.Items.Remove(selectedItems[i]);
        }
    }


    Please remember to mark the replies as answers if they help and unmarked 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.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Wednesday, June 17, 2020 9:20 PM
    Moderator
  • Hi Mncedi,

    I just want to add a little reminder.

    When you want to select multiple items at once, you need to set the SelectionMode property of the listbox to MultiExtended or MultiSimple.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, June 18, 2020 2:53 AM
  • hi

    Put two list box as listbox1 and listbox2 and a button as button1 and bellow is the code 

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using System.io;
    namespace listboxmovement
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
                startprograme();
            }
            public void startprograme()
            {
                listBox1.Items.Add("D:/Test1");
                listBox1.Items.Add("D:/Test2");
                listBox1.Items.Add("D:/Test3");
                listBox1.Items.Add("D:/Test4");
                listBox1.Items.Add("D:/Test5");
            }
    
           
     private void button1_Click(object sender, EventArgs e)
            {
                try
                {
                    if (listBox1.Items.Count > 0)
                    {
                        var selectitem = listBox1.SelectedItem.ToString();

                        var selectitem2 = listBox2.SelectedItem.ToString();
                        File.Move(selectitem, selectitem2);

                        listBox1.Items.Remove(selectitem);
                    }
                }
                catch (Exception)
                {

                   // throw;
                }
               
            }
        }
    }



    Friday, June 19, 2020 7:13 PM
  • hi

    Is solved?

    Thursday, June 25, 2020 4:09 PM
  • Hi,

    Has your issue been resolved?

    If so, please click on the "Mark as answer" option of the reply that solved your question, so that it will help other members to find the solution quickly if they face a similar issue.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, July 10, 2020 9:35 AM