VB 2005 Resizing controls automatically on a form

Hi

I have a number of ComboBoxes on a FlowLayoutPanel that are added at runtime.
I want them to resize automatically when the form is resized by the user.

I have experimented with the anchor property but either I am not doing it correctly or it isn't the best option.

Any help would be appreciated.

Thanks
Murray BrownHead DeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

SanclerCommented:
As you don't say what you are doing, it's hard to know where you might be going wrong.  But this might help

   http://msdn2.microsoft.com/en-us/library/ms171633.aspx

or look for

   How to: Anchor and Dock Child Controls in a FlowLayoutPanel Control

in the help file

Roger
Murray BrownHead DeveloperAuthor Commented:
Hi Roger

All I am trying to do is set a ComboBoxes properties at runtime so that it changes size proportionately with the FlowLayoutPanel that
it is in.

Thanks for the first response but there is so much in there that I can't find specifics

Murray
SanclerCommented:
Murray

Can you please post the code by which you are adding the comboboxes at runtime?  And, if you are adding the FlowLayoutPanel at runtime, the code for that, too?  If not, can you copy and paste the code relating to the FlowLayoutPanel from the <yourform>.Designer.vb file?

I haven't yet personally used the FlowLayoutPanel with controls added at runtime, but if we can see what you've got someone might be able to help you.

Roger
Learn SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

Murray BrownHead DeveloperAuthor Commented:
Hi Roger

I don't have access to all the code but all I have done start a new form, then add a FlowLayoutPanel, and anchored it at top, left, bottom and right.
I called it fl

Then I have added a row of Comboboxes at runtime using using a button click event

Dim A1 As New ComboBox
Dim B1 As New ComboBox
Dim C1 As New ComboBox

fl.Controls.Add(A1)
fl.Controls.Add(B1)
fl.Controls.Add(C1)

The user then can add as many rows of buttons, one row per click.

If the user wants to resize the form all I want to know is how to make the form resize with the FlowLayoutPanel, which resizes with the form because it is anchored.

If I try and anchor the ComboBoxes they just slip further down, but do not resize.

I hope this is clear

Thanks

Murray



SanclerCommented:
Murray

Here's some code.  All you need is a form with a FlowLayoutPanel - named fl - on it.

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim A1 As New ComboBox
        Dim B1 As New ComboBox
        Dim C1 As New ComboBox


        fl.Controls.Add(A1)
        fl.Controls.Add(B1)
        fl.Controls.Add(C1)

        getWidths()

    End Sub

    Private Sub getWidths() 'how wide are they to start with
        For Each ctl As Control In fl.Controls
            If TypeOf ctl Is ComboBox Then
                ctl.Tag = ctl.Width / fl.Width 'use Tag to store factor
            End If
        Next
    End Sub

    Private Sub putWidths() 'maintain the same proportion
        For Each ctl As Control In fl.Controls
            If TypeOf ctl Is ComboBox Then
                ctl.Width = ctl.Tag * fl.Width
            End If
        Next
    End Sub

    Private Sub fl_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles fl.Resize
        putWidths()
    End Sub

It uses essentially the same approach as for resizing controls within any other container control.  It means you are not using the specific "wrapping on resizing" features of the FlowLayoutPanel, but I assume it is what you want.

Similar effects might be achievable with Anchor, Dock, Padding, Margin but - for me - this seemed a lot easier.

Roger

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Murray BrownHead DeveloperAuthor Commented:
Hi Roger

Thanks very much for taking the time out to put that together.

Looks great!

Very much appreciated!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.