Results 1 to 8 of 8

Thread: [RESOLVED] Dynamically added controls can't be replaced by new ones

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2018
    Posts
    514

    Resolved [RESOLVED] Dynamically added controls can't be replaced by new ones

    Hi
    I have an issue
    Based on a selected item in a combobox, a panel will be created and a few controls will be added to that panel. All good on the fist selection. however, when the combo is changed, the initially created control are still there even though I think that in my code I have disposed them adn added new ones.
    can someone please help t see what I cam doing wrong and how I can make it work.
    Thanks.
    here my code:

    Code:
    Private Sub CB_Register_IC_Receiver_SelectionChangeCommitted(sender As Object, e As EventArgs) Handles CB_Register_IC_Receiver.SelectionChangeCommitted
    
            Call SetTheValuesForDetailPanels(ThisSite, ThisArea, PL_Register_IC, "Register IC")
    
    End Sub
    
    Public Shared Sub SetTheValuesForDetailPanels(ThisSite As String, ThisArea As String, ThisContainer As Control, Where As String)
    
            Dim MyQuery As String = ""
    
            Dim ThisPanel As Panel = Form1.PL_Register_IC.Controls.Find("PL_Receiver_Detail1_" & ThisSite, True).FirstOrDefault()
    
            If Not ThisPanel Is Nothing Then
                While ThisPanel.Controls.Count > 0
                    ThisPanel.Controls(0).Dispose()
                End While
            End If
    
            'add new panel
            Dim PL_Detail1 As New Panel
            PL_Detail1.Name = "PL_Receiver_Detail1_" & ThisSite
            PL_Detail1.Tag = "Detail1_" & ThisSite
            PL_Detail1.Size = New Size(280, 199)
            PL_Detail1.Location = New Point(510, 3)
            'PL_Detail1.Dock = DockStyle.Top
    
            'add group box for items
            Dim GB_Items As New GroupBox
            GB_Items.Name = "GB_Register_IC_CanSendItems"
            GB_Items.Tag = "ItemAvilable"
            GB_Items.Text = "Defect items available and can be sent?"
            GB_Items.Size = New Size(240, 35)
            GB_Items.Location = New Point(3, 3)
    
            ' add radio buttons for group box
            Dim RB_Items_Yes As New RadioButton
            RB_Items_Yes.Name = "RB_Register_IC_Defect_Yes"
            RB_Items_Yes.Tag = "Yes"
            RB_Items_Yes.Text = "Yes"
            RB_Items_Yes.AutoSize = True
            RB_Items_Yes.Location = New Point(12, 14)
    
            Dim RB_Items_No As New RadioButton
            RB_Items_No.Name = "RB_Register_IC_Defect_No"
            RB_Items_No.Tag = "No"
            RB_Items_No.Text = "No"
            RB_Items_No.AutoSize = True
            RB_Items_No.Location = New Point(61, 14)
    
            ' add the radios to group box
            GB_Items.Controls.Add(RB_Items_Yes)
            GB_Items.Controls.Add(RB_Items_No)
    
            ' add the group box to form 1
            PL_Detail1.Controls.Add(GB_Items)
    
            ' add labels
            Dim ProductLBL As New Label
            ProductLBL.Text = GetLabelTextFromDB("ProductType", ThisSite, ThisArea)
            ProductLBL.Tag = "ProductType"
            ProductLBL.AutoSize = True
            ProductLBL.Location = New Point(4, 40)
    
            Dim ComponentLBL As New Label
            ComponentLBL.Text = GetLabelTextFromDB("Component", ThisSite, ThisArea)
            ComponentLBL.Tag = "Component"
            ComponentLBL.AutoSize = True
            ComponentLBL.Location = New Point(4, 79)
    
            Dim ErrorTypeLBL As New Label
            ErrorTypeLBL.Text = GetLabelTextFromDB("ErrorType", ThisSite, ThisArea)
            ErrorTypeLBL.Tag = "ErrorType"
            ErrorTypeLBL.AutoSize = True
            ErrorTypeLBL.Location = New Point(4, 118)
    
            Dim SupplierLBL As New Label
            SupplierLBL.Text = GetLabelTextFromDB("Supplier", ThisSite, ThisArea)
            SupplierLBL.Tag = "Supplier"
            SupplierLBL.AutoSize = True
            SupplierLBL.Location = New Point(4, 157)
    
            ' add labels to panel
            PL_Detail1.Controls.Add(ProductLBL)
            PL_Detail1.Controls.Add(ComponentLBL)
            PL_Detail1.Controls.Add(ErrorTypeLBL)
            PL_Detail1.Controls.Add(SupplierLBL)
    
            ' linked label
            Dim NoProductLBL As New LinkLabel
            NoProductLBL.Text = "Why no product is available?"
            NoProductLBL.Tag = "WhyNoProduct"
            NoProductLBL.Location = New Point(144, 40)
    
            ' Add combos
            Dim ProductCB As New ComboBox
            ProductCB.Name = "CB_Register_IC_ProductType" & ThisSite
            ProductCB.Tag = "ProductType"
            ProductCB.DropDownStyle = ComboBoxStyle.DropDownList
            ProductCB.Size = New Size(240, 21)
            ProductCB.Location = New Point(4, 56)
    
            MyQuery = "Select Distinct Product From DV_Arena_ProductAndComponent 
                             Where 
                           Site = '" & ThisSite & "' And 
                           Area = '" & ThisArea & "' And 
                           ActiveStatus= 'Yes' 
                          Order By Product ASC"
    
            Call GetThingsInComboBox_PreDefined(ProductCB, MyQuery, {}, "", ProductLBL, GetLabelTextFromDB("ProductType", ThisSite, ThisArea) & ": ")
    
            If Not NoProductLBL Is Nothing Then
                If ProductCB.Items.Count = 0 Then
                    NoProductLBL.Visible = True
                Else
                    NoProductLBL.Visible = False
                End If
            End If
    
            Dim ComponentCB As New ComboBox
            ComponentCB.Name = "CB_Register_IC_Component" & ThisSite
            ComponentCB.Tag = "Component"
            ComponentCB.DropDownStyle = ComboBoxStyle.DropDownList
            ComponentCB.Size = New Size(240, 21)
            ComponentCB.Location = New Point(4, 95)
    
            Dim ErrorTypeCB As New ComboBox
            ErrorTypeCB.Name = "CB_Register_IC_ErrorType" & ThisSite
            ErrorTypeCB.Tag = "ErrorType"
            ErrorTypeCB.DropDownStyle = ComboBoxStyle.DropDownList
            ErrorTypeCB.Size = New Size(240, 21)
            ErrorTypeCB.Location = New Point(4, 134)
    
            Dim SupplierCB As New ComboBox
            SupplierCB.Name = "CB_Register_IC_Supplier" & ThisSite
            SupplierCB.Tag = "Supplier"
            SupplierCB.DropDownStyle = ComboBoxStyle.DropDownList
            SupplierCB.Size = New Size(240, 21)
            SupplierCB.Location = New Point(4, 173)
    
            MyQuery = "Select Distinct ListItemName from DV_Arena_ListItems Where 
                            listitemtype = 'Supplier' And 
                            Site = '" & ThisSite & "' And 
                            Area = '" & ThisArea & "' And  
                            ActiveStatus= 'Yes' Order By ListItemName ASC"
    
            Call GetThingsInComboBox_PreDefined(SupplierCB, MyQuery, {}, "", SupplierLBL, GetLabelTextFromDB("Supplier", ThisSite, ThisArea) & ": ")
    
            ' add combox to panel
            PL_Detail1.Controls.Add(ProductCB)
            PL_Detail1.Controls.Add(ComponentCB)
            PL_Detail1.Controls.Add(ErrorTypeCB)
            PL_Detail1.Controls.Add(SupplierCB)
    
            'add a picture box
            Dim Pbox_ErrorType As New PictureBox
            Pbox_ErrorType.Tag = "ErrorType"
            Pbox_ErrorType.Image = My.Resources.info_button
            Pbox_ErrorType.Size = New Size(15, 15)
            Pbox_ErrorType.SizeMode = PictureBoxSizeMode.StretchImage
            Pbox_ErrorType.Location = New Point(229, 117)
            AddHandler Pbox_ErrorType.Click, AddressOf MyViewRBClicked
    
            'add the picturebox to the panel
            PL_Detail1.Controls.Add(Pbox_ErrorType)
    
            'add the panel to the control on form1
            Form1.PL_Register_IC.Controls.Add(PL_Detail1)
    
    End Sub

  2. #2
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    Re: Dynamically added controls can't be replaced by new ones

    Try removing them instead.

    Also, why are you going to all that trouble? I don't see anything in there that necesitates the need for removing & re-adding controls like that.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2018
    Posts
    514

    Re: Dynamically added controls can't be replaced by new ones

    Thanks, I will do that. So, remove and then dispose?
    And yes, the necessity is not shown here to simplify; each selection will result in different set of controls within that panel. Any suggestion for more simplification?

  4. #4
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,458

    Re: Dynamically added controls can't be replaced by new ones

    Quote Originally Posted by Grand View Post
    Thanks, I will do that. So, remove and then dispose?
    And yes, the necessity is not shown here to simplify; each selection will result in different set of controls within that panel. Any suggestion for more simplification?
    You could create a UserControl for each related set of controls, that way you would only need to add or remove a UserControl to the panel when the selection changes. a USerControl would also allow you to encapsulate any UI interactions behaind properties / methods as well.

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    Re: Dynamically added controls can't be replaced by new ones

    I think what the damp one suggested is probably the best answer, but another one would be to put the controls on a panel (I realize they're already on one panel, but this would be a panel within the panel). You might have one set of controls on one panel and a different set of controls on a different panel. Set the Top property to something like -2000 for both, and they'll be out of sight. Then just set the Top property to the right location when you want it.

    Essentially, the controls are always there, you're just shifting the ones you want into view when you need them.
    My usual boring signature: Nothing

  6. #6
    Fanatic Member Delaney's Avatar
    Join Date
    Nov 2019
    Location
    Paris, France
    Posts
    845

    Re: Dynamically added controls can't be replaced by new ones

    I usually use a GroupBox to do that so I hide/unhide, enable/disabled the group box and everything in it in the same time.
    The best friend of any programmer is a search engine
    "Don't wish it was easier, wish you were better. Don't wish for less problems, wish for more skills. Don't wish for less challenges, wish for more wisdom" (J. Rohn)
    “They did not know it was impossible so they did it” (Mark Twain)

  7. #7
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    Re: Dynamically added controls can't be replaced by new ones

    I'll use Tabs & TabPages for that sometimes... then hide all the tabs, and only show one at a time when needed.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  8. #8

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2018
    Posts
    514

    Re: Dynamically added controls can't be replaced by new ones

    Thanks for the suggestions. I actually started with having multiple panels and make them visible one at the time when they were selected but then the collection increased to over 100 panels; also that it was a manual work and I had to add a new panel each time a new item needed to be added. So I thought to go dynamic.

    I also found that beside that I was not removing the controls (only disposing), I also was not searching for the old panel to be removed. So, each time a new item was selected from the combo I needed to remove the old panel with the old name but I took for the newly selected item from the combo and look for the controls that belong to the new selection which was not created yet and that was the issue of not being able to remove the old controls.

    in this project, not all panels have the same controls, the variation is set up in some admin section when a new member needs to join the system. based on that variation then the controls also vary when the corresponding item is selected from the conmbo that represent the member area. So, I guess user control won't help either?
    Last edited by Grand; Sep 23rd, 2021 at 02:20 AM.

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