0

我在运行时生成的组框中更改运行时生成的复选框名称时遇到问题。我正在使用多个组框,然后获取数据库中的所有行并为它们创建复选框。复选框名称如下""chkClass" & intGroupBoxNumber & intCurrentRow"。删除组框后,我会重新编号所有当前的组框,并希望复选框名称也更改为新的组框编号,如果这有意义的话。我的代码如下:

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 

我很确定我的麻烦现在存在于 Me.Controls(strCheckBoxOldName).Name = strCheckBoxNewName

错误如下:“对象引用未设置为对象的实例”

是否有不同的方法来引用该运行时生成的组框?

谢谢。对不起,如果这令人困惑!

4

2 回答 2

1

表单的Controls集合仅包含直接放置在表单上的顶级控件。如果将控件加载到容器控件中,例如 a GroupBox,则必须在该容器控件的Controls集合中找到它。

所以,而不是这样做:

Me.Controls(strCheckBoxOldName).Name = strCheckBoxNewName

你应该做这样的事情:

Me.Controls(strControlName).Controls(strCheckBoxOldName).Name = strCheckBoxNewName
于 2012-11-28T17:41:34.593 回答
0

我会暂时注释掉 try/catch 块。然后应用程序将在违规语句处中断,您不必只是“非常确定”问题出在哪里。然后,检查相关变量的值(突出显示,shfit-F9)。您可能会发现其中一个对象的值为零。这会导致您提到的错误消息。

于 2012-11-28T17:39:56.620 回答