11 Replies - 37918 Views - Last Post: 15 September 2007 - 11:18 PM Rate Topic: -----

#1 Nolan  Icon User is offline

  • D.I.C Head

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

VB.NET - CheckBox Array - how to use it?

Posted 13 September 2007 - 11:00 AM

I'm not really sure how to implement the checkbox control array in .NET.
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.
Can someone please supply a sample snippet of code that will create an array of check boxes?

Here is my code, but I'm getting errors regarding "Use the 'NEW' keyword..."


'GET THE NUMBER OF LAYERS FROM THE MapControl
Dim NumLayers as Integer
NumLayers = AxMapControl1.LayerCount

'CREATE NEW CHECKBOX CONTROL ARRAY
Dim MyCheckboxes As New CheckBox()

'LOOP THROUGH THE LAYERS AND CREATE A CHECKBOX FOR EACH
Dim i As Integer
For i = 0 To NumLayers - 1 
'if the number of layers is 5 our Layer array will be 0 to 4

'CYCLE THROUGH AND POPULATE CHECKBOXES

MyCheckboxes(i).Visible = True
MyCheckboxes(i).Checked = True
MyCheckboxes(i).Text = AxMapControl1.Map.Layer(i).Name 

Next i



I know this is not correct, but am not sure how to do it...

Nolan

Is This A Good Question/Topic? 0
  • +

Replies To: VB.NET - CheckBox Array - how to use it?

#2 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: VB.NET - CheckBox Array - how to use it?

Posted 13 September 2007 - 11:20 AM

You need to declare the object before you can instantiate it when it comes to dynamic arrays. As you can see I modified one line and added another to your code below. You need to first tell it how many elements are in your array. Then as you cycle through each element you will then need to instantiate it with the NEW keyword.

Like this:
'CREATE NEW CHECKBOX CONTROL ARRAY
Dim MyCheckboxes(NumLayers) As CheckBox

'LOOP THROUGH THE LAYERS AND CREATE A CHECKBOX FOR EACH
Dim i As Integer
For i = 0 To NumLayers - 1
'if the number of layers is 5 our Layer array will be 0 to 4

MyCheckboxes(i) = new CheckBox

'CYCLE THROUGH AND POPULATE CHECKBOXES
MyCheckboxes(i).Visible = True

Was This Post Helpful? 0
  • +
  • -

#3 Nolan  Icon User is offline

  • D.I.C Head

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

Re: VB.NET - CheckBox Array - how to use it?

Posted 13 September 2007 - 11:50 AM

Ok I see how this is done.
Each checkbox is created on the fly as I cycle through the For/Next loop - correct?

I have made the changes, but the checkboxes do not "appear"

It would seem to be done something like this:
			 MyCheckboxes(i).Visible = True
			 MyCheckboxes(i).checked= True
			' MyCheckboxes(i).Location.X = 300
			'the line above is giving errors....



Any suggestions? I think I'm close?

<ModEdit>Please put your code in code tags :)</ModEdit>

This post has been edited by PsychoCoder: 15 September 2007 - 01:25 AM

Was This Post Helpful? 0
  • +
  • -

#4 Nolan  Icon User is offline

  • D.I.C Head

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

Re: VB.NET - CheckBox Array - how to use it?

Posted 13 September 2007 - 12:26 PM

I have added a msgbox to see if the loop is being run and it is:
But they're just not APPEARING on the form....????


Dim i As Integer
		For i = 0 To NumLayers - 1
			'if the number of layers is 5 our Layer array will be 0 to 4

			MyCheckboxes(i) = New CheckBox
		   
			[b]MsgBox(i) [/b]		'CHECK TO SEE IF IT'S ACTUALLY CREATING THESE CHECKBOXES
			'CYCLE THROUGH AND POPULATE CHECKBOXES
			MyCheckboxes(i).Visible = True
			MyCheckboxes(i).BringToFront()
			MyCheckboxes(i).Checked = True
			'MyCheckboxes(i).Location.X() = 300
		Next



<ModEdit>Please put your code in code tags :) </ModEdit>

This post has been edited by PsychoCoder: 15 September 2007 - 01:26 AM

Was This Post Helpful? 0
  • +
  • -

#5 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: VB.NET - CheckBox Array - how to use it?

Posted 13 September 2007 - 02:46 PM

Don't forget to add the controls to your form inside the loop.

Me.Controls.Add(MyCheckboxes(i))



To use the Location property you need to give it a Point. Where Xvalue/Yvalue is the X and Y coordinates.

Like this:
MyCheckboxes(i).Location = new Point(Xvalue, Yvalue)

Was This Post Helpful? 0
  • +
  • -

#6 Nolan  Icon User is offline

  • D.I.C Head

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

Re: VB.NET - CheckBox Array - how to use it?

Posted 14 September 2007 - 06:24 PM

Just wanted to say Thanks to the guys who posted replies - totally helped me out.

Here's the final code in case anyone wants to see the full implementation:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

		Dim Ypos As Integer	   'Need to move the position of the checkboxes down
		Dim NumLayers As Integer   ' This is the number of layers contained in the map

		NumLayers = AxMapControl1.Map.LayerCount
		'CREATE NEW CHECKBOX CONTROL ARRAY
		Dim MyCheckboxes(NumLayers) As CheckBox
		Dim i As Integer		' OUR COUNTER
		Ypos = 20				'Y POSITION OF THE FIRST CHECKBOX
		
	   For i = 0 To NumLayers - 1	'if the number of layers is 5 our Layer array will be 0 to 4

			MyCheckboxes(i) = New CheckBox

		  'CYCLE THROUGH AND POPULATE CHECKBOXES

			MyCheckboxes(i).Visible = True
			MyCheckboxes(i).Checked = True
			MyCheckboxes(i).Text = AxMapControl1.Map.Layer(i).Name
			Me.Controls.Add(MyCheckboxes(i))
			MyCheckboxes(i).Location = New Point(520, Ypos)	' X LOCATION NEVER CHANGES, Y POSITION DOWN BY 25
			Ypos = Ypos + 25
			'MyCheckboxes(i).Location.X() = 300
		Next

	End Sub




Once again thanks for your help - next question...

How do I check to see if one of MyCheckboxes has been clicked on?
I thought this might work:

Private Sub MyCheckboxes_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
MsgBox("Hey")
End Sub

But when I click on them nothing happens..... Not critical... just curious as to if these checkboxes are accessible or not

Nolan
Was This Post Helpful? 0
  • +
  • -

#7 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: VB.NET - CheckBox Array - how to use it?

Posted 14 September 2007 - 08:03 PM

You will need to use a loop to iterate through the array of checkboxes and see if the Checked property is true for each control in the array.
Was This Post Helpful? 0
  • +
  • -

#8 Nolan  Icon User is offline

  • D.I.C Head

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

Re: VB.NET - CheckBox Array - how to use it?

Posted 15 September 2007 - 10:44 AM

I've tried your idea using the following code, but
A) MyCheckBoxes_CheckChanged does not fire
B) MyCheckBoxes(i). "is not declared"
I'm assuming this is because the MyCheckboxes array is not recognized until run-time...

What I'm looking for is an Event that will be recognized

	Private Sub MyCheckboxes_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
		Dim i As Integer
		Dim numlayers As Integer
		numlayers = AxMapControl1.LayerCount
		For i = 0 To numlayers - 1
			If MyCheckBoxes(i).checked = False Then
				MsgBox("Hey")
			End If
		Next

	End Sub



I guess basically I'm looking to find out HOW to identify the checkbox array.
If I drop a checkbox onto the form, it's callled Checkbox1 and I can check it's Changed status with "CheckBox1_CheckedChanged", but the array is not recognized the same as it's built when the form loads...

This post has been edited by PsychoCoder: 15 September 2007 - 11:36 AM

Was This Post Helpful? 0
  • +
  • -

#9 PsychoCoder  Icon User is offline

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

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

Re: VB.NET - CheckBox Array - how to use it?

Posted 15 September 2007 - 11:38 AM

Your MyCheckBoxes_CheckChanged Event isnt firing because all control events have to have Handles ControlName.WhatEvent, like this

Private Sub MyCheckboxes_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyCheckboxes.CheckChanged
		Dim i As Integer
		Dim numlayers As Integer
		numlayers = AxMapControl1.LayerCount
		For i = 0 To numlayers - 1
			If MyCheckBoxes(i).checked = False Then
				MsgBox("Hey")
			End If
		Next

	End Sub



I will need to look into how to fire that event for a dynamically created control array, since control array's arent technically supported in VB.Net. Give me a little while to see if I can find a solution :)
Was This Post Helpful? 0
  • +
  • -

#10 PsychoCoder  Icon User is offline

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

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

Re: VB.NET - CheckBox Array - how to use it?

Posted 15 September 2007 - 11:45 AM

You need to add an event handler for each contrl, so you can tell which control was checked

Private Sub CreateCheckboxArray()

		Dim Ypos As Integer	   'Need to move the position of the checkboxes down
		Dim NumLayers As Integer   ' This is the number of layers contained in the map

		NumLayers = AxMapControl1.Map.LayerCount
		'CREATE NEW CHECKBOX CONTROL ARRAY
		Dim MyCheckboxes(NumLayers) As CheckBox
		Dim i As Integer		' OUR COUNTER
		Ypos = 20				'Y POSITION OF THE FIRST CHECKBOX
		
	   For i = 0 To NumLayers - 1	'if the number of layers is 5 our Layer array will be 0 to 4

			MyCheckboxes(i) = New CheckBox

		  'CYCLE THROUGH AND POPULATE CHECKBOXES

			MyCheckboxes(i).Visible = True
			MyCheckboxes(i).Checked = True
			MyCheckboxes(i).Text = AxMapControl1.Map.Layer(i).Name
			Me.Controls.Add(MyCheckboxes(i))
			'Add Event Handler to checkbox
			 AddHandler MyCheckboxes(i).CheckChanged, AddressOf Me.CheckChanged  <-- Add Event Handler
			MyCheckboxes(i).Location = New Point(520, Ypos)	' X LOCATION NEVER CHANGES, Y POSITION DOWN BY 25
			Ypos = Ypos + 25
			'MyCheckboxes(i).Location.X() = 300
		Next

	End Sub



Then in your check changed event

Private Sub MyCheckboxes_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) 
		Dim i As Integer
		Dim numlayers As Integer
		numlayers = AxMapControl1.LayerCount
		For i = 0 To numlayers - 1
			If MyCheckBoxes(i).checked = False Then
				MsgBox("Hey")
			End If
		Next

	End Sub



See if that helps you at all :)

This post has been edited by PsychoCoder: 15 September 2007 - 11:49 AM

Was This Post Helpful? 0
  • +
  • -

#11 Nolan  Icon User is offline

  • D.I.C Head

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

Re: VB.NET - CheckBox Array - how to use it?

Posted 15 September 2007 - 11:13 PM

Hey man I really appreciate your help!

Unfortunately, I'm not getting any joy with this code...
First problemo:

The AddHandlerl line is throwing this error:
'CheckChanged' is not a member of 'DesktopWindowsApplication1.Form1'.

Second (still...) in the CheckChanged routine:
Name 'MyCheckBoxes' is not declared.

Did I miss something?
Was This Post Helpful? 0
  • +
  • -

#12 PsychoCoder  Icon User is offline

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

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

Re: VB.NET - CheckBox Array - how to use it?

Posted 15 September 2007 - 11:18 PM

First, try changing this line

AddHandler MyCheckboxes(i).CheckChanged, AddressOf Me.CheckChanged



to
AddHandler MyCheckboxes(i).CheckChanged, AddressOf MyCheckboxes(i).CheckChanged



See if that gets rid of the. As for the error in the CheckChanged Sub, that is all your code, I just copied and pasted it straight from your post into mine. Try chaning the way the event handler is added and seeif that resolves both of the errors.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1