Create an grid of check boxes programatically

madaxe

Member
Joined
Mar 18, 2008
Messages
16
Programming Experience
1-3
I want to create a grid of checkboxes dynamically on my form for example the user may key in 16 which is a 4x4 grid of checkboxes or 81 which is a 9x9 grid.

my code seems to be generating the correct values but the checkboxes dont go to the correct location on the form

Thanks

Madaxe:mad:

VB.NET:
Public Class Form1

    Public NumberOfInputs As Integer = 81

    Public CheckBoxArray() As Control


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

        PopulateFormInputs()

    End Sub

    Private Sub PopulateFormInputs()

        Dim Index As Integer = 0
        Dim Counter As Integer = 9
        Dim VerticalPosition As Integer = 0
        Dim HorizontalPosition As Integer = 15
        Dim MyPoint As New Point

        For Index = 1 To NumberOfInputs

            If Counter Mod 9 = 0 Then

                VerticalPosition += 15
                HorizontalPosition = 15

            Else

                HorizontalPosition += 15

            End If

            MyPoint.Y = VerticalPosition
            MyPoint.X = HorizontalPosition

            Console.WriteLine(VerticalPosition & "-" & HorizontalPosition)

            ReDim Preserve CheckBoxArray(Index - 1)
            CheckBoxArray(Index - 1) = New MyCheckBox()


            Me.Controls.Add(CheckBoxArray(Index - 1))
            AddHandler CheckBoxArray(Index - 1).Click, AddressOf CheckBoxArray_Click

            With CheckBoxArray(Index - 1)

                .Text = ""
                .Location = MyPoint
                .Name = "CheckBox_" & Index

            End With

            Counter += 1

        Next

    End Sub

    Private Sub CheckBoxArray_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Click

        MsgBox(sender.Name)

    End Sub

End Class

Public Class MyCheckBox

    Inherits System.Windows.Forms.CheckBox

End Class
 
Last edited:
I got it to work by adding the size to each of the checkboxes? I dont know why it took this to make it work but it works

My next problem is the click event handler it triggers when I click on the form how can I make it work for just my check boxes

madaxe

VB.NET:
    Private Sub CheckBoxArray_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Click

        MsgBox(sender.Name)

    End Sub
 
With regards to your most recent question, the event is raised when you click on the form because you're handling the form's Click event:
VB.NET:
Private Sub CheckBoxArray_Click(ByVal sender As Object, ByVal e As System.EventArgs) [B][U][COLOR="red"]Handles Me.Click[/COLOR][/U][/B]
As for your original question, I would suggest using a TableLayoutPanel. You can add one to your form at design time and then set the number of rows and columns at run time. You simply add the controls to the container and they will automatically go to the correct cell, row first and then column.
 
Thanks for your help i am learning alot, i have only been using VB.NET for a little while. And your help is much appreciated.

Madaxe:)
 
Back
Top