locked
Inner Margin/Padding of TextBox and ComboBox Controls RRS feed

  • Question

  • Hello all!

    I have researched a lot in that regard but there does not seem to be a solution. I am looking for a way to add padding to a TextBox and ComboBox control in WinForms - i.e. space between the text box of each control and the border.

    Like the below picture and like in HTML5 and CSS. However, the size is always reset to fit the text and it also seems that the text box is always at the top left.

    Any help would be much appreciated.

    Best regards,

    Christian


    Tuesday, April 28, 2020 5:45 AM

All replies

  • In case of comboboxes, try this approach: insert a Panel, set the border, set background colour to “Window”. Then insert the combobox inside this panel, set Flat Style to “Flat”. Adjust the sizes and locations. If this is required in many places, then define a User Control.

    In contrast with Forms, WPF has more formatting and animation capabilities and offers features that are similar to CSS.


    • Edited by Viorel_MVP Tuesday, April 28, 2020 9:25 AM
    Tuesday, April 28, 2020 9:24 AM
  • Hi ChrisP1980,

    Thank you for posting here.

    >>it also seems that the text box is always at the top left.

    You need to set ComboBox's 'DrawMode' property to 'OwnerDrawFixed' and 'DropDownStyle' property to 'DropDownList'.

    Here's an example:

        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            ComboBox1.Items.Add("1")
            ComboBox1.Items.Add("2")
            ComboBox1.DrawMode = DrawMode.OwnerDrawFixed
           ComboBox1.DropDownStyle = ComboBoxStyle.DropDownList
        End Sub
    
        Private Sub ComboBox1_DrawItem(sender As Object, e As DrawItemEventArgs) Handles ComboBox1.DrawItem
            Dim cbx As ComboBox = TryCast(sender, ComboBox)
            If cbx IsNot Nothing Then
                e.DrawBackground()
    
                If e.Index >= 0 Then
                    Dim sf As StringFormat = New StringFormat()
                    sf.LineAlignment = StringAlignment.Center
                    sf.Alignment = StringAlignment.Center
                    Dim brush As Brush = New SolidBrush(cbx.ForeColor)
                    If (e.State And DrawItemState.Selected) = DrawItemState.Selected Then brush = SystemBrushes.HighlightText
                    e.Graphics.DrawString(cbx.Items(e.Index).ToString(), cbx.Font, brush, e.Bounds, sf)
                End If
            End If
        End Sub

    Result:

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Tuesday, April 28, 2020 9:25 AM
  • Hello all!

    I have researched a lot in that regard but there does not seem to be a solution. I am looking for a way to add padding to a TextBox and ComboBox control in WinForms - i.e. space between the text box of each control and the border.

    You already asked that in your other thread and you got a working code showing how to move the textbox inside the combobox
    Tuesday, April 28, 2020 9:48 AM
  • Hello!

    It seems that we keep misunderstanding each other. All the code is referring to horizontal center and not to vertical. Please review my picture above. I need a possibility to add margin between the border and the text at all sides.

    It is not even possible to change the size of the combobox - it is always resized to fit the text. I need to make it higher and then center the text vertically.

    Best regards,

    Christian

    Tuesday, April 28, 2020 11:04 AM
  • Hi ChrisP1980,

    Thanks for your feedback.

    By changing the ComboBox.ItemHeight Property, we can change the height of an item in the combo box, and the selected item in ComboBox will be vertical alignment after using the above code.

    My test:

    Hope it could be helpful.

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Wednesday, April 29, 2020 10:15 AM
  • Hello!

    Looks good, thank you! However, I need a combo box where the text can be edited. Is that still possible then?

    Best regards,

    Christian

    Wednesday, April 29, 2020 7:30 PM
  • Hi ChrisP1980,

    Actually, set the DropDownStyle property to 'DropDownList' will make ComboBox non-editable.

    As an alternative, you can add a textbox and click button to change the text in combobox.

    For example:

        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            ComboBox1.Items(ComboBox1.SelectedIndex) = TextBox1.Text
        End Sub

    Result:

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, April 30, 2020 7:36 AM
  • Hi ChrisP1980,

    Did you solve your problem? If your question has been answered then please click the "Mark as Answer" Link at the bottom of the correct post(s), so that it will help other members to find the solution quickly if they face a similar issue.

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, May 7, 2020 5:42 AM