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)"
LVL 1
kayhustleAsked:
Who is Participating?
 
PockyMasterCommented:
If you set the text from a different thread, you should use a delegate + invoke on your combo
0
 
PockyMasterCommented:
0 is an actual index, you probably have no items in your combobox?
0
 
kayhustleAuthor Commented:
Yeah I don't but why is that stopping me from getting and setting the text inside the combobox?
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
PockyMasterCommented:
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
0
 
kayhustleAuthor Commented:
yeah but I'm doing this:
comboBox1.Text = "value";
0
 
Bob LearnedCommented:
There's not enough information to help troubleshoot this problem.

Bob
0
 
kayhustleAuthor Commented:
u tellin me, I don't see the exception listed in the .net documentation either for combobox.Text set/get
0
 
RobertRFreemanCommented:
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.
0
 
winsoftechCommented:
comboBox1.Text = "value";

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

Bob
0
 
PockyMasterCommented:
Is it clear for you now kayhustle?
0
 
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?
0
 
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?
0
 
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.
thanks
0
 
PockyMasterCommented:
Sorry I was too late to answer, but you answered your own question in just 15 minutes :D
Well done :D
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.