Results 1 to 2 of 2

Thread: [RESOLVED] Changing Runtime Generated Checkbox Names in Runtime Generated Groupbox

  1. #1

    Thread Starter
    Member
    Join Date
    Aug 2012
    Location
    Las Vegas, NV
    Posts
    41

    Resolved [RESOLVED] Changing Runtime Generated Checkbox Names in Runtime Generated Groupbox

    Hey Everybody,

    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:

    Code:
    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!

  2. #2

    Thread Starter
    Member
    Join Date
    Aug 2012
    Location
    Las Vegas, NV
    Posts
    41

    Resolved Re: Changing Runtime Generated Checkbox Names in Runtime Generated Groupbox

    The solution is as follows:

    Change:
    Code:
    Me.Controls(strCheckBoxOldName).Name = strCheckBoxNewName
    To:
    Code:
    Me.Controls(strControlName).Controls(strCheckBoxOldName).Name = strCheckBoxNewName
    The Me.Controls method only accesses the top level controls, therefore, I needed to access the control's controls collection to make name changes.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width