0 votes

Accommodate automatically controls in a panel or form, vb

I have to add by code a certain amount of controls to a panel, this control contains only a button, the question is how do I make this control fits automatically to the load, I did it but with if nested the problem is that we do not know how you can get to increase the amount of load, since this depends on the amount of products that drive the company, here is the code.

Private Sub btnAgregar_Click(sender As Object, e As EventArgs) Handles btnAgregar.Click
    Dim ctrl As New CtrlMesa
    Dim x, y, ctrls As Integer
    ctrl.btnMesa.Text = "Mesa " & Mesas.Controls.Count
    If Mesas.Controls.Count <= 10 Then
        ctrls = Mesas.Controls.Count
        x = Mesas.Controls.Item(ctrls - 1).Location.X
        y = Mesas.Controls.Item(ctrls - 1).Location.Y
        ctrl.Location = New Point(x + 85, y)
        Mesas.Controls.Add(ctrl)
        Me.Close()
    ElseIf Mesas.Controls.Count > 10 And Mesas.Controls.Count < 22 Then

        If Mesas.Controls.Count = 11 And Mesas.Controls.Count < 22 Then
            ctrl.Location = New Point(10, 95)
            Mesas.Controls.Add(ctrl)
            Me.Close()
        ElseIf Mesas.Controls.Count > 11 Then
            ctrls = Mesas.Controls.Count
            x = Mesas.Controls.Item(ctrls - 1).Location.X
            y = Mesas.Controls.Item(ctrls - 1).Location.Y
            ctrl.Location = New Point(x + 85, y)
            Mesas.Controls.Add(ctrl)
            Me.Close()
        End If
    ElseIf Mesas.Controls.Count >= 22 And Mesas.Controls.Count < 33 Then
        If Mesas.Controls.Count = 22 Then
            ctrl.Location = New Point(10, y + 180)
            Mesas.Controls.Add(ctrl)
            Me.Close()
        ElseIf Mesas.Controls.Count > 22 Then
            ctrls = Mesas.Controls.Count
            x = Mesas.Controls.Item(ctrls - 1).Location.X
            y = Mesas.Controls.Item(ctrls - 1).Location.Y
            ctrl.Location = New Point(x + 85, y)
            Mesas.Controls.Add(ctrl)
            Me.Close()
        End If

    ElseIf Mesas.Controls.Count >= 33 And Mesas.Controls.Count <= 43 Then
        If Mesas.Controls.Count = 33 Then
            ctrl.Location = New Point(10, y + 265)
            Mesas.Controls.Add(ctrl)
            Me.Close()
        ElseIf Mesas.Controls.Count > 32 Then
            ctrls = Mesas.Controls.Count
            x = Mesas.Controls.Item(ctrls - 1).Location.X
            y = Mesas.Controls.Item(ctrls - 1).Location.Y
            ctrl.Location = New Point(x + 85, y)
            Mesas.Controls.Add(ctrl)
            Me.Close()
        End If
    End If
End Sub

As you can see I have a limit of 43 items, which I agg more but also that is not the way :(

Note: Table is the Form

1voto

Henry Rodriguez Points 447

I recommend you make use of the FlowLayoutPanel that way only you would need to add the CtrlMesa to the FlowLayoutPanel, and this would match dynamic-mind:

Private Sub btnAgregar_Click(sender As Object, e As EventArgs) Handles btnAgregar.Click

    Dim ctrl As New CtrlMesa

    ctrl.btnMesa.Text = "Mesa " & Mesas.Controls.Count      

    'FlowLayoutPanel1 debe ser el control que actue como contenedor de las mesas

    FlowLayoutPanel1.Controls.Add(ctrl);        

    Me.Close()

End Sub

HolaDevs.com

HolaDevs is an online community of programmers and software lovers.
You can check other people responses or create a new question if you don't find a solution

Powered by:

X