Results 1 to 2 of 2

Thread: Listbox value to listbox and back. Error with saving Properties.Settings

  1. #1
    .NET Framework
    .NET 4.0
    Join Date
    Jan 2019
    Posts
    1
    Rep Power
    0

    Listbox value to listbox and back. Error with saving Properties.Settings

    Hi,

    I have a good working solution to get items from one listbox to another listbox and back.

    9KXHz.png

    The listbox is filled by a list. Next step is to save the custom template from both listboxes.
    I tried to do this with Properties.Setting.Default to save the listboxes. But when i can't find a way
    to solve the following error:

    System.ArgumentException: 'Items collection cannot be modified when the DataSource property is set.

    This is my code so far:
    Code:
    publicpartialclassForm1:Form
    {
        List<string> l2;
        string selectedItemText;
        intSelectedIndex;  
    
        publicForm1()
        {
            InitializeComponent();           
    
            l2 =newList<string>();
            l2.Add("1");
            l2.Add("2");
            l2.Add("3");
            l2.Add("4");
            l2.Add("5");
    
            listBox1.DataSource= l2;
        }
    
        privatevoidForm1_Load(object sender,EventArgs e)
        {         
            listBox1.Items.Clear();
            foreach(object item inProperties.Settings.Default.listSave)
            {
                listBox1.Items.Add(item);
            }
    
            listBox2.Items.Clear();
            foreach(object item inProperties.Settings.Default.listSave2)
            {
                listBox2.Items.Add(item);
            }            
        }
    
        privatevoid button1_Click(object sender,EventArgs e)
        {
            this.Close();
    
            Properties.Settings.Default.listSave.Clear();
            Properties.Settings.Default.listSave2.Clear();
    
            foreach(object item in listBox1.Items)
            {
                Properties.Settings.Default.listSave.Add(item.ToString());
            }
    
            foreach(object item in listBox2.Items)
            {
                Properties.Settings.Default.listSave2.Add(item.ToString());
            }
            Properties.Settings.Default.Save();          
    
        }
    
        privatevoid button2_Click_1(object sender,EventArgs e)
        {
            selectedItemText = listBox1.SelectedItem.ToString();
            SelectedIndex= listBox1.SelectedIndex;
    
            listBox2.Items.Add(selectedItemText);
    
            if(l2 !=null)
            {
                l2.RemoveAt(SelectedIndex);
            }
    
            DataBindings();
        }
    
        privatevoidDataBindings()
        {
            listBox1.DataSource=null;
            listBox1.DataSource= l2;
    
        }
    
        privatevoid button4_Click(object sender,EventArgs e)
        {
            selectedItemText = listBox2.SelectedItem.ToString();
            SelectedIndex= listBox2.SelectedIndex;
            l2.Add(selectedItemText);
    
            listBox2.Items.RemoveAt(listBox2.Items.IndexOf(listBox2.SelectedItem));
    
            DataBindings();
        }
    }



  2. #2
    .NET Framework
    .NET 4.5
    Join Date
    Apr 2011
    Posts
    2,001
    Rep Power
    156
    When you bind a ListBox, i.e. set its DataSource property, you can get an item via the Items collection of the ListBox but you cannot add or remove items that way. If you want to add or remove then you MUST do that via the data source. If you want to be able to add and remove items directly then don't bind in the first place and if you do bind then you must add and remove items via the data source.

Similar Threads

  1. Question Print ListBox
    By GHOST in forum Reporting / Printing
    Replies: 1
    Last Post: 09-02-2018, 1:39 PM
  2. Sorting a listbox by columns
    By Abe in forum Windows Presentation Foundation (WPF)
    Replies: 5
    Last Post: 07-17-2017, 11:40 AM
  3. Get Selected Items from Listbox...
    By cboshdave in forum Windows Forms
    Replies: 8
    Last Post: 09-06-2016, 3:00 AM
  4. Update ListBox
    By inkedGFX in forum Windows Presentation Foundation (WPF)
    Replies: 0
    Last Post: 06-21-2014, 12:30 PM
  5. How can I update/refresh a Listbox?
    By MinusZero in forum Windows Forms
    Replies: 2
    Last Post: 08-26-2013, 12:53 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •