?
Solved

Problem setting Combo Box Text

Posted on 2006-04-11
15
Medium Priority
?
434 Views
Last Modified: 2007-12-19
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)"
0
Comment
Question by:kayhustle
  • 6
  • 5
  • 2
  • +2
15 Comments
 
LVL 14

Expert Comment

by:PockyMaster
ID: 16430994
0 is an actual index, you probably have no items in your combobox?
0
 
LVL 1

Author Comment

by:kayhustle
ID: 16431256
Yeah I don't but why is that stopping me from getting and setting the text inside the combobox?
0
 
LVL 14

Expert Comment

by:PockyMaster
ID: 16431312
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
 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

 
LVL 1

Author Comment

by:kayhustle
ID: 16431419
yeah but I'm doing this:
comboBox1.Text = "value";
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 16431573
There's not enough information to help troubleshoot this problem.

Bob
0
 
LVL 1

Author Comment

by:kayhustle
ID: 16431589
u tellin me, I don't see the exception listed in the .net documentation either for combobox.Text set/get
0
 
LVL 16

Expert Comment

by:RobertRFreeman
ID: 16431914
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
 
LVL 1

Expert Comment

by:winsoftech
ID: 16433584
comboBox1.Text = "value";

doesn't add item to como box
u have to add item to combobox using
comboBox1.Items.Add("xxx")
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 16435234
Some code would be nice ;)

Bob
0
 
LVL 14

Expert Comment

by:PockyMaster
ID: 16437402
Is it clear for you now kayhustle?
0
 
LVL 1

Author Comment

by:kayhustle
ID: 16484005
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
 
LVL 14

Accepted Solution

by:
PockyMaster earned 1500 total points
ID: 16484461
If you set the text from a different thread, you should use a delegate + invoke on your combo
0
 
LVL 1

Author Comment

by:kayhustle
ID: 16487830
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
 
LVL 1

Author Comment

by:kayhustle
ID: 16488043
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
 
LVL 14

Expert Comment

by:PockyMaster
ID: 16488917
Sorry I was too late to answer, but you answered your own question in just 15 minutes :D
Well done :D
0

Featured Post

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Screencast - Getting to Know the Pipeline
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question