I am having an issue changing runtime generated checkbox names in runtime generated groupboxes. I am using multiple groupboxes, and then taking all of the rows in a database and creating checkboxes for them. The checkbox names are as follows ""chkClass" & intGroupBoxNumber & intCurrentRow". Upon deletion of a groupbox, I renumber all of the current group boxes, and would like the checkbox names to change as well to the new groupbox number, if this makes any sense. My code is as follows:

strControlName = "grpGroup" & strIBResult
        Try
            intGroupBoxOldYLocation = Me.Controls(strControlName).Location
            Me.Controls(strControlName).Dispose()
            MessageBox.Show("Deleted: " & strControlName)
            intRenameGroup = strIBResult + 1

            Try
                strControlName = "grpGroup" & intRenameGroup
                strControlNewName = "grpGroup" & intRenameGroup - 1
                Me.Controls(strControlName).Location = intGroupBoxOldYLocation
                Me.Controls(strControlName).Text = "Group " & intRenameGroup - 1
                Me.Controls(strControlName).Name = strControlNewName
                MessageBox.Show("Changed: " & strControlName & " to: " & strControlNewName)

                Do While intCurrentClassRow < intTotalClassRows
                    strCheckBoxOldName = "chkClass" & intRenameGroup & intCurrentClassRow
                    strCheckBoxNewName = "chkClass" & intRenameGroup - 1 & intCurrentClassRow
                    MessageBox.Show("Renaming: " & strCheckBoxOldName & " to: " & strCheckBoxNewName)
                    Me.Controls(strCheckBoxOldName).Name = strCheckBoxNewName
                    intCurrentClassRow += 1
                    MessageBox.Show("Renamed: " & strCheckBoxOldName & " to: " & strCheckBoxNewName)
                Loop

                intCurrentClassRow = 0
                intRenameGroup += 1
                intGroupBoxNewYIncrement = intGroupBoxOldYLocation.Y + Me.Controls(strControlNewName).Height + 50

                Do
                    strControlName = "grpGroup" & intRenameGroup
                    strControlNewName = "grpGroup" & intRenameGroup - 1
                    Me.Controls(strControlName).Location = New Point(intCurrentXPosition, intGroupBoxNewYIncrement)
                    Me.Controls(strControlName).Text = "Group " & intRenameGroup - 1
                    Me.Controls(strControlName).Name = strControlNewName

                    Do While intCurrentClassRow < intTotalClassRows
                        strCheckBoxOldName = "chkClass" & intRenameGroup & intCurrentClassRow
                        strCheckBoxNewName = "chkClass" & intRenameGroup - 1 & intCurrentClassRow
                        Me.Controls(strCheckBoxOldName).Name = strCheckBoxNewName
                        intCurrentClassRow += 1
                        MessageBox.Show("Renamed: " & strCheckBoxOldName & " to: " & strCheckBoxNewName)
                    Loop

                    intCurrentClassRow = 0
                    intRenameGroup += 1
                    intGroupBoxNewYIncrement = intGroupBoxNewYIncrement + Me.Controls(strControlNewName).Height + 50
                Loop

            Catch ex As Exception
                MessageBox.Show("Control: " & strControlName & " does not exist")
                MessageBox.Show(ErrorToString)
            End Try

        Catch ex As Exception
            'MessageBox.Show("Control: " & strControlName & " never existed")
            MessageBox.Show("Please enter a valid group number to delete.", "Invalid Entry")
            Exit Sub
        End Try 

I am pretty sure my trouble now exists at the Me.Controls(strCheckBoxOldName).Name = strCheckBoxNewName

The error is as follows: "Object reference not set to an instance of an object"

Is there a different method to reference that runtime generated groupbox?

Thanks. Sorry if this is confusing!

有帮助吗?

解决方案

The form's Controls collection only contains the top-level controls that are placed directly on the form. If you load a control into a container control, such as a GroupBox, you must find it in that container control's Controls collection.

So, instead of doing this:

Me.Controls(strCheckBoxOldName).Name = strCheckBoxNewName

You should be doing something like this:

Me.Controls(strControlName).Controls(strCheckBoxOldName).Name = strCheckBoxNewName

其他提示

I would temporarily comment out the try/catch blocks. Then the application will break at the offending statement and you won't have to be just "pretty sure" where the problem is. Then, check the values of the associated variables (highlight, shfit-F9). You'll probably find that one of the objects has a value of nothing. This causes the error message you mentioned.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top