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)"
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

0 is an actual index, you probably have no items in your combobox?
kayhustleAuthor Commented:
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.


MessageBox.Show (comboBox1.Items(0).ToString) '<--- gives error
MessageBox.Show (comboBox1.Items(0).ToString) '<-- now should be ok
C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

kayhustleAuthor Commented:
yeah but I'm doing this:
comboBox1.Text = "value";
Bob LearnedCommented:
There's not enough information to help troubleshoot this problem.

kayhustleAuthor Commented:
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
Bob LearnedCommented:
Some code would be nice ;)

Is it clear for you now kayhustle?
kayhustleAuthor Commented:
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?
If you set the text from a different thread, you should use a delegate + invoke on your combo

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
kayhustleAuthor Commented:
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?
kayhustleAuthor Commented:
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.
Sorry I was too late to answer, but you answered your own question in just 15 minutes :D
Well done :D
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.