0

Can the TextBox portion of a ComboBox be made resizeable on all sides?

I've done this with the TextBox (shown below). I thought the ComboBox was a composite control with a TextBox & ListBox, so I assumed it could be extended to reveal the AutoSize property. But after checking the Reference Source for the ComboBox, it seems I was incorrect. It seems to be scratch-built control, and I am uncertain how to do it.

Also, now that I've pasted that link to the source, I realized I don't know what version of .Net that is. I can't find any indication of the version on that page.

This project is VB .Net Framework 4.7.2, but I will gladly accept any comments & answers for both C# and VB, possibly even other versions of .Net if I can make it work.

Imports System.ComponentModel
Public Class TextBoxSizeable
    Inherits TextBox
    <Browsable(True)>
    <EditorBrowsable(EditorBrowsableState.Always)>
    <DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)>
    Public Overrides Property AutoSize() As Boolean
        Get
            Return MyBase.AutoSize
        End Get
        Set(ByVal value As Boolean)
            MyBase.AutoSize = value
        End Set
    End Property
End Class

EDIT:

This reason for this is cosmetic; so the ComboBox matches the height of other controls on the form, such as Buttons and TextBoxes.

4
  • 1
    Found it is Net 8 : github.com/KirillOsenkov/SourceBrowser/pulse See following : learn.microsoft.com/en-us/dotnet/api/… Commented Sep 13 at 1:30
  • 3
    The .NET ComboBox is a composite control but not at the .NET level, i.e. it is not a .NET control that contains other .NET controls. It is a .NET control that contains multiple Win32 controls. There is a low-level edit control in there, not a .NET TextBox control. Commented Sep 13 at 3:45
  • 1
    Theoretically, you can change the size of a Win32 ComboBox (as mentioned, this is a Win32 composite Control, which includes the external container, an Edit Control, a Button Control and a ListControl), but you'd have to rewrite its Designer, its LayoutEngine and a bunch of other stuff, given that a stndard ComboBox redefines its bounds each time you interact with it -- If you describe the reasons that led to this question, maybe we could suggest an alternative approach Commented Sep 13 at 11:15
  • Thank you all. I've been using .IntegralHeight on various list controls for longer than I can remember - possibly back to VB6 & VBA. I don't think I will go as far as rewriting the Win32 designer. My reasons for this OP re completely cosmetic, and my obsessive design habit of ensuring all controls are perfectly aligned. In this case the ComboBox, should be precisely the same height as the button to the right, which is exactly the same height as all other buttons. And all controls are aligned to a grid of an odd value (9), so when halving if needed it falls on a full pixel instead of between. Commented Sep 18 at 16:56

2 Answers 2

2

I thought the ComboBox was a composite control with a TextBox & ListBox

It is, if you set the DropDownStyle property to Simple. If you do so, you can freely resize also the vertical size of the ComboBox:

enter image description here

If you want to fill the specified size completely, you might want to set IntegralHeight to false as well, allowing partially shown items in the ListBox area.

Sign up to request clarification or add additional context in comments.

2 Comments

At first this seemed like a perfect & simple solution. But after testing I found it doesn't change the size of the textbox portion of the combobox. That is my goal.
The height of the textbox part auto-adjusts itself to the currently selected Font. This is how a regular single-line TextBox works as well. "This reason for this is cosmetic; so the ComboBox matches the height of other controls on the form" - I know where you're coming from, I tend to do the same. I recommend resizing the buttons and other controls then. Maybe even at run-time if you are a perfectionist, as fixed-height native Win32 controls may look slightly differently with visual styles disabled or with high DPI settings, etc.
0

If you enable all the four anchors of the comboBox then your comboBox will resize according to its parent container. The parent control can be a TableContainer, GroupBox or maybe just the Form window.

Given below code will make your comboBox to change its width with respect to its parent container.

ComboBox1.Dock = DockStyle.None
ComboBox1.Anchor = AnchorStyles.Top Or AnchorStyles.Bottom Or AnchorStyles.Left Or AnchorStyles.Right

However, the comboBox is not meant to resize vertically based on its parent container height, you have to manually increase or decrease the height of the combo box.

Another way is to use DockStyle.Fill, this will make the combo box to take the entire space available. Suppose you are using TableContainer then your combo box will resize automatically according to the space available in the table cell. But again here the combo box will not resize vertically.

2 Comments

[...] you have to manually increase or decrease the height of the combo box?
I tried this but it did not work. I placed the ComboBox inside a Panel Container, and set the Dock to All and all four Achors to True. But the result was the same; it doesn't change the size of the textbox portion of the combobox. That is my goal.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.