Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi everyone

i have a winform that has many groupboxes in ,,
i want to hide some of these groupboxes ,,,
but the problem that when i hide one of them its location still empty ,,
is there an easy way to rearrange them dynamically so no empty places there ,,,
hope find hints >>
thank
Posted
Updated 5-Jan-14 11:58am
v2

1 solution

There are a few ways you could achieve this ...

For example: Calculate the positions on the fly ... this snippet works but isn't very tidy
VB
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim x, y As Integer
    x = 0   'Starting point
    y = 0

    For Each c As Control In Me.Controls
        If TypeOf (c) Is GroupBox Then
            c.Left = y
            c.Top = x
            If c.Visible Then
                If y + c.Width <= Me.Width - c.Width Then
                    y += c.Width
                Else
                    y = 0
                    x += c.Height
                End If
            End If
        End If
    Next

End Sub
It attempts to get a best fit based on space left. Disadvantage is that you can't control the order that the controls are returned (well you can but it is fiddly)

Or you could have a set of pre-defined locations in a list and just move the groupboxes into them e.g.
VB
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

 Dim locations As New List(Of System.Drawing.Point)
    Dim l As System.Drawing.Point = New System.Drawing.Point(1, 0)
    locations.Add(l)
    l = New System.Drawing.Point(98, 0)
    locations.Add(l)
    l = New System.Drawing.Point(195, 0)
    locations.Add(l)
    l = New System.Drawing.Point(1, 67)
    locations.Add(l)

    Dim i As Integer
    Dim j As Integer
    For i = 1 To 4
        Dim g As GroupBox = Me.Controls.Find("GroupBox" + i.ToString(), True)(0)
        If g.Visible Then
            g.Location = locations(j)
            j += 1
        End If
    Next
End Sub

NB - I've put all of the positions into this button click event - this is not the best approach. Nor did I use Imports System.Drawing
The second method depends on all of the Groupboxes being named with a suffix of 1, 2, 3, etc
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900