Link to home
Start Free TrialLog in
Avatar of kayhustle
kayhustle

asked on

Problem setting Combo Box Text

I get the following exception when attempting to set or get the text property of a ComboBox

ArgumentOutOfRangeException{"InvalidArgument=Value of '0' is not valid for 'index'.\r\nParameter name: index"}

Here is the stack trace:

"   at System.Windows.Forms.ComboBox.ObjectCollection.get_Item(Int32 index)\r\n   at System.Windows.Forms.ComboBox.get_Text()\r\n   at CommonUIWindows.ItemChooser.cbtextchanged(Object target) in C:\\Documents and Settings\\Acronym Media\\My Documents\\Visual Studio Projects\\Reporting Solution\\CommonUIWindows\\ItemChooser.cs:line 153\r\n   at System.Threading._ThreadPoolWaitCallback.WaitCallback_Context(Object state)\r\n   at System.Threading.ExecutionContext.runTryCode(Object userData)\r\n   at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)\r\n   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)\r\n   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)\r\n   at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback(Object state)"
Avatar of PockyMaster
PockyMaster
Flag of Netherlands image

0 is an actual index, you probably have no items in your combobox?
Avatar of kayhustle
kayhustle

ASKER

Yeah I don't but why is that stopping me from getting and setting the text inside the combobox?
You cannot get or set items that don't exist.

You should add them first.

e.g.

MessageBox.Show (comboBox1.Items(0).ToString) '<--- gives error
comboBox1.Items.Add("myFirstItem")
MessageBox.Show (comboBox1.Items(0).ToString) '<-- now should be ok
yeah but I'm doing this:
comboBox1.Text = "value";
Avatar of Bob Learned
There's not enough information to help troubleshoot this problem.

Bob
u tellin me, I don't see the exception listed in the .net documentation either for combobox.Text set/get
The combobox.Text property gets/sets the current item's text value.  If there are no items, then there is no current item.  You will get that message.
comboBox1.Text = "value";

doesn't add item to como box
u have to add item to combobox using
comboBox1.Items.Add("xxx")
Some code would be nice ;)

Bob
Is it clear for you now kayhustle?
Sorry for the delay, all I can say is that I was setting the text of the combobox on a ThreadPool thread.  Should I be doing it on a different thread, or does it not matter?
ASKER CERTIFIED SOLUTION
Avatar of PockyMaster
PockyMaster
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ohh so you're saying the function that changes the combobox text should be invoked using a delegate?
Is that how all user controls, should be modified, because I had to disable the IllegalCrossThreadCalls MDA to even do this?
Did a bit more research and found that this.Invoke(del, new object[]{"text"}), works if del points to a method that changes the combobox text.
thanks
Sorry I was too late to answer, but you answered your own question in just 15 minutes :D
Well done :D