Using triggering events from Dynamically Created CheckBoxes and TextBoxes in vb

138 Views Asked by At

I am not a vb newbie, but I am no expert by any stretch. Frankly, I haven't messed with VB since VB6 in the late 90s I think. Now, I have a project that has me stumped.

I have been perusing stackoverflow since mid-August in search of a solution where a form has many dynamically created controls. In my project, I have a form with 130 textboxes and 130 checkboxes, and a couple odds and ends. Each textbox has an associated checkbox right next to it. Click events from checkboxes need to change the appearance of textboxes and checkboxes are enabled/disabled based on content of the textboxes.

On my first attempt at this, I have this working 100% where all the events are handled by individually by textboxes on-change and checkboxes on-click events. The code is clunky and difficult to update since any change to one thing has to be repeatedly... well 129 more times. Not to mention all the controls were placed on the form manually then positioned as I mentioned.

In my second attempt (shown below), I am trying to use dynamically created controls. My primary interest is the textbox and checkbox controls although there are others. So far, I have generated the form and everything looks great... now for making it work; i.e., sending change and click events to applicable code.

I have code (below) that can catch events - sorta as a set. But the issue is, the code I have catches all the events without pinpointing what indexed control triggered the event. I cannot figure out how to catch a specific control's event; i.e., checkbox(78).Checked... pass checked event to code that can then be used to update TextBox(78).

Here is what I have so far...

Public Class Form1

    Dim dynTextBox(129) As TextBox
    Dim BoxesList As New List(Of TextBox)
    Dim dynCheckBox(129) As CheckBox
    Dim CheckBoxList As New List(Of CheckBox) 

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

        Dim X, Y, YY As Integer
        YY = 0 'Adds the same amount of pixels to all calculations
        X = 20 : Y = 40 'Boxes starting position
        'Set up textboxes
        For j = 0 To 6
            For i = 0 To 19
                dynTextBox(i) = New TextBox
                With dynTextBox(i)
                    .Parent = Me
                    .Size = New Size(100, 50)
                    .Location = New Point(X, Y)
                    ' I removed formatted stuff since it does not apply
                    ' to my question
                    Me.Controls.Add(dynTextBox(i))
                    BoxesList.Add(dynTextBox(i))
                    'AddHandler dynTextBox(i).Click, AddressOf Clickbox
                End With
                Y += 35
                If (j * 20) + i = 129 Then i = 19
            Next
            Y = 40 + YY ' YY is used to move everything down if needed.
            X += 150
        Next

        'CheckBoxes starting position
        X = 122 : Y = 42 + YY
        'Setup 130 CheckBoxes
        For j = 0 To 6
            For i = 0 To 19
                dynCheckBox(i) = New CheckBox
                With dynCheckBox(i)
                    .Parent = Me
                    .Size = New Size(20, 20)
                    .Location = New Point(X, Y)
                    .Text = i.ToString
                    .Enabled = True ' This will be set to false later.
                                    ' Changing a related text box needs
                                    ' to enable the associated checkbox.
                    Me.Controls.Add(dynCheckBox(i))
                    'CheckBoxList.Add(dynCheckBox(i))
                    AddHandler dynCheckBox(i).CheckedChanged, AddressOf OnCheckBoxClick
                End With
                Y += 35
                If (j * 20) + i = 129 Then i = 19
            Next
            Y = 42 + YY ' YY is used to move everything down if needed.
            X += 150
        Next
    End Sub

    Sub OnCheckBoxClick(ByVal sender As System.Object, ByVal e As System.EventArgs)

        Dim i As Integer
        For i = 0 To 19
            If dynCheckBox(i).Text = sender.text Then
               MessageBox.Show(dynCheckBox(i).Text & " Item: " & (i + 1).ToString)
            End If
        Next
    End Sub
End Class

As you can see, I stopped with attempting to catch CheckBox clicks events. Since this is not working as I hoped, I did not try to also fail at catching TextBox change events. The "OnCheckBoxClick" subroutine squirts out an index number from the for loop (which seems useless) and position (between 1 and 20), but not that actual control's index from the dynCheckbox(i).

Humbly, pcitizen.

1

There are 1 best solutions below

0
PCitizen On

Here's the answer...

The issue was that I was indexing item in a loop. Each loop the item started over at 0 and went to 19. By adding a new cnt integer, this could be counted up by 1 after each button is created and also used as the index to the dynTextBox(cnt).