locked
FlowLayoutPanel and Child controls RRS feed

  • Question

  • I have a FlowLayoutPane with AutoScroll property set to true.

    I want to add child Buttons, but i have a problem when ScrollBars appears i want that Buttons resize itself so it'll show only the Vertical Scrollbar and not both Vertical and Horizontal ScrollBars.

    FlowDirection is set to TopDown and Button's margin is set to Padding.Empty.

    Ideas?
    Wednesday, August 13, 2008 11:54 PM

Answers

  • I solved. Wink

     

    What do you think about this approach?

     

     

    Code Snippet

    Private Sub FlowLayoutPanel1_ControlAdded(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ControlEventArgs) Handles FlowLayoutPanel1.ControlAdded

    Dim w As Integer = FlowLayoutPanel1.Width

    If FlowLayoutPanel1.HorizontalScroll.Visible Then

    w -= System.Windows.Forms.SystemInformation.VerticalScrollBarWidth

    End If

    For Each f As Control In FlowLayoutPanel1.Controls

    If f.Width <> w Then f.Width = w

    Next

    End Sub

     

     

    Thursday, August 14, 2008 11:13 AM

All replies

  • Hmm, some assumptions and questions are needed here:

    1. You populate the FlowLayoutPanel (FLP) dynamically with buttons, that is - from your code.
    2. Is the FLP docked within a form?
    3. Is the form resizeable?

    Anyway, guessing: Yes, Yes, Yes then:

    Just set the WrapContents property to false.  That will prevent the horizontal scrollbar to appear, given that the default width of the buttons are smaller than the default width of the FLP and its form.  (Anyway, believe you will hav problems anyway to make it too small (in width), manually it can't be resized too small.)

    If you still want some resizing, you have to catch the appropriate event.  If you mean manually resizing the FLP and form, just catch that event (the Resize event), if you want to check it every time a button is added, catch the ControlAdded event.  In the handlers of these just change the form/FLP anyway you want.


    - terje
    Thursday, August 14, 2008 12:41 AM
  • The problem still perxists

    Thursday, August 14, 2008 12:51 AM
  • Can you post some of your code?

    I just tested it with the following code:

     
    Code Snippet

    for (int i=0;i<10;i++)

    {


                    Button b = new Button() {Name = i.ToString(), Text = "Btn" + i};

                    flowLayoutPanel1.Controls.Add(b);

                }


          and with the FLP docked within the form.     
    Thursday, August 14, 2008 1:38 AM
  •  

    My FLP is docked into a Panel Docked at Left.

     

    My Issue is that i want my button with Width Resized also when there is scrollbar and i want only VScrool showing and not HScroll

    Thursday, August 14, 2008 10:48 AM
  • I solved. Wink

     

    What do you think about this approach?

     

     

    Code Snippet

    Private Sub FlowLayoutPanel1_ControlAdded(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ControlEventArgs) Handles FlowLayoutPanel1.ControlAdded

    Dim w As Integer = FlowLayoutPanel1.Width

    If FlowLayoutPanel1.HorizontalScroll.Visible Then

    w -= System.Windows.Forms.SystemInformation.VerticalScrollBarWidth

    End If

    For Each f As Control In FlowLayoutPanel1.Controls

    If f.Width <> w Then f.Width = w

    Next

    End Sub

     

     

    Thursday, August 14, 2008 11:13 AM
  • After having the same problem I came upon another approach which is less resource hungry.

    If when adding the controls you set all except the first one with an anchor property of right and left

    ie:

    for i = 0 to 9
     dim btn as new button
     if i <> 0 then btn.Anchor = CType(AnchorStyles.Left Or System.Windows.Forms.AnchorStyles.Right, AnchorStyles)
    next

    Then in the FlowLayoutPanel_ControlAdded Event you merely have to use one line of code being:

    flowlayoutpanel1.controls(0).width = flowlayoutpanel1.clientrectangle.width

    With FLP's any controls anchored the way i mentioned assume the same width as the first control that has a set width (or height in a left-right layout)

    setting the controls width to the clientrectangle sets the width to the working area of the control so it subtracts the scrollbar width automatically if present.

    I know you solved the problem but thought this may interest you

    Regards

    Louis

    Thursday, August 28, 2008 9:47 PM