CheckedListBox Problems

bjwade62

Well-known member
Joined
May 25, 2006
Messages
50
Programming Experience
3-5
I have two problems with my code for a CheckedListBox. I have a sub that checks to see if an item in a CheckedListBox is checked. If it is it's supposed to copy the corrisponding file to a folder. See code below.

Problem #1
I get an IndexOutOfRangeException error at line:
clbText = CheckedListBox1.CheckedItems.Item(i)

Problem # 2
After the file is copied I want the checked item to become unchecked.

Here's my code. Thanks.

VB.NET:
[COLOR=#0000ff][SIZE=2][COLOR=#0000ff]If [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].CheckedListBox1.CheckedItems.Count > 1 [/SIZE][SIZE=2][COLOR=#0000ff]Then[/COLOR][/SIZE][/COLOR]


[COLOR=#0000ff][INDENT][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] clbText [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][/INDENT][INDENT][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] i [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][/INDENT][INDENT][SIZE=2][COLOR=#0000ff]For[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Each[/COLOR][/SIZE][SIZE=2] item [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Object[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]In[/COLOR][/SIZE][SIZE=2] CheckedListBox1.Items[/SIZE][/INDENT][INDENT][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] item = CheckState.Checked [/SIZE][SIZE=2][COLOR=#0000ff]Then[/COLOR][/SIZE][/INDENT][INDENT][SIZE=2]clbText = CheckedListBox1.GetItemText(item)[/SIZE][/INDENT][INDENT][SIZE=2][COLOR=#008000]'copies selected file to project folder[/COLOR][/SIZE][/INDENT][INDENT][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] strFileToSaveNoPath [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2] = System.IO.Path.GetFileNameWithoutExtension(clbText)[/SIZE][/INDENT][INDENT][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] strFileToSaveNoPathDWG [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2] = strFileToSaveNoPath & [/SIZE][SIZE=2][COLOR=#800000]".dwg"[/COLOR][/SIZE][/INDENT][INDENT][SIZE=2]strFileToSave = [/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].dirStdDtl.Path & [/SIZE][SIZE=2][COLOR=#800000]"\"[/COLOR][/SIZE][SIZE=2] & strFileToSaveNoPath & [/SIZE][SIZE=2][COLOR=#800000]".dwg"[/COLOR][/SIZE][/INDENT][INDENT][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] strNewFile [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2] = [/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].proj_dir.Path & [/SIZE][SIZE=2][COLOR=#800000]"\"[/COLOR][/SIZE][SIZE=2] & strFileToSaveNoPathDWG[/SIZE][/INDENT][INDENT][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] FileExists [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Boolean[/COLOR][/SIZE][SIZE=2] = System.IO.File.Exists(strNewFile)[/SIZE][/INDENT][INDENT][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] FileExists = [/SIZE][SIZE=2][COLOR=#0000ff]True[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Then[/COLOR][/SIZE][/INDENT][INDENT][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] msg [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2] = ([/SIZE][SIZE=2][COLOR=#800000]"One or more detail(s) already exist in "[/COLOR][/SIZE][SIZE=2] & [/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].proj_dir.Path & [/SIZE][SIZE=2][COLOR=#800000]"."[/COLOR][/SIZE][SIZE=2] _[/SIZE][/INDENT][INDENT][SIZE=2]& vbCrLf & [/SIZE][SIZE=2][COLOR=#800000]"Are you sure you want overwrite existing detail(s)?"[/COLOR][/SIZE][SIZE=2])[/SIZE][/INDENT][INDENT][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] Response [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2] = MsgBox(msg, MsgBoxStyle.OkCancel, [/SIZE][SIZE=2][COLOR=#800000]"File(s) already exists"[/COLOR][/SIZE][SIZE=2])[/SIZE][/INDENT][INDENT][SIZE=2][COLOR=#0000ff]Select[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Case[/COLOR][/SIZE][SIZE=2] Response[/SIZE][/INDENT][INDENT][SIZE=2][COLOR=#0000ff]Case[/COLOR][/SIZE][SIZE=2] vbOK[/SIZE][/INDENT][INDENT][SIZE=2]IO.File.Delete(strNewFile)[/SIZE][/INDENT][INDENT][SIZE=2]System.IO.File.Copy(strFileToSave, strNewFile)[/SIZE][/INDENT][INDENT][SIZE=2][COLOR=#0000ff]Case[/COLOR][/SIZE][SIZE=2] vbCancel[/SIZE][/INDENT][INDENT][SIZE=2][COLOR=#0000ff]Exit[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][/INDENT][INDENT][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Select[/COLOR][/SIZE][/INDENT][INDENT][SIZE=2][COLOR=#0000ff]Else[/COLOR][/SIZE][/INDENT][INDENT][SIZE=2]System.IO.File.Copy(strFileToSave, strNewFile)[/SIZE][/INDENT][INDENT][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][/INDENT][INDENT][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].CheckedListBox1.SetItemCheckState(i, CheckState.Unchecked)[/SIZE][/INDENT][INDENT][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].CheckedListBox1.Refresh()[/SIZE][/INDENT][INDENT][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][/INDENT][INDENT][SIZE=2][COLOR=#0000ff]Next[/COLOR][/SIZE][/INDENT][INDENT][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][/COLOR]
[/INDENT]
 
Last edited:
good god, all that code is on one line, nobody's going be able to read it like that, i would suggest putting carrage returns and using spaces in there
 
That's what I thought when I pasted it. It's not that way in VS. Guess I'm not using the mark up codes on this site correctly.

Got my answer on another site. On that site you use [vb] and [\vb], I looked it up here and it's
VB.NET:
... but I must be using it wrong since it appears on one line.
 
Any suggestions for the future?
 
i just copy and paste the code from the IDE (or windows notepad, whichever i'm using at the time) and paste it in the box on the website then i type in the code tags around it and things appear normal
 
bjwade62, you can also edit your first post right now to make it readable for possible helpers. Going for the advanced forum posting editor also provides some buttons you can click to automatically insert the different code/post tags.
 
I really want to do as you asked but I'm still not getting it write. Two quaestions.

1. Why does my code show up in a very small window with scrollbars?

2. All the tabs (indents?) are missing. Do you have to add those manually?

Bernie
 
1. I have no idea, I've never seen anything like it. I think you should try again. Most posters manages to do this (with some trial and error?). There is also Preview available when writing/editing a post so you can make sure the post is how you want it. It is important for you to make a good post for voluntary helpers to bother with it.

2. 'Proper' indenting is a 'problem' because there is no board software that interprets the code and Html spaces are surpressed by the markup language standard behaviour. Different posting procedure and text attributes like font and color can make difference.
 
Back
Top