As mentioned with other projects, in VB.Net, arrays of controls are easy to create and manipulate. Actually, a Form has an array of various objects on it.
Every object on the form has an Index number. The controls placed at design time have a fixed index number. The index numbers of Controls created with code at run time follow the last control index from design time.
We can loop through objects with the Index number, a number at the end of an object name or even objects with completely different names. If I can loop through objects then that's an array.
Every object on the form has an Index number. The controls placed at design time have a fixed index number. The index numbers of Controls created with code at run time follow the last control index from design time.
We can loop through objects with the Index number, a number at the end of an object name or even objects with completely different names. If I can loop through objects then that's an array.
The control array on this form consists of a Label and a Button. Those 2 controls have the index numbers 0 and 1. We'll create a control array in code now. Double click the form. At the moment it's size is 660 x 565. This will take some time as I'm still thinking how to code this project.
Private Sub frmWordFinder_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim intCol As Integer
Dim intRow As Integer
Dim intSize As Integer = 20 'Height and width of the Labels + distance from left of form
Dim intTop As Integer = 40 'Distance of Labels from the top of the form
'Create the letter labels in a 16 rows x 16 columns grid
For intRow = 0 To 15
For intCol = 0 To 15
Dim myLabel As New Label
myLabel.Font = New Font(myLabel.Font, FontStyle.Bold)
myLabel.TextAlign = ContentAlignment.MiddleCenter
myLabel.Left = intSize + intCol * intSize - intCol
myLabel.Top = intTop + intRow * intSize - intRow
myLabel.Size = New Size(intSize, intSize)
myLabel.BorderStyle = BorderStyle.FixedSingle
Me.Controls.Add(myLabel)
'Nominate the click procedure for the labels just created
AddHandler myLabel.Click, AddressOf Me.myLabel_click
'Record the Index of the first Label
If intCol = 0 And intRow = 0 Then
intFirstLabel = Me.Controls.IndexOf(myLabel)
End If
Next intCol
Next intRow
The code is pinched from another project. A difference is that I want to know the index number of the first label.
We better write the Handler procedure so the above recognizes it. First we need some global variables. Actually, do all this before, or as writing, the above procedure.
Public Class frmWordFinder
'Global Variables
Dim intRow As Integer
Dim intCol As Integer
Dim intFirstLabel As Integer
Sub myLabel_click(sender As Object, e As System.Windows.Forms.MouseEventArgs)
Dim intControl As Integer
intControl = CStr(Controls.IndexOf(CType(sender, Label)))
intRow = Math.Floor((intControl - intFirstLabel) / 16) + 1
intCol = (intControl - intFirstLabel) Mod 16 + 1
End Sub
The code to calculate intRow took quite a while for me to get right. I haven't had to use Math.Floor before. While testing the code I had the following extra lines. I remmed them for later use maybe.
Me.Text = intControl
lblTitle.Text = intRow
btnNewGame.Text = intCol
I need the row and column numbers to work out whether words will fit the grid.
Private Sub frmWordFinder_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim intCol As Integer
Dim intRow As Integer
Dim intSize As Integer = 20 'Height and width of the Labels + distance from left of form
Dim intTop As Integer = 40 'Distance of Labels from the top of the form
'Create the letter labels in a 16 rows x 16 columns grid
For intRow = 0 To 15
For intCol = 0 To 15
Dim myLabel As New Label
myLabel.Font = New Font(myLabel.Font, FontStyle.Bold)
myLabel.TextAlign = ContentAlignment.MiddleCenter
myLabel.Left = intSize + intCol * intSize - intCol
myLabel.Top = intTop + intRow * intSize - intRow
myLabel.Size = New Size(intSize, intSize)
myLabel.BorderStyle = BorderStyle.FixedSingle
Me.Controls.Add(myLabel)
'Nominate the click procedure for the labels just created
AddHandler myLabel.Click, AddressOf Me.myLabel_click
'Record the Index of the first Label
If intCol = 0 And intRow = 0 Then
intFirstLabel = Me.Controls.IndexOf(myLabel)
End If
Next intCol
Next intRow
The code is pinched from another project. A difference is that I want to know the index number of the first label.
We better write the Handler procedure so the above recognizes it. First we need some global variables. Actually, do all this before, or as writing, the above procedure.
Public Class frmWordFinder
'Global Variables
Dim intRow As Integer
Dim intCol As Integer
Dim intFirstLabel As Integer
Sub myLabel_click(sender As Object, e As System.Windows.Forms.MouseEventArgs)
Dim intControl As Integer
intControl = CStr(Controls.IndexOf(CType(sender, Label)))
intRow = Math.Floor((intControl - intFirstLabel) / 16) + 1
intCol = (intControl - intFirstLabel) Mod 16 + 1
End Sub
The code to calculate intRow took quite a while for me to get right. I haven't had to use Math.Floor before. While testing the code I had the following extra lines. I remmed them for later use maybe.
Me.Text = intControl
lblTitle.Text = intRow
btnNewGame.Text = intCol
I need the row and column numbers to work out whether words will fit the grid.