CheckedListBox - Is it Checked or Unchecked?

Can't seem to access the Checked state of a checklistbox

Page 1 of 1

6 Replies - 116686 Views - Last Post: 02 October 2007 - 07:51 PM Rate Topic: -----

#1 Nolan  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 52
  • Joined: 12-September 07

CheckedListBox - Is it Checked or Unchecked?

Post icon  Posted 02 October 2007 - 05:05 PM

Hi there.

I had posted code earlier regarding how to create a check box array (like in older VB).
The topic is here: http://www.dreaminco...wtopic33191.htm

Basic idea is this:
I am using an ARCGIS map control which has "Layers" of spatial data (i.e. Roads, Creeks, Rivers, etc.)
When I load the form and the map control, I want to create a Dynamic Table of Contents in the form of Check Boxes in order to control whether the Layers will be visible or not.

The original idea to use SIMPLE Check Boxes did not work, although I got very close.
I have found that using the alternative "CheckedListBox" works just as well:

This code loads the checkedlistbox and sets all boxes to checked when the form loads:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
		
		Dim NumLayers As Integer
		NumLayers = AxMapControl1.LayerCount
		MsgBox(NumLayers)
		Dim i As Integer
		For i = 0 To NumLayers - 1
			CheckedListBox1.Items.Add(AxMapControl1.Map.Layer(i).Name, True)
		Next i
		   
	End Sub



This code SHOULD check to see what the checked state is and if it's CHECKED, then UNCHECK it, otherwise, CHECK IT

Private Sub CheckedListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckedListBox1.SelectedIndexChanged

		If CheckedListBox1.SelectedIndex() = checked Then
			'uncheck it
			CheckedListBox1.SelectedIndex(0) = unchecked

		Else
			'must already be unchecked, so check it
			CheckedListBox1.SelectedIndex() = checked

		End If

	End Sub



Now I know THIS is not working, but I am not sure exactly how to implement what I want.
I did find (don't remember where, the CHECKSTATE method, but was not able to implement it to any satisfaction.

Any ideas (I'll thank Psychocoder in advance...)

Nolan

Is This A Good Question/Topic? 1
  • +

Replies To: CheckedListBox - Is it Checked or Unchecked?

#2 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1642
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: CheckedListBox - Is it Checked or Unchecked?

Posted 02 October 2007 - 05:47 PM

That's actually a little trickier than most would think. To toggle the check state of an item in a CheckedListBox you need to first get the index of the selected item, then determine if it's -1 (nothing is checked), if it's not -1 then use the GetItemCheckState Property of the CheckedListBox to get the check state of the item (this is done in a loop from 0 to CheckedListBox.Items.Count - 1), then reverse the check state using the SetItemCheckState Property of the CheckedListBoxbased on the current check state, like so

Private Sub CheckedListBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) _
 Handles CheckedListBox1.SelectedIndexChanged
		' Get the selected item index.
		Dim selectedIndex As Integer = CheckedListBox1.SelectedIndex
		If (selectedIndex <> -1) Then
			For i As Integer = 0 To CheckedListBox1.Items.Count - 1
				' Get the selected item's check state.
				Dim chkstate As CheckState
				chkstate = CheckedListBox1.GetItemCheckState(i)
				' Toggle the item state.
				If (chkstate = CheckState.Checked) Then
					chkstate = CheckState.Unchecked
				Else
					chkstate = CheckState.Checked
				End If
				' Set the new state.
				CheckedListBox1.SetItemCheckState(i, chkstate)
			Next
		End If
End Sub



Hope this helps :)
Was This Post Helpful? 3

#3 Nolan  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 52
  • Joined: 12-September 07

Re: CheckedListBox - Is it Checked or Unchecked?

Posted 02 October 2007 - 06:20 PM

Thanks Psycho. That did help.
Didn't really need the loop though... maybe you misunderstood what I was after.
Once I have access to SELECTEDINDEX, I'm off to the races.
The code you supplied definitely got me going in the right direction. (by the way, something was ??possibly?? backwards in your code... If I clicked on 1 checkbox, every OTHER checkbox would UNCHECK, and the one I clicked on would remain CHECKED, and vice-versa...)
Anyway, here's what I ended up doing (commented out some of the original code)

	   ' Get the selected item index.
		Dim selectedIndex As Integer = CheckedListBox1.SelectedIndex
		MsgBox(selectedIndex)

		If (selectedIndex <> -1) Then
			'
			'DON'T NEED THIS LOOP
			'For i As Integer = 0 To CheckedListBox1.Items.Count - 1
			
		   ' Get the selected item's check state.
			Dim chkstate As CheckState
			chkstate = CheckedListBox1.GetItemCheckState(selectedIndex)


			' Toggle the item state.
			If (chkstate = CheckState.Checked) Then
				chkstate = CheckState.Unchecked
			Else
				chkstate = CheckState.Checked
			End If
		   

			 'DIDN'T NEED THIS BIT EITHER
			 ' Set the new state.
			'CheckedListBox1.SetItemCheckState(i, chkstate)
			' Next
		End If




You're the MAN!!

Thanks again -
Nolan
Was This Post Helpful? 0
  • +
  • -

#4 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1642
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: CheckedListBox - Is it Checked or Unchecked?

Posted 02 October 2007 - 07:02 PM

Oh it wasn't backwards, thats code to allow only one selection in the list, but I thought the logic would help you.
Was This Post Helpful? 1

#5 Nolan  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 52
  • Joined: 12-September 07

Re: CheckedListBox - Is it Checked or Unchecked?

Posted 02 October 2007 - 07:41 PM

Hey what can you tell me about this:
			
					   Dim chkstate As CheckState
					  chkstate = CheckedListBox1.GetItemCheckState(selectedIndex)

					  MsgBox(chkstate) 'returns 0 if box WAS checked and BECOMES unchecked...

					 If (chkstate = CheckState.Checked) Then
				chkstate = CheckState.Unchecked
				AxMapControl1.Map.Layer(selectedIndex).Visible = True
				AxMapControl1.Refresh()


			Else
				chkstate = CheckState.Checked
				AxMapControl1.Map.Layer(selectedIndex).Visible = False
				AxMapControl1.Refresh()

			End If



Every time I look at this I expect the results to be:
Click on a checkbox and if it's checked, uncheck it, then set the layer's visible property to TRUE, and vice versa.
This is completely freaking me out, because what ACTUALLY happens is that the Layer(selectedindex) VISIBILITY is OPPOSITE of what I expect... Does this make sense? When I uncheck a checkbox, the map layer dissappears, BUT that's NOT what the code reads.... am I missing something about checkstate?

The code is ''' working '''' but I'd really like to know WHY...

Thanks.
Nolan

This post has been edited by Nolan: 02 October 2007 - 07:43 PM

Was This Post Helpful? 0
  • +
  • -

#6 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1642
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: CheckedListBox - Is it Checked or Unchecked?

Posted 02 October 2007 - 07:46 PM

From looking at your code it looks like it does what you're describing. First check the state, if its checked then uncheck it and show the layer, otherwise check it and hide the layer. Thats how I'm understanding you code, is that correct?
Was This Post Helpful? 0
  • +
  • -

#7 Nolan  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 52
  • Joined: 12-September 07

Re: CheckedListBox - Is it Checked or Unchecked?

Posted 02 October 2007 - 07:51 PM

View PostPsychoCoder, on 2 Oct, 2007 - 07:46 PM, said:

From looking at your code it looks like it does what you're describing. First check the state, if its checked then uncheck it and show the layer, otherwise check it and hide the layer. Thats how I'm understanding you code, is that correct?


I think you understand exactly what I expect the code to do, however, it's not doing it that way.
When a checkbox becomes UNCHECKED, the layer dissappears, when I CHECK it, it REAPPEARS.

It's what I want to have happen, but THE CODE looks like it should work as you described....

Any idea why?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1