none
Noggin Scratcher RRS feed

  • Question

  • Hi Everyone,

    I have a Noggin Scratcher for you.

    I have a demonstration application that accesses four different database types. However, there is a different connection form for each.

    For educational purposes I wanted to see if I could make a form where I could dynamically change the connection criteria with a press of a button. Here is the base form.

    Clicking of the MS-Access button. You get the connection setup for MS-Access.

    And likewise when you click on the SQL Server button.

    This section is where I started polishing my noggin.  

    Private aryDynCtrls() As String = {"lblDB", "txtDB", "lblUser", _ "txtUser", "lblPW", "txtPW", _ "btnBrowse", "btnConnect", _ "btnCancel", "dgvRelationships", _ "lblServer", "txtServer", _ "lblAuthentication", "cboAuthentication", _ "lblLogin", "txtLogin", "lblPW", _ "txtPW", "lblCnnDB", "cboCnnDB", _ "btnConnect", "btnCancel"} Private Sub CLR_SwitchBrd() Dim I As Long For I = 0 To aryDynCtrls.Count - 1 Me.spltC.Panel1.Controls.RemoveByKey(aryDynCtrls(I)) Next 'Dim cControl As Control 'Dim strName As String 'For Each cControl In Me.spltC.Panel1.Controls 'strName = cControl.Name.ToString 'If cControl.Tag = "Dynamic" Then 'Me.spltC.Panel1.Controls.RemoveByKey(strName) 'End If 'Debug.Print(UCase(cControl.Name.ToString)) 'This is a dynamically created control 'Remove 'Debug.Print(vbTab & vbTab & "Delete Ctrl: " & cControl.Name.ToString) 'Me.spltC.Panel1.Controls.RemoveByKey(cControl.Name.ToString) 'End Select 'Next cControl End Sub

    The For Loop works as intended. It will remove all of the dynamically created controls by their name, which are listed in the aryDynCtrls array.

    This routine should be operationally equivalent, but it does not clear all the dynamically created controls.

    Private Sub CLR_SwitchBrd()
            'Dim I As Long
            'For I = 0 To aryDynCtrls.Count - 1
            'Me.spltC.Panel1.Controls.RemoveByKey(aryDynCtrls(I))
            'Next
    
            Dim cControl As Control
            Dim strName As String
            For Each cControl In Me.spltC.Panel1.Controls
                strName = cControl.Name.ToString
                If cControl.Tag = "Dynamic" Then
                    Me.spltC.Panel1.Controls.RemoveByKey(strName)
                End If
                Debug.Print(UCase(cControl.Name.ToString))
                'This is a dynamically created control
                'Remove   
                'Debug.Print(vbTab & vbTab & "Delete Ctrl: " & cControl.Name.ToString)
                Me.spltC.Panel1.Controls.RemoveByKey(cControl.Name.ToString)
            Next cControl
    End Sub
    

    Results of this routine for an MS-Access connection setup.

    Results for SQL Server connection setup.

    Can someone explain to me why I get such radically different results? I don't remember Visual Basic 6 Professional having such issues.

    Have Fun,


    MRM256

    Saturday, August 5, 2017 9:56 PM

Answers

  • When I woke up this morning the obvious occurred to me. I can use tab pages for the different log on forms and not worry about dynamic controls.

    It still doesn't answer the question why removing the dynamic controls behave so differently between the two different routines, but that is no longer an issue.

    Sometimes we get so caught up in our own cleverness that we ignore the obvious.

    Thanks,


    MRM256

    • Marked as answer by MRM256 Sunday, August 6, 2017 2:53 PM
    Sunday, August 6, 2017 2:53 PM

All replies

  • For educational purposes I wanted to see if I could make a form where I could dynamically change the connection criteria with a press of a button. Here is the base form.

    You should not modify a collection when you iterate over it using a For Each.

    Change you code to

            For I As Integer = Me.spltC.Panel1.Controls.Count - 1 to 0 Step -1
                Dim cControl As Control = Me.spltC.Panel1.Controls(I)
                strName = cControl.Name.ToString
                If cControl.Tag = "Dynamic" Then
                    Me.spltC.Panel1.Controls.RemoveByKey(strName)
                End If

    If you are only interested in removing the dynamic controls then the better structure is to create your own list of the dynamic controls you create for each form when you create them, and then use that list to determine what controls need to be deleted. Also, you can avoid creating and deleting controls by creating panels with the controls already placed in the designer, ensuring that the panels exactly overlay each other, and then showing whichever panel the user requests for their login as the topmost panel.


    • Edited by Acamar Saturday, August 5, 2017 10:09 PM fmt
    • Proposed as answer by Frank L. Smith Saturday, August 5, 2017 11:15 PM
    Saturday, August 5, 2017 10:09 PM
  • This sounds like a better solution. Do you have any links or tutorials where I can research this further?

    Thank you,


    MRM256

    Sunday, August 6, 2017 12:30 AM
  • This sounds like a better solution. Do you have any links or tutorials where I can research this further?

    If the suggested change to your code solved your problem then you should indicate that by marking one or more of the responses as an answer.

    If you want to change the way you are implementing your solution and have questions about that, then that should be a new question with new example code.

    Sunday, August 6, 2017 1:22 AM
  • When I woke up this morning the obvious occurred to me. I can use tab pages for the different log on forms and not worry about dynamic controls.

    It still doesn't answer the question why removing the dynamic controls behave so differently between the two different routines, but that is no longer an issue.

    Sometimes we get so caught up in our own cleverness that we ignore the obvious.

    Thanks,


    MRM256

    • Marked as answer by MRM256 Sunday, August 6, 2017 2:53 PM
    Sunday, August 6, 2017 2:53 PM