如何在运行时生成的组框中重命名运行时生成的复选框?

时间:2012-11-28 17:07:46

标签: vb.net checkbox vb.net-2010 groupbox

我在运行时生成的组框中更改运行时生成的复选框名称时遇到问题。我正在使用多个groupbox,然后获取数据库中的所有行并为它们创建复选框。复选框名称如下“”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

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

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

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

2 个答案:

答案 0 :(得分:1)

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

所以,而不是这样做:

Me.Controls(strCheckBoxOldName).Name = strCheckBoxNewName

你应该这样做:

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

答案 1 :(得分:0)

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