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
ArgumentOutOfRangeExceptio n{"Invalid Argument=V alue of '0' is not valid for 'index'.\r\nParameter name: index"}
Here is the stack trace:
" at System.Windows.Forms.Combo Box.Object Collection .get_Item( Int32 index)\r\n at System.Windows.Forms.Combo Box.get_Te xt()\r\n at CommonUIWindows.ItemChoose r.cbtextch anged(Obje ct target) in C:\\Documents and Settings\\Acronym Media\\My Documents\\Visual Studio Projects\\Reporting Solution\\CommonUIWindows\ \ItemChoos er.cs:line 153\r\n at System.Threading._ThreadPo olWaitCall back.WaitC allback_Co ntext(Obje ct state)\r\n at System.Threading.Execution Context.ru nTryCode(O bject userData)\r\n at System.Runtime.CompilerSer vices.Runt imeHelpers .ExecuteCo deWithGuar anteedClea nup(TryCod e code, CleanupCode backoutCode, Object userData)\r\n at System.Threading.Execution Context.Ru nInternal( ExecutionC ontext executionContext, ContextCallback callback, Object state)\r\n at System.Threading.Execution Context.Ru n(Executio nContext executionContext, ContextCallback callback, Object state)\r\n at System.Threading._ThreadPo olWaitCall back.Perfo rmWaitCall back(Objec t state)"
ArgumentOutOfRangeExceptio
Here is the stack trace:
" at System.Windows.Forms.Combo
0 is an actual index, you probably have no items in your combobox?
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).ToStri ng) '<--- gives error
comboBox1.Items.Add("myFir stItem")
MessageBox.Show (comboBox1.Items(0).ToStri ng) '<-- now should be ok
You should add them first.
e.g.
MessageBox.Show (comboBox1.Items(0).ToStri
comboBox1.Items.Add("myFir
MessageBox.Show (comboBox1.Items(0).ToStri
ASKER
yeah but I'm doing this:
comboBox1.Text = "value";
comboBox1.Text = "value";
There's not enough information to help troubleshoot this problem.
Bob
Bob
ASKER
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")
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
Bob
Is it clear for you now kayhustle?
ASKER
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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?
Is that how all user controls, should be modified, because I had to disable the IllegalCrossThreadCalls MDA to even do this?
ASKER
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
thanks
Sorry I was too late to answer, but you answered your own question in just 15 minutes :D
Well done :D
Well done :D