none
Moving all the SelectedValuePath items from a ListBox to another ListBox

    Question

  • #1# Design:

    I have ListBox1, ListBox2 and a Button.

    In ListBox1, have a set of items (DisplayMemberPath) while ListBox2 is empty.

     

    #2# Required Logic:

    Pressing the Button will move all the (SelectedValuePath) items in ListBox1 to ListBox2.

     

    Note: I want all the items (not only the selected ones) in ListBix1 to be moved to ListBox2 !

    I am using WPF C#

    Can any expert help me to achieve this?

    Sunday, May 29, 2011 7:25 AM

Answers

  • Hi Adel k,

         You can easily achieve this, hare is the sample code: where i use 2 list box & 1 button which will move leftlistbox data to right Listbox:

    using System;
    using System.Windows;
    namespace ListViewItemMove
    {
      public partial class MainWindow : Window
      {
        public MainWindow()
        {
          InitializeComponent();
          leftlistBox.Items.Add("Atik");
          leftlistBox.Items.Add("Refat");
          leftlistBox.Items.Add("Amin");
        }
        private void ItemMovebuttonClick(object sender, RoutedEventArgs e)
        {
          try
          {
            string item = leftlistBox.SelectedItem as string;
            if (item != null)
            {
              if (DuplicacyCheck(item))
              {
                rightlistBox.Items.Add(item);
              }
            }
          }
          catch (Exception ex)
          {
            
            MessageBox.Show(ex.Message, "Item moving Error", MessageBoxButton.OK, MessageBoxImage.Error);
          }
        }
        private bool DuplicacyCheck(string item)
        {
          if (rightlistBox.Items.Count > 0)
          {
            for (int i = 0; i < rightlistBox.Items.Count; i++)
            {
              string rightItem = rightlistBox.Items[i] as string;
              if(item == rightItem)
              {
                return false;
              }
            }
          }
          return true;
        }
      }
    }
    
    

     

     

     

    Hope it will help you.


    Regards atik sarker
    Sunday, May 29, 2011 11:16 AM
  • Hi Adel K,

    Since you only want to move all items in a listbox to another listbox, you can simply use their ItemsSource property to accomplish this.

    Below is a simple example.

        private void Button_Click(object sender, RoutedEventArgs e)
        {
          RightListBox.ItemsSource = LeftListBox.ItemsSource;
          LeftListBox.ItemsSource = null;
        }
    

     

    If you still have any questions or concerns about this issue, please feel free to let me know.

     


    Min Zhu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, June 01, 2011 6:20 AM

All replies

  • Hi Adel k,

         You can easily achieve this, hare is the sample code: where i use 2 list box & 1 button which will move leftlistbox data to right Listbox:

    using System;
    using System.Windows;
    namespace ListViewItemMove
    {
      public partial class MainWindow : Window
      {
        public MainWindow()
        {
          InitializeComponent();
          leftlistBox.Items.Add("Atik");
          leftlistBox.Items.Add("Refat");
          leftlistBox.Items.Add("Amin");
        }
        private void ItemMovebuttonClick(object sender, RoutedEventArgs e)
        {
          try
          {
            string item = leftlistBox.SelectedItem as string;
            if (item != null)
            {
              if (DuplicacyCheck(item))
              {
                rightlistBox.Items.Add(item);
              }
            }
          }
          catch (Exception ex)
          {
            
            MessageBox.Show(ex.Message, "Item moving Error", MessageBoxButton.OK, MessageBoxImage.Error);
          }
        }
        private bool DuplicacyCheck(string item)
        {
          if (rightlistBox.Items.Count > 0)
          {
            for (int i = 0; i < rightlistBox.Items.Count; i++)
            {
              string rightItem = rightlistBox.Items[i] as string;
              if(item == rightItem)
              {
                return false;
              }
            }
          }
          return true;
        }
      }
    }
    
    

     

     

     

    Hope it will help you.


    Regards atik sarker
    Sunday, May 29, 2011 11:16 AM
  • Hi atik,

     

    thanks for your clean answer but your job was answering another question (how can i add a selected item in a listbox to another listbox avoiding repitition?)

     

    what i need is the following:

    a button one single press to add all the items in a listbox to another listbix (in your example, from leftlistbox to rightlistbox)

    Monday, May 30, 2011 1:55 PM
  • For this you have to select all item of the listbox. But if you use tree then when you select one root tree item it will move all child item in second tree.
    Regards atik sarker
    Tuesday, May 31, 2011 6:25 PM
  • Hi Adel K,

    Since you only want to move all items in a listbox to another listbox, you can simply use their ItemsSource property to accomplish this.

    Below is a simple example.

        private void Button_Click(object sender, RoutedEventArgs e)
        {
          RightListBox.ItemsSource = LeftListBox.ItemsSource;
          LeftListBox.ItemsSource = null;
        }
    

     

    If you still have any questions or concerns about this issue, please feel free to let me know.

     


    Min Zhu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, June 01, 2011 6:20 AM
  • Hi Adel K,

    We have not heard from you in a couple of days.

    Please post back at your convenience if we can assist further.

    Enjoy your day!


    Min Zhu [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, June 06, 2011 7:46 AM