dcsimg
Results 1 to 7 of 7

Thread: Stop FLowLayoutPanel from repeat loading dynamic controls within a timer

  1. #1

    Thread Starter
    Member
    Join Date
    Jul 2011
    Posts
    51

    Stop FLowLayoutPanel from repeat loading dynamic controls within a timer

    Ok super simple code and I hope an easy question to answer

    I have a FLowLayoutPanel and in it generating a few GroupBoxes and in them a few labels.

    Works dandy by itself when called from say a Form1_Load

    But when I load the TestBox() sub from a timer, it keeps loading the same x groupboxes over and over

    what am I missing?

    FlowLayoutPanel1.Controls.Remove(gbNew) either doesn't do squat or completely blanks the panel depending on where I put it, so that isn't a solution.

    I don't seem to have the issue when I do the loop without a FlowLayoutPanel so it seems something inherent to that control

    Code is below


    Code:
    Public Class Form1
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Timer1.Start()
        End Sub
    
        Private Sub TestBox()
    
            Dim gbCount As Integer = 5
            Dim I As Integer
    
            For I = 0 To gbCount
    
                Dim gbNew As New GroupBox()
                Dim wlbNew As New Label()
                'FlowLayoutPanel1.Controls.Remove(gbNew)  ' doesn't do squat here and totally blanks before next
                FlowLayoutPanel1.Controls.Add(gbNew)
    
                gbNew.Controls.Add(wlbNew)
                wlbNew.Text = "Boogers"
                wlbNew.Visible = True
                wlbNew.Location = New System.Drawing.Point(230, 34)
                wlbNew.Font = New Font(wlbNew.Font.FontFamily, 6.5)
                wlbNew.BackColor = Color.Transparent
    
                gbNew.Size = New System.Drawing.Size(270, 80)
                'gbNew.BackColor = System.Drawing.Color.Silver
                gbNew.Visible = True
                gbNew.Text = "Tis ME" & " " & I
    
            Next
    
        End Sub
    
        Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
            TestBox()
        End Sub
    End Class
    Any assistance, long as it's VB and not another language I know even less, would be greatly appreciated.

    TIA

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,566

    Re: Stop FLowLayoutPanel from repeat loading dynamic controls within a timer

    Look at this code:
    Code:
                Dim gbNew As New GroupBox()
                Dim wlbNew As New Label()
                'FlowLayoutPanel1.Controls.Remove(gbNew)
    Why would you expect that third line to do anything useful? 'gbNew' refers to a new GroupBox that you created moments before and haven't even added to the FlowLayoutPanel. Think of it like this: you are making a cake and putting it in the oven, then an hour later you are making another cake. Can you remove that second cake from the oven? Of course not, because you haven't even put it in the oven yet. Would trying to remove the new cake somehow magically remove the cake that is already in the oven? Of course not, because it's a different cake. If you only want one cake in the oven at a time then obviously you have to remove the cake you made first from the oven, then you can put the new cake in. If you want to remove the GroupBox that you created last time then that's the one you have to remove, not the one you just created this time.

    That said, does it really make sense to remove all the controls you already created? Maybe it does but it would be more efficient to re-use them if possible.

  3. #3

    Thread Starter
    Member
    Join Date
    Jul 2011
    Posts
    51

    Re: Stop FLowLayoutPanel from repeat loading dynamic controls within a timer

    Quote Originally Posted by jmcilhinney View Post
    Look at this code:
    Code:
                Dim gbNew As New GroupBox()
                Dim wlbNew As New Label()
                'FlowLayoutPanel1.Controls.Remove(gbNew)
    Why would you expect that third line to do anything useful? 'gbNew' refers to a new GroupBox that you created moments before and haven't even added to the FlowLayoutPanel. Think of it like this: you are making a cake and putting it in the oven, then an hour later you are making another cake. Can you remove that second cake from the oven? Of course not, because you haven't even put it in the oven yet. Would trying to remove the new cake somehow magically remove the cake that is already in the oven? Of course not, because it's a different cake. If you only want one cake in the oven at a time then obviously you have to remove the cake you made first from the oven, then you can put the new cake in. If you want to remove the GroupBox that you created last time then that's the one you have to remove, not the one you just created this time.

    That said, does it really make sense to remove all the controls you already created? Maybe it does but it would be more efficient to re-use them if possible.
    Thanks for replying. I kinda get what you're saying but I don't what I need to remove. I have a FAR more complex version that is pulling info from my sql db and I need to refresh the info every second or two, which is why I have it in the loop.

    The # of boxes is dynamic as is the data I need to display in said boxes. So what would I need to put in that code above to either refresh the data within the controls or just redraw the controls by first removing the 1st cake so I can bake the 2nd? since I only want one cake in the oven at a time?

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,566

    Re: Stop FLowLayoutPanel from repeat loading dynamic controls within a timer

    Do you want to remove everything from the FLP? If so then just call the Clear method of its Controls collection.

    The issue with that is that it won't dispose them, which good practice dictates that you do. You should either recursively visit each control and dispose it or else store all the controls in another list that you can enumerate and dispose each item.

    If you need to access a control in the FLP's Controls collection in order to remove it and the only reference to it is in the Controls collection then you have to access it via the Controls collection.

  5. #5

    Thread Starter
    Member
    Join Date
    Jul 2011
    Posts
    51

    Re: Stop FLowLayoutPanel from repeat loading dynamic controls within a timer

    I tried using FlowLayoutPanel1.Controls.Clear() and while it did work, it was really ugly. It basically flashes the screen on each redraw and even when double buffered. Not good IS there a way I can create the dynamic layout as a static screen and just update the info being shown on it?

  6. #6
    New Member
    Join Date
    Nov 2014
    Posts
    1

    Re: Stop FLowLayoutPanel from repeat loading dynamic controls within a timer

    Hello,
    Im new here, and I have exactly same problems like thequestor had. Is any elegant way to implement such control, where you can dynamicaly add other control in it. I'm using FlowLayoutPanel, like thequestor did , and I'm adding other control in ths panel in a method, which is triggered by timer. IS working good, but problem is that is blinking. Do you hav eeny idea , how to make such things?

    thequestor, are you still alive? how u made this task?

    thanks

  7. #7

    Thread Starter
    Member
    Join Date
    Jul 2011
    Posts
    51

    Re: Stop FLowLayoutPanel from repeat loading dynamic controls within a timer

    Quote Originally Posted by martin.kavcic View Post
    Hello,
    Im new here, and I have exactly same problems like thequestor had. Is any elegant way to implement such control, where you can dynamicaly add other control in it. I'm using FlowLayoutPanel, like thequestor did , and I'm adding other control in ths panel in a method, which is triggered by timer. IS working good, but problem is that is blinking. Do you hav eeny idea , how to make such things?

    thequestor, are you still alive? how u made this task?

    thanks
    I wish I could answer this as it was over 2 years ago and my brain only holds thoughts for about 30 seconds I think I gave up and went a different route.

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
  •  



Featured


Click Here to Expand Forum to Full Width