Sloth Armstrong
Sloth Armstrong

Reputation: 1066

How do I properly apply padding to a group box control?

I have a class that inherits a panel which I am adding a group box to. This group box contains for now a couple text boxes. I would like to have the text boxes centered horizontally and vertically within the group box by using the AutoSize property of the group box and the Padding property of the group box. Here is my attempt:

Imports System.Drawing

Public Class pnlItemMstr_A_OSI
    Inherits Panel

    Public Sub New(ByRef ItemMstr_DS As DataSet, ByVal padding As Integer)
        MyBase.New()

        Dim drItemMstr As DataRow = ItemMstr_DS.Tables(0)(0)
        Dim txtHeight As Integer = 26

        ' article numbers
        Dim grpArtNum As New GroupBox
        Dim txtARTC_NUM_DOM_C As New TextBox
        Dim txtARTC_NUM_CAN_C As New TextBox

        With txtARTC_NUM_DOM_C
            .Text = drItemMstr("ARTC_NUM_DOM_C").ToString
            .Size = New Size(200, txtHeight)
            .Location = New Point(0, 0)
        End With

        With txtARTC_NUM_CAN_C
            .Text = drItemMstr("ARTC_NUM_CAN_C").ToString
            .Size = New Size(200, txtHeight)
            .Location = New Point(0, txtHeight)
        End With

        With grpArtNum
            grpArtNum.Text = "Article Number"
            grpArtNum.Padding = New Padding(padding)
            grpArtNum.Controls.Add(txtARTC_NUM_DOM_C)
            grpArtNum.Controls.Add(txtARTC_NUM_CAN_C)
        End With

        Me.Controls.Add(grpArtNum)

    End Sub

End Class

What I am ending up with is ugly and not what I would expect to happen, notice how the group box text is cut off:

enter image description here

It seems the padding is not being applied properly, but I am sure I am just doing something wrong.

Upvotes: 1

Views: 1646

Answers (1)

LarsTech
LarsTech

Reputation: 81610

The Padding property is used primarily for Dock Styled child controls, so try adding a dock filled panel into your GroupBox to contain those TextBoxes:

With grpArtNum
  grpArtNum.Text = "Article Number"
  grpArtNum.Padding = New Padding(padding)
  Dim innerPanel As New Panel With {.Dock = DockStyle.Fill}
  innerPanel.Controls.Add(txtARTC_NUM_DOM_C)
  innerPanel.Controls.Add(txtARTC_NUM_CAN_C)
  grpArtNum.Controls.Add(innerPanel)
End With

Upvotes: 1

Related Questions