Table of Checkboxes

MyForumID

Active member
Joined
Sep 25, 2005
Messages
26
Programming Experience
Beginner
Table of Checkboxes [RESOLVED]

Hello,

I am new to VB.Net and have a program in Excel VBA that I want to convert. I currently don't have any forms and just use the spreadsheet to get all the option inputs.

I was hoping someone could please explain how to make a table of checkboxes that can be addressed by row and column rather than manually addressing over 150 different boxes. I would also like to have header boxes for each row and column that will automatically fill in the appropriate row/column.

I tried learning about datagrids but every explanation I've seen has had these tied in to databases, which I'm not using. Is what I'd like to do possible or is the pain a necessary evil?

Thank you very much in advance.
 
Last edited:
what would be the purposes ive made a table of 900 checkboxes on a program i made.... Describe the commands of them and exactly what your talking about and i can give some pointers
 
You could create a UserControl that did the work for you. You could give it a constructor that took a row and column count and created all the desired CheckBoxes and then allowed you to index them by row and column.
 
Thank you for your replies.

{IP}Gil-Galad:

The table is a table of options that gives a user option to do something for one total vs another total. So all I need from that table is a simple true/false that can be addressed by it's table location. I'd like them to be toggled by the row and column as well.

The second table I'll be making will be the same total vs total scenario but will need 3 different options so will likely be better served with a table of drop boxes that could also be toggled by row/column.


jmcilhinney:

Is this hard to do? How would one address the boxes if they aren't being addressed by name? Does this mean you can have an array of boxes?


Oh, and I had one more question. My program handles a large amount of data and I was limited in VBA by the size of my structures and memory limitation so I had to limit the amount of analysis I wanted to do on the data. Do these same limitations hold for VB.Net or can I go nuts and save more information since I have to switch to classes?

Thanks again.
 
MyForumID said:
jmcilhinney:

Is this hard to do? How would one address the boxes if they aren't being addressed by name? Does this mean you can have an array of boxes?
Of course. You can have an array of whatever type you want. Your UserControl could contain a snippet of code similar to this:
VB.NET:
Private m_Boxes As CheckBox(,)

Public ReadOnly Property Box(ByVal row As Integer, ByVal column As Integer) As CheckBox
	Get
		Return Me.m_Boxes(row, column)
	End Get
End Property

Public Sub New(ByVal rows As Integer, ByVal columns As Integer)
	ReDim Me.m_Boxes(rows - 1, columns - 1)

	For row As Integer = 0 To rows - 1 Step 1
		For column As Integer = 0 To columns - 1 Step 1
			Dim box As New CheckBox

			'Set properties here, e.g. Size and Location.

			'Add the CheckBox to the array so it can be accessed by index.
			Me.m_Boxes(row, column) = box

			'Add the CheckBox to the Controls collection so it is visible.
			Me.Controls.Add(box)
		Next column
	Next row
End Sub
 
You rock!!!

Thank you very much! I just renamed the "New" function to "CreateCheckBoxArray" and left everything else untouched and it worked great :)

I also changed the add function to add it to a group and I make the coordinates a function of the row # and column #. I didn't even bother to name any of the boxes.

Like I said I am very new to VB.Net since I've only used (and taught myself) VBA for Excel and I was wondering if you don't mind explaining a couple of things before I change this to resolved if you don't mind.

How does the program know that "Dim box As New CheckBox" refers to a brand new box everytime since it's always called "box"? I've never seen a Dim statement in the middle of for/next loops. What would happen if I did that with something like "Dim num as integer" - would it be a whole bunch of new integer variables all named "num". If I said num = 5, how would you be able to know which one was being addressed? Do the old ones disappear into the ether with each next statement?

What is the purpose/function of this function and the "ReadOnly" and "Property" descriptions:

VB.NET:
Public ReadOnly Property Box(ByVal row As Integer, ByVal column As Integer) As CheckBox
	Get
		Return Me.m_Boxes(row, column)
	End Get
End Property

I tried taking it out and it seemed to work fine since I didn't call it.

Sorry for all the questions but I'm very curious. I've just begun the process of converting my very ugly, 200+ pages of VBA code into VB.Net and I'm hoping to make it better and have more memory available to do more with the data it's generating so this is very helpful. BTW - do you know if there are similar memory restrictions in VB.Net to those of VBA?

Thank you again very much!
 
Last edited:
If you're new to .NET then you are probably new to object-oriented programming too. A CheckBox is a reference-type object, which means that the object itself is created in one area of memory (the heap) and a CheckBox variable in your code is created somewhere else (the stack). The variable does not contain the object itself, but rather it contains the memory address of the object, which is called a reference. Each time you assign a New CheckBox to the variable, you are creating a new CheckBox object in memory and storing its address in the variable, but the old object still exists. That way you are able to use the same variable each time to create a new CheckBox and then store its address elsewhere if you like, like in the array. Note that the array is filled with the memory addresses of all the CheckBoxes you created. Note that the "box" variable is re-declared each time through the loop, so it is not strictly the same variable. The same result could also be achieved like this:
VB.NET:
Public Sub New(ByVal rows As Integer, ByVal columns As Integer)
	ReDim Me.m_Boxes(rows - 1, columns - 1)
	Dim box As CheckBox

	For row As Integer = 0 To rows - 1 Step 1
		For column As Integer = 0 To columns - 1 Step 1
			box = New CheckBox

			'Set properties here, e.g. Size and Location.

			'Add the CheckBox to the array so it can be accessed by index.
			Me.m_Boxes(row, column) = box

			'Add the CheckBox to the Controls collection so it is visible.
			Me.Controls.Add(box)
		Next column
	Next row
End Sub
or this
VB.NET:
Public Sub New(ByVal rows As Integer, ByVal columns As Integer)
 	ReDim Me.m_Boxes(rows - 1, columns - 1)
 
 	For row As Integer = 0 To rows - 1 Step 1
 		For column As Integer = 0 To columns - 1 Step 1
 			'Add the CheckBox to the array so it can be accessed by index.
 			Me.m_Boxes(row, column) = New CheckBox

  			'Set properties here, e.g. Size and Location.
 
 			'Add the CheckBox to the Controls collection so it is visible.
 			Me.Controls.Add(Me.m_Boxes(row, column))
 		Next column
 	Next row
 End Sub

A property is a new concept for .NET. It allows you to create an interface that behaves like a variable from the outside but like a method from the inside. A property usually has Get and Set methods. When using the property from outside the object you treat it just like a variable. The Checked property of the CheckBox is an example. Inside the object, you can include as much processing in the Get and Set methods as you like, so it behaves like a pair of methods. When you declare a property as ReadOnly you don't create a Set method. This means you can get the value of the property from the outside but you can't assign a value to it. I included this because I had intended that this code be used in a UserControl. If you have added the code directly to a form then the property is not needed, as you've discovered.
 
Thank you again very much for your help and explanations. No need to reply to the below but thought I'd share.

The latter code makes more sense to me. I don't know if this is right or not (it's been over 15 years since my intro to Pascal class), but I'll think of the "box" variable as a pointer. I don't exactly remember the details about what a pointer is, but I remember the concept and it seems to fit. It seems to me that the memory object pointed to by "box" could theoretically get "lost" if you went through the loop without pointing the array to the address of each one as it is created since with every next it's being redirected to a new reference. I wonder if the program would be able to release the memory if you ran it without the re-assignment when you closed the program...

It seems I have a lot more homework to do since I tried reading the help file on "Get" and it was like trying to read Russian :) I also don't know what a method is, what friends and enemies are, why I'm supposed to care if my code is public or private since no one else is going to see it, etc... I read a book right after my Pascal class about C++ on my own so I have a very elementary, 15 year-old and distorted understanding of object-oriented programming, but like I said, I have a lot of homework to do during this conversion process. For now I'll skip using "get" though since I don't seem to need it. It's amazing though what VBA and Visual studios VB.Net have let me do so far with only a primitive programming background.

Anways, I'll let you go and if you've read this you're probably cringing, but thank you again very much. That was exactly what I needed and I'll use the same code for my other table with the listboxes as well :)
 
Back
Top