Listbox removeAt - Datasource error

Listbox removeAt - Datasource error

k005
Advisor Advisor
2,102 Views
10 Replies
Message 1 of 11

Listbox removeAt - Datasource error

k005
Advisor
Advisor

Hi all !

 

I am listing some text files from my dwg file in Listbox.

I want to do extraction with the code below. But it gives an error about datasource.

 

error :

 

"Items collection cannot be modified when the DataSource property is set."

 

How can I remove an item from the listbox without this error?

 

 

 

 

int secim = listBox1.SelectedIndex;
         
            if (secim != -1)
            {
                listBox1.Items.RemoveAt(secim);
              
               // listBox1.DataSource = listBox1.Items;

            }
            else
            {
                MessageBox.Show("Seçim Yapın!");
            }

 

0 Likes
Accepted solutions (1)
2,103 Views
10 Replies
Replies (10)
Message 2 of 11

fieldguy
Advisor
Advisor

this has nothing to do with autocad .net.  you've been told several times - try google

0 Likes
Message 3 of 11

k005
Advisor
Advisor

dwg file... that's why I wrote it here. normally you don't have any questions.

0 Likes
Message 4 of 11

Norman_Yuan
Mentor
Mentor

I strongly concur with @fieldguy's reply: this forum is for AutoCAD .NET API discussion. While more generic/basic programming topics could also discussed, one can get suitable answers a lot easier/quicker. For example, simply enter "Items collection cannot be modified when the DataSource property is set" in Google search, you would get over 100,000 results, and the very first one would give you the CLEAREST, SIMPLEST answer, with sample code. Please try it before post back again.

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 5 of 11

fieldguy
Advisor
Advisor

your subject is "listbox removeat - datasource error".  it doesn't matter what's in the listbox.  Enter those exact words in google for more than 1 million hits.  "Listbox" is not a member of the autocad API.

 

IMO you are looking for someone else to do your work for you. 

0 Likes
Message 6 of 11

k005
Advisor
Advisor

You think I didn't google this?

 

You offer suggestions but not solutions...

 

Now what is the result: You wrote 0 answers in vain.

0 Likes
Message 7 of 11

k005
Advisor
Advisor

 

 

I totally disagree with this guy...@ fieldguy

0 Likes
Message 8 of 11

hippe013
Advisor
Advisor

@k005 

I am going to have to agree with everyone else that has responded to your question. The solution to your problem is in the error message that you posted. 

 

"Items collection cannot be modified when the DataSource property is set."

 

It clearly stats that an Items Collection (the list in your listbox) cannot be modified when the DataSource property is set. 

 

Your solution is the following: Add items to your listbox manually rather than using the DataSource property. 

 

Ultimately, I have found that the people on this forum are always more than willing to help with AutoCAD API related questions and are often excited to do so. They are NOT, however, here to provide answers to other API related questions or to do the work for someone else. 

 

With that said, I hope that this helps you to find a solution to your error. 

0 Likes
Message 9 of 11

k005
Advisor
Advisor

@hippe013 

 

Hi,

 

...

Your solution is the following: Add items to your listbox manually rather than using the DataSource property.

...

 

 

 

i tried adding it manually listbox is getting empty... the code should change completely.

I want to do this while in datasource ? Isn't there a method for this?

0 Likes
Message 10 of 11

hippe013
Advisor
Advisor
Accepted solution

There are many ways to skin a cat. Without going on too much about MVVM, databinding, and the whole works... a quick example is the following: 

 

The following code works, but probably not ideal. 

'In the Context of the ListBox (Manually Add)
ListBox1.Items.Add("Item 1")
ListBox1.Items.Add("Item 2")
ListBox1.Items.Add("Item 3")

'In the context of a method:
Dim idx As Integer = ListBox1.SelectedIndex
ListBox1.Items.RemoveAt(idx)

 

The following code also works, better than the previous, but could also be improved upon.

'In the Context of the ListBox (set ItemsSource)
Private obList As New ObservableCollection(Of String)(New List(Of String) From {"Item 1", "Item 2", "Item 3"})

ListBox1.ItemsSource = obList

'In the context of a method:
Dim idx as Integer = ListBox1.SelectedIndex
obList.RemoveAt(idx)

 

I hope that this clarifies for you what I meant about adding the items manually. You are setting the ListBox1.ItemSource to a list of items and then when you go to remove an item you are removing from ListBox1.Items and this is causing your error. 

 

0 Likes
Message 11 of 11

k005
Advisor
Advisor

OK. Thanks.

0 Likes