1

I have 2 panels aligned next to each other, as shown in the screenshot. Each panel consists of 3-4 controls.

Screenshot of Panels alignment

What I am trying to achieve is - When the form size changes, the size of the panels should should auto adjust, maintaining the the constant gap between the 2 panels.

So far, I have tried various anchor combinations of the two panels, but could not get the desired result. I am kind of stuck here. (I am working in Visual Basic 2013)

Dr. Atul Tiwari
  • 1,085
  • 5
  • 22
  • 46
  • 1
    Sounds like a task for the [TableLayoutPanel](http://msdn.microsoft.com/en-us/library/system.windows.forms.tablelayoutpanel(v=vs.110).aspx). – Bjørn-Roger Kringsjå Jan 11 '15 at 11:43
  • @Bjørn-RogerKringsjå I first tried with TableLayoutPanel only, but for some unidentified reasons, I was Not able to insert multiple controls in a single cells. – Dr. Atul Tiwari Jan 11 '15 at 11:47
  • @Bjørn-RogerKringsjå, I tried again after your mention, and I can confirm that multiple controls can't be inserted in a single cells. However **I achieved my desired result**, by keeping controls in a panel (as seen in screenshot of original question), and then keeping every panel in the cell of a TableLayoutPanel. But, **I don't know if this is the actual solution or has any drawback?** Can you please tell me should I proceed with this? Thanks for the idea :) – Dr. Atul Tiwari Jan 11 '15 at 11:59
  • No you can't. But you don't need more than one control. You just need to add a docked panel to the first and third cell. – Bjørn-Roger Kringsjå Jan 11 '15 at 12:02

2 Answers2

1
  • Drag & drop a TableLayoutPanel onto the form and set the Dock style to Fill.
  • Remove last row and add a new column.
  • Set the column style/width of the first and third column to percent 50%.
  • Set the column style/width of the second column to absolute 20.
  • Drag & drop a panel into the first and third cell and set the Dock style to Fill.
  • Place whatever controls you need inside these two panels.

Also, I suggest you read the following SO post on how to improve TLP rendering:

Sample form:

Public Class Form1

    Public Sub New()
        Me.InitializeComponent()
    End Sub

    Private Sub InitializeComponent()
        Me.TableLayoutPanel1 = New System.Windows.Forms.TableLayoutPanel()
        Me.Panel1 = New System.Windows.Forms.Panel()
        Me.Panel2 = New System.Windows.Forms.Panel()
        Me.TableLayoutPanel1.SuspendLayout()
        Me.SuspendLayout()
        '
        'TableLayoutPanel1
        '
        Me.TableLayoutPanel1.ColumnCount = 3
        Me.TableLayoutPanel1.ColumnStyles.Add(New System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50.0!))
        Me.TableLayoutPanel1.ColumnStyles.Add(New System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 20.0!))
        Me.TableLayoutPanel1.ColumnStyles.Add(New System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50.0!))
        Me.TableLayoutPanel1.Controls.Add(Me.Panel1, 0, 0)
        Me.TableLayoutPanel1.Controls.Add(Me.Panel2, 2, 0)
        Me.TableLayoutPanel1.Dock = System.Windows.Forms.DockStyle.Fill
        Me.TableLayoutPanel1.Location = New System.Drawing.Point(0, 0)
        Me.TableLayoutPanel1.Name = "TableLayoutPanel1"
        Me.TableLayoutPanel1.RowCount = 1
        Me.TableLayoutPanel1.RowStyles.Add(New System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 100.0!))
        Me.TableLayoutPanel1.Size = New System.Drawing.Size(682, 260)
        Me.TableLayoutPanel1.TabIndex = 1
        '
        'Panel1
        '
        Me.Panel1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
        Me.Panel1.Dock = System.Windows.Forms.DockStyle.Fill
        Me.Panel1.Location = New System.Drawing.Point(3, 3)
        Me.Panel1.Name = "Panel1"
        Me.Panel1.Size = New System.Drawing.Size(325, 254)
        Me.Panel1.TabIndex = 0
        '
        'Panel2
        '
        Me.Panel2.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
        Me.Panel2.Dock = System.Windows.Forms.DockStyle.Fill
        Me.Panel2.Location = New System.Drawing.Point(354, 3)
        Me.Panel2.Name = "Panel2"
        Me.Panel2.Size = New System.Drawing.Size(325, 254)
        Me.Panel2.TabIndex = 1
        '
        'Form1
        '
        Me.AutoScaleDimensions = New System.Drawing.SizeF(8.0!, 16.0!)
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.ClientSize = New System.Drawing.Size(682, 260)
        Me.Controls.Add(Me.TableLayoutPanel1)
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.TableLayoutPanel1.ResumeLayout(False)
        Me.ResumeLayout(False)

    End Sub

    Friend WithEvents TableLayoutPanel1 As System.Windows.Forms.TableLayoutPanel
    Friend WithEvents Panel1 As System.Windows.Forms.Panel
    Friend WithEvents Panel2 As System.Windows.Forms.Panel

End Class
Community
  • 1
  • 1
Bjørn-Roger Kringsjå
  • 9,849
  • 6
  • 36
  • 64
  • Superb, basic same thing I tried to say in my last comment on my question. But your representation is best. – Dr. Atul Tiwari Jan 11 '15 at 12:21
  • 1
    @Dr.AtulTiwari Great! You should also visit the link a added in my edit. – Bjørn-Roger Kringsjå Jan 11 '15 at 12:22
  • I looked into the link, you provided, but I couldn't get it to convert to visual basic. I don't know C#. I tried online converters, but all of them returned code with lots of error, which I was unable to resolve. Can you help me how NOT slow down tablelayoutpanel. thanks. – Dr. Atul Tiwari Jan 22 '15 at 09:35
  • I used http://converter.telerik.com/ without any problems. Just be sure to call `BeginUpdate` / `EndUpdate` in the `ResizeBegin` / `ResizeEnd` events of the form. – Bjørn-Roger Kringsjå Jan 22 '15 at 10:57
  • thanks. with your help I have converted the code successfully. Eventually I came to know, I forgot to import `System.Runtime.InteropServices` for `DllImport`, but still I was not able to implement it into `ResizeBegin`, so I thought now it should be asked as a separate question. Its link is [Link](http://stackoverflow.com/questions/28104271/tablelayoutpanel-responds-very-slowly-to-events-in-visual-basic-2013) – Dr. Atul Tiwari Jan 23 '15 at 06:37
0

just set the dock of left panel to left and right one to right!