[program-l] Question relating to manually updating value/item in combobox - windows application using C#

  • From: Jacob Kruger <Jacob.Kruger@xxxxxxxxxxxx>
  • To: "program-l@xxxxxxxxxxxxx" <program-l@xxxxxxxxxxxxx>
  • Date: Tue, 27 Oct 2009 10:44:48 +0200

As part of building my interface based on the contents of an XML file, I am 
generating combo boxes and populating them by setting their datasource 
properties equal to a string[] array that I am generating by splitting the 
incoming text/string values.

That piece of code looks like this:
char[] separators = {','};
string[] stItems = stText.Split(separators);
cbNew.DataSource = stItems;

Only problem now is I want to allow the user to select an item, and then try 
edit it, but when I try something like updating the item, nothing happens, and 
if I try to remove the selected item and then insert a new one using the items 
collection, I get a JIT debugger error relating to being unable to update the 
collection items of a databound object, when the datasource is set or something 
like that.

Suppose if I manually added each item to the items collection initially by 
looping through array, it might then be more prepared to let me update them, or 
else I should do something like store the data arrays in global variables and 
update those or something to hope the databinding would carry out the update 
for me?

The code there currently looks like this:
int iIndex = frmParent.curCB.SelectedIndex;
frmParent.curCB.Items.RemoveAt(iIndex);
frmParent.curCB.Items.Insert(iIndex - 1, "new value");

The error text specifically states the following:
System.ArgumentException: Items collection cannot be modified when the 
DataSource property is set

Another question is when I want to get the second sub form shown to allow the 
user to edit the values in these combo boxes, or add new ones, I seem to have 
to manually create an instance of the other form class, and then tell it to 
perform a method/subroutine to tell it who it's parent form is so they can 
'communicate' with each other, and am pretty sure in VB.Net would generally 
just have called a .Show class to an automatic instance of the other forms 
class or something, but suppose might also be wrong about that one...<smile>

Sorry if this is a silly question, but, to be honest haven't recently really 
done much work on windows apps like this.

Stay well

Jacob Kruger
jacob.kruger@xxxxxxxxxxxx


** To leave the list, click on the immediately-following link:-
** [mailto:program-l-request@xxxxxxxxxxxxx?subject=unsubscribe]
** If this link doesn't work then send a message to:
** program-l-request@xxxxxxxxxxxxx
** and in the Subject line type
** unsubscribe
** For other list commands such as vacation mode, click on the
** immediately-following link:-
** [mailto:program-l-request@xxxxxxxxxxxxx?subject=faq]
** or send a message, to
** program-l-request@xxxxxxxxxxxxx with the Subject:- faq

Other related posts: