2 Replies - 846 Views - Last Post: 20 April 2009 - 09:55 AM Rate Topic: -----

#1 MrWobbles  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 31
  • View blog
  • Posts: 328
  • Joined: 11-April 08

Arrays and CheckListBoxes

Posted 20 April 2009 - 06:50 AM

I need to update my array when the user checks a box in a checked list box. I have been struggling with this for awhile and have it working almost perfectly. It adds items just fine, but when I try to remove them it won't do it until I check another box.

I have a CheckedListBox called fields, and a ListBox called included, and an array called fieldsArry.

Here is the code when the user checks a box:

	Private Sub fields_ItemCheck(ByVal sender As Object, ByVal e As System.Windows.Forms.ItemCheckEventArgs) Handles fields.ItemCheck
			'We do not want to call the updateFieldArray function when it is just loading up the fields  
			'because those will already be in our array, but we want to call it all other times.
		If reload = "No" Then
			updateFieldArray()
		End If
		reload = "No"
	End Sub



and updateFieldArray():

	Private Sub updateFieldArray()
		Dim i As Integer = 0

		While i < fields.Items.Count
			If fields.CheckedItems.Contains(fields.Items.Item(i)) Or fields.SelectedItem = 
																											 fields.Items.Item(i) Then
				If Not fieldsArry.Contains(fields.Items.Item(i)) Then
					fieldsArry.Add(fields.Items.Item(i))
					included.Items.Add(fields.Items.Item(i))
				End If
			ElseIf Not fields.CheckedItems.Contains(fields.Items.Item(i)) Then
				If fieldsArry.Contains(fields.Items.Item(i)) Then
					fieldsArry.Remove(fields.Items.Item(i))
					included.Items.Remove(fields.Items.Item(i))
				End If
			End If
			i = i + 1
		End While
	End Sub



I know why it is only working on one and not the other - my problem is figuring out how to get it to work on both. For whatever reason (this drives me crazy sometimes) when I check a box in the CheckedListBox it runs all the code I put in there before it even checks the box. So I can't just reference that box as a checked box until after all the code in the itemCheck event is finished. I remedied this in the updateFieldArray by including the currently selected item as a included item, which means that even when they are trying to uncheck something (remove it from the array) it is selected and will be included.

I'm afraid that I am getting to rambly, so if there are any questions then I would be happy to clear them up for you.

Is This A Good Question/Topic? 0
  • +

Replies To: Arrays and CheckListBoxes

#2 LemonMan  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 15
  • View blog
  • Posts: 170
  • Joined: 22-August 05

Re: Arrays and CheckListBoxes

Posted 20 April 2009 - 08:31 AM

Try this:

	Private Sub updateFieldArray()
For i As Integer = 0 to (fields.Items.Count - 1)
			If fields.CheckedItems.Contains(fields.Items.Item(i)) OrElse fields.SelectedItem = fields.Items.Item(i) Then
				If Not fieldsArry.Contains(fields.Items.Item(i)) Then
					fieldsArry.Add(fields.Items.Item(i))
					included.Items.Add(fields.Items.Item(i))
				End If
			End If
		 Next
For i As Integer =  (fields.Items.Count - 1) to 0 Step -1		   
If Not fields.CheckedItems.Contains(fields.Items.Item(i)) Then
				If fieldsArry.Contains(fields.Items.Item(i)) Then
					fieldsArry.RemoveAt(i)
					included.Items.RemoveAt(fields.Items.Item(i))
				End If
Next
	End Sub 



The reason there is one loop to handle adding and another to handle removing is to avoid errors. If you have to remove items 1 and 2, and you remove item 1 first, then when you remove item 2, you get an error because item 2 is now item 1. So you remove them in reverse.
Was This Post Helpful? 0
  • +
  • -

#4 Ian2009  Icon User is offline

  • New D.I.C Head

Reputation: 7
  • View blog
  • Posts: 31
  • Joined: 07-April 09

Re: Arrays and CheckListBoxes

Posted 20 April 2009 - 09:55 AM

View PostMrWobbles, on 20 Apr, 2009 - 05:50 AM, said:

...For whatever reason (this drives me crazy sometimes) when I check a box in the CheckedListBox it runs all the code I put in there before it even checks the box. So I can't just reference that box as a checked box until after all the code in the itemCheck event is finished. I remedied this in the updateFieldArray by including the currently selected item as a included item, which means that even when they are trying to uncheck something (remove it from the array) it is selected and will be included.

I'm afraid that I am getting to rambly, so if there are any questions then I would be happy to clear them up for you.


The ItemCheck event fires BEFORE an item's checkstate changes and the item is not in the CheckedItems collection at this point. But you can use e.NewValue to see what the new checkstate is going to be. There is no need to loop thru all items. try the following example
Public Class Form1
	Dim ArrF As ArrayList
	Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
		ArrF = New ArrayList(New String() {"A", "B", "C"})
		CheckedListBox1.Items.AddRange(ArrF.ToArray)
	End Sub
	Private Sub CheckedListBox1_ItemCheck(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ItemCheckEventArgs) Handles CheckedListBox1.ItemCheck
		If e.NewValue = CheckState.Checked Then
			ListBox1.Items.Add(CheckedListBox1.Items(e.Index))
		Else
			ListBox1.Items.Remove(CheckedListBox1.Items(e.Index))
		End If
	End Sub
End Class


Was This Post Helpful? 1

Page 1 of 1