Search code examples
vb.netcontrolsdocking

.NET Docking a control to another control


It is not possible to dock one control to another one, is it?

For example, if you have the option

"Hide application after" "(COMBOBOX)" "minutes"

in your application...

"Hide application after" is one label, then comes a combobox that lets the user select a decimal value, then comes the "minutes" label.

I find it a bit hard to set the sizes of these 3 items correctly. However, e. g. Thunderbird manages these well. I was wondering if I have to fallback on custom coding in VB.NET, or if there is an automatic way to do that.

Thanks!


Solution

  • You can use the FlowLayoutPanel control to house the three controls and allow them read from one control to the other:

    Protected Overrides Sub OnLoad(e As EventArgs)
      MyBase.OnLoad(e)
    
      flp.WrapContents = False
      flp.Controls.Add(New Label() With {.Text = "Hide Application After",
                                         .AutoSize = True,
                                         .Margin = New Padding(0, 6, 0, 0)})
      Dim cb As New ComboBox
      cb.DropDownStyle = ComboBoxStyle.DropDownList
      cb.Items.AddRange(New Object() {10, 20, 30})
      cb.Width = 42
      cb.SelectedIndex = 1
      flp.Controls.Add(cb)
    
      flp.Controls.Add(New Label() With {.Text = "Minutes",
                                         .AutoSize = True,
                                         .Margin = New Padding(0, 6, 0, 0)})
    End Sub
    

    Result:

    enter image description here