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
my code seems to be generating the correct values but the checkboxes dont go to the correct location on the form
Thanks
Madaxe
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: