Adding an item to a Checked List Box

S

Simon Jefferies

Hello,

I am trying to add an item to a checked list box, like:

clbList.Items.add("Hello",true)

I get an error back:

Run-time exception thrown: System.ArgumentOutOfRangeException - Specified
argument was out of the range of valid values. Parameter name: '-1' is not a
valid value for index.

This doesn't make a lot of sense? Any ideas why this happens?

Regards
Simon Jefferies
Tools Programmer, Headfirst Productions
mailto:[email protected]
-
 
H

Herfried K. Wagner [MVP]

Simon,

* "Simon Jefferies said:
I am trying to add an item to a checked list box, like:

clbList.Items.add("Hello",true)

I get an error back:

Run-time exception thrown: System.ArgumentOutOfRangeException - Specified
argument was out of the range of valid values. Parameter name: '-1' is not a
valid value for index.

This doesn't make a lot of sense? Any ideas why this happens?

I am not able to repro that. Are you sure this line throws the
exception? Which version of .NET do you use?
 
A

Armin Zingler

Simon Jefferies said:
Hello,

I am trying to add an item to a checked list box, like:

clbList.Items.add("Hello",true)

I get an error back:

Run-time exception thrown: System.ArgumentOutOfRangeException -
Specified argument was out of the range of valid values. Parameter
name: '-1' is not a valid value for index.

This doesn't make a lot of sense? Any ideas why this happens?

Can't repro the prob with VB 2003. Please show me the real line of code (the
above one isn't).


--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html
 
S

Simon Jefferies

I'm running VB .NET 2003 too.

I'm trying with the line quoted just to get it to add something basic.

Regards
Simon Jefferies
Tools Programmer, Headfirst Productions
mailto:[email protected]
-
 
A

Armin Zingler

Simon Jefferies said:
I'm running VB .NET 2003 too.

I'm trying with the line quoted just to get it to add something
basic.

But "add" and "true" usually start with capital letters.

Can you post the callstack from your function call down to the function
throwing the exception?
 
S

Simon Jefferies

Here's the callstack, the first line looks interesting...

system.windows.forms.dll!System.Windows.Forms.CheckedListBox.SetItemCheckState(int
index = -1, System.Windows.Forms.CheckState value = Checked) + 0xbc bytes
system.windows.forms.dll!System.Windows.Forms.CheckedListBox.ObjectCollection.Add(System.Object
item = "Hello", System.Windows.Forms.CheckState check = Checked) + 0xa3
bytes
system.windows.forms.dll!System.Windows.Forms.CheckedListBox.ObjectCollection.Add(System.Object
item = "Hello", bool isChecked = true) + 0x38 bytes
hfsoundeditorplugin.dll!HfSoundEditorPlugin.frmSoundEditorView.DisplayWaveFilesForSoundBank()
Line 725 + 0x2b bytes Basic
hfsoundeditorplugin.dll!HfSoundEditorPlugin.frmSoundEditorView.DisplaySoundBankProperties()
Line 707 + 0xa bytes Basic
hfsoundeditorplugin.dll!HfSoundEditorPlugin.frmSoundEditorView.tvwMain_AfterSelect(Object
sender = {System.Windows.Forms.TreeView},
System.Windows.Forms.TreeViewEventArgs e =
{System.Windows.Forms.TreeViewEventArgs}) Line 622 + 0xa bytes Basic
system.windows.forms.dll!System.Windows.Forms.TreeView.OnAfterSelect(System.Windows.Forms.TreeViewEventArgs
e = {System.Windows.Forms.TreeViewEventArgs}) + 0x23 bytes
system.windows.forms.dll!System.Windows.Forms.TreeView.TvnSelected(System.Windows.Forms.NativeMethods.NMTREEVIEW*
nmtv = 1237196) + 0x9c bytes
system.windows.forms.dll!System.Windows.Forms.TreeView.WmNotify(System.Windows.Forms.Message
m = {System.Windows.Forms.Message}) + 0x137 bytes
system.windows.forms.dll!System.Windows.Forms.TreeView.WndProc(System.Windows.Forms.Message
m = {System.Windows.Forms.Message}) + 0x8c bytes
system.windows.forms.dll!ControlNativeWindow.OnMessage(System.Windows.Forms.Message
m = {System.Windows.Forms.Message}) + 0x19 bytes
system.windows.forms.dll!ControlNativeWindow.WndProc(System.Windows.Forms.Message
m = {System.Windows.Forms.Message}) + 0xda bytes
system.windows.forms.dll!System.Windows.Forms.NativeWindow.DebuggableCallback(int
hWnd = 24578450, int msg = 8270, int wparam = 24578450, int lparam =
1237196) + 0x39 bytes
user32.dll!77d8857a()
user32.dll!77d8867b()
user32.dll!77d89363()
user32.dll!77d893cb()
system.windows.forms.dll!System.Windows.Forms.Control.SendMessage(int msg
= 8270, int wparam = 24578450, int lparam = 1237196) + 0x40 bytes
system.windows.forms.dll!System.Windows.Forms.Control.ReflectMessageInternal(int
hWnd = 24578450, System.Windows.Forms.Message m =
{System.Windows.Forms.Message}) + 0x78 bytes
system.windows.forms.dll!System.Windows.Forms.Control.WmNotify(System.Windows.Forms.Message
m = {System.Windows.Forms.Message}) + 0x35 bytes
system.windows.forms.dll!System.Windows.Forms.Control.WndProc(System.Windows.Forms.Message
m = {System.Windows.Forms.Message}) + 0x662 bytes
system.windows.forms.dll!System.Windows.Forms.GroupBox.WndProc(System.Windows.Forms.Message
m = {System.Windows.Forms.Message}) + 0x6c bytes
system.windows.forms.dll!ControlNativeWindow.OnMessage(System.Windows.Forms.Message
m = {System.Windows.Forms.Message}) + 0x19 bytes
system.windows.forms.dll!ControlNativeWindow.WndProc(System.Windows.Forms.Message
m = {System.Windows.Forms.Message}) + 0xda bytes
system.windows.forms.dll!System.Windows.Forms.NativeWindow.DebuggableCallback(int
hWnd = 21432856, int msg = 78, int wparam = 24578450, int lparam = 1237196)
+ 0x39 bytes
user32.dll!77d8857a()
user32.dll!77d8867b()
user32.dll!77d89363()
user32.dll!77d893cb()
comctl32.dll!4d6daf3c()
comctl32.dll!4d6f6cb7()
comctl32.dll!4d6f8fe2()
comctl32.dll!4d6fa864()
user32.dll!77d8857a()
user32.dll!77d8867b()
user32.dll!77d8acba()
user32.dll!77d8ace0()
system.windows.forms.dll!System.Windows.Forms.NativeWindow.DefWndProc(System.Windows.Forms.Message
m = {System.Windows.Forms.Message}) + 0xc2 bytes
system.windows.forms.dll!System.Windows.Forms.Control.DefWndProc(System.Windows.Forms.Message
m = {System.Windows.Forms.Message}) + 0x19 bytes
system.windows.forms.dll!System.Windows.Forms.Control.WndProc(System.Windows.Forms.Message
m = {System.Windows.Forms.Message}) + 0x9f5 bytes
system.windows.forms.dll!System.Windows.Forms.TreeView.WndProc(System.Windows.Forms.Message
m = {System.Windows.Forms.Message}) + 0x61c bytes
system.windows.forms.dll!ControlNativeWindow.OnMessage(System.Windows.Forms.Message
m = {System.Windows.Forms.Message}) + 0x19 bytes
system.windows.forms.dll!ControlNativeWindow.WndProc(System.Windows.Forms.Message
m = {System.Windows.Forms.Message}) + 0xda bytes
system.windows.forms.dll!System.Windows.Forms.NativeWindow.DebuggableCallback(int
hWnd = 24578450, int msg = 4363, int wparam = 9, int lparam = 1856120) +
0x39 bytes
user32.dll!77d8857a()
user32.dll!77d8867b()
user32.dll!77d89363()
user32.dll!77d893cb()
system.windows.forms.dll!System.Windows.Forms.Control.SendMessage(int msg
= 4363, int wparam = 9, int lparam = 1856120) + 0x5b bytes
system.windows.forms.dll!System.Windows.Forms.TreeView.set_SelectedNode(System.Windows.Forms.TreeNode
value = {Text="Unnamed"}) + 0x61 bytes
hfsoundeditorplugin.dll!HfSoundEditorPlugin.frmSoundEditorView.AddBankToTree(HfSoundEditorPlugin.SoundBank
Bank = {HfSoundEditorPlugin.SoundBank}) Line 564 + 0x18 bytes Basic
hfsoundeditorplugin.dll!HfSoundEditorPlugin.frmSoundEditorView.itmAddSoundBank_Click(Object
sender = {System.Windows.Forms.MenuItem}, System.EventArgs e =
{System.EventArgs}) Line 646 + 0xd bytes Basic
system.windows.forms.dll!System.Windows.Forms.MenuItem.OnClick(System.EventArgs
e = {System.EventArgs}) + 0x8f bytes
system.windows.forms.dll!MenuItemData.Execute() + 0x1e bytes
system.windows.forms.dll!System.Windows.Forms.Command.Invoke() + 0x4c
bytes
system.windows.forms.dll!System.Windows.Forms.Command.DispatchID(int id =
289) + 0x2c bytes
system.windows.forms.dll!System.Windows.Forms.Control.WmCommand(System.Windows.Forms.Message
m = {System.Windows.Forms.Message}) + 0x4a bytes
system.windows.forms.dll!System.Windows.Forms.Control.WndProc(System.Windows.Forms.Message
m = {System.Windows.Forms.Message}) + 0x238 bytes
system.windows.forms.dll!System.Windows.Forms.TreeView.WndProc(System.Windows.Forms.Message
m = {System.Windows.Forms.Message}) + 0x61c bytes
system.windows.forms.dll!ControlNativeWindow.OnMessage(System.Windows.Forms.Message
m = {System.Windows.Forms.Message}) + 0x19 bytes
system.windows.forms.dll!ControlNativeWindow.WndProc(System.Windows.Forms.Message
m = {System.Windows.Forms.Message}) + 0xda bytes
system.windows.forms.dll!System.Windows.Forms.NativeWindow.DebuggableCallback(int
hWnd = 24578450, int msg = 273, int wparam = 289, int lparam = 0) + 0x39
bytes
user32.dll!77d8857a()
user32.dll!77d8867b()
user32.dll!77d88ec4()
user32.dll!77d88f4d()
system.windows.forms.dll!System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods+IMsoComponentManager.FPushMessageLoop(int
dwComponentID = 3, int reason = -1, int pvLoopData = 0) + 0x2fb bytes
system.windows.forms.dll!ThreadContext.RunMessageLoopInner(int reason
= -1, System.Windows.Forms.ApplicationContext context =
{System.Windows.Forms.ApplicationContext}) + 0x1db bytes
system.windows.forms.dll!ThreadContext.RunMessageLoop(int reason = -1,
System.Windows.Forms.ApplicationContext context =
{System.Windows.Forms.ApplicationContext}) + 0x4f bytes
system.windows.forms.dll!System.Windows.Forms.Application.Run(System.Windows.Forms.Form
mainForm = {HfToolFramework.frmMain}) + 0x35 bytes
HfToolFramework.exe!HfToolFramework.frmMain.Main() Line 26 + 0x1d bytes
Basic

Regards
Simon Jefferies
Tools Programmer, Headfirst Productions
mailto:[email protected]
www.headfirst.co.uk www.callofcthulhu.com
-
 
A

Armin Zingler

Simon Jefferies said:
Here's the callstack, the first line looks interesting...


system.windows.forms.dll!System.Windows.Forms.CheckedListBox.SetItemCheckSta
te(int
index = -1, System.Windows.Forms.CheckState value = Checked) + 0xbc
bytes

system.windows.forms.dll!System.Windows.Forms.CheckedListBox.ObjectCollectio
n.Add(System.Object
item = "Hello", System.Windows.Forms.CheckState check = Checked) +
0xa3 bytes

system.windows.forms.dll!System.Windows.Forms.CheckedListBox.ObjectCollectio
n.Add(System.Object
item = "Hello", bool isChecked = true) + 0x38 bytes

Yes, looks interesting. Tried to repro it again and to find the reason (IL
code) but I failed, sorry. Maybe Herfried will find out more.
 
H

Herfried K. Wagner [MVP]

* "Simon Jefferies said:
I'm running VB .NET 2003 too.

I'm trying with the line quoted just to get it to add something basic.

Enclose the line in a 'Try...Catch':

\\\
Try
Me.CheckedListBox...
Catch
End Try
///

Is the exception still thrown? If yes, it's not caused by the line!
 
H

Herfried K. Wagner [MVP]

* "Armin Zingler said:
Yes, looks interesting. Tried to repro it again and to find the reason (IL
code) but I failed, sorry. Maybe Herfried will find out more.

I am not able to repro it on VB.NET 2002 and VB.NET 2003. Mhm...
 
H

Herfried K. Wagner [MVP]

Simon,

* "Simon Jefferies said:
clbWaves.BeginUpdate()
clbWaves.Items.Clear()
Try
clbWaves.Items.Add("Hello", True)
Catch ex As Exception
End Try

clbWaves.EndUpdate()

The Catch statement is called with the Out of range exception.

Does the exception occur if you skip the 'Clear', 'BeginUpdate', and
'EndUpdate'? Are you sure you are performing the operation in the main
GUI thread?
 
S

Simon Jefferies

I don't get an exception when I comment out the BeginUpdate, Clear and
EndUpdate.

I put back in the Clear() and its still working, so it means BeginUpdate()
or EndUpdate() or both causes this problem?

Yes, I am performing this in the main application thread.

Why would this happen?

--
Regards
Simon Jefferies
Tools Programmer, Headfirst Productions
mailto:[email protected]
www.headfirst.co.uk www.callofcthulhu.com
-
 
H

Herfried K. Wagner [MVP]

* "Simon Jefferies said:
I don't get an exception when I comment out the BeginUpdate, Clear and
EndUpdate.

I put back in the Clear() and its still working, so it means BeginUpdate()
or EndUpdate() or both causes this problem?

Yes, I am performing this in the main application thread.

I currently only have access to VB.NET 2002, there I am not able to
repro the "bug".
 
J

John Eikanger [MSFT]

Hi, Simon

I can't get it to repro here either.

What happens when you start a new project, add a checked list box & a
button, and put your code in the button's click event? If that works,
there is probably something wrong in your form or project files. Have you
edited the code in the Windows Form Designer generated code section? That
could be the culpret. If the simple project fails, please zip up the
simple project and attach it to a post. That will give us something real
to play with.

Hope this helps,

John Eikanger
Microsoft Developer Support

This posting is provided “AS IS” with no warranties, and confers no rights.
--------------------
| From: "Simon Jefferies" <[email protected]>
| Subject: Re: Adding an item to a Checked List Box
| Date: Thu, 15 Apr 2004 15:57:44 +0100
| X-Tomcat-NG: microsoft.public.dotnet.languages.vb
|
| I don't get an exception when I comment out the BeginUpdate, Clear and
| EndUpdate.
|
| I put back in the Clear() and its still working, so it means
BeginUpdate()
| or EndUpdate() or both causes this problem?
|
| Yes, I am performing this in the main application thread.
|
| Why would this happen?
|
| --
| Regards
| Simon Jefferies
| Tools Programmer, Headfirst Productions
| mailto:[email protected]
| www.headfirst.co.uk www.callofcthulhu.com
| -
| | > Simon,
| >
| > * "Simon Jefferies" <[email protected]> scripsit:
| >> clbWaves.BeginUpdate()
| >> clbWaves.Items.Clear()
| >> Try
| >> clbWaves.Items.Add("Hello", True)
| >> Catch ex As Exception
| >> End Try
| >>
| >> clbWaves.EndUpdate()
| >>
| >> The Catch statement is called with the Out of range exception.
| >
| > Does the exception occur if you skip the 'Clear', 'BeginUpdate', and
| > 'EndUpdate'? Are you sure you are performing the operation in the main
| > GUI thread?
| >
| > --
| > Herfried K. Wagner [MVP]
| > <URL:http://dotnet.mvps.org/>
|
|
|
 
A

adamz5

Just to make sure the code is in the right place.
Can you put the code behind a command button and try it. should work fine..
 
A

Armin Zingler

J

Jonathan

I am trying to add an item to a checked list box, like:
clbList.Items.add("Hello",true)

I get an error back:

Run-time exception thrown: System.ArgumentOutOfRangeException - Specified
argument was out of the range of valid values. Parameter name: '-1' is not a
valid value for index.

true is a boolean but its integer representation is -1.
It maybe that this suggests that the 2nd parameter was expecting an integer?

Jonathan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top