How to find the size of the drop down arrow button on a combobox in VB.NET

Posted on 2008-06-13
Last Modified: 2013-11-26
I'm trying to find out the width of the drop down arrow button on a combobox within VB.NET. I would like to be able to find out what the width is with visual styles turned on or off.  Any help would be great thanks.
Question by:microdrainage
  • 3
  • 3
LVL 96

Expert Comment

by:Bob Learned
ID: 21785125
I wouldn't know of way to get that from the operating system, but I am curious why you need that information?  There might be another way to accomplish what you need without that tid bit.

Author Comment

ID: 21793071

I'm trying to use the error provider to place an icon actually inside the textbox area of the the combobox control (from the righthand side), using SetIconPadding and using a negative number to offset the icon. Hence I need to know the width of the drop down button in order to offset it by that amount.  I've tried using an owner draw combobox but this only seems to work if the dropdownstyle property is set to DropDown, the other two setting owner draw does not work!

I've also tried using the attached code but this appears to return the size of the icon within the button itself (on my machine 15x16), you would either have to find the size of the button of the size of the border (which makes the button graphic) and add this to the size of the icon! In the code below when creating a graphics object the me here represent the combobox control.

        If Not Application.RenderWithVisualStyles Then

            Return Nothing

        End If

        Dim element As VisualStyleElement = VisualStyleElement.ComboBox.DropDownButton.Normal

        Dim vsr As VisualStyleRenderer = New VisualStyleRenderer(element)

        Dim g As System.Drawing.Graphics = Me.CreateGraphics

        Return vsr.GetPartSize(g, ThemeSizeType.True)

Open in new window

LVL 96

Expert Comment

by:Bob Learned
ID: 21793361
You might want to use the edit region to determine that location.

Imports System

Imports System.Windows.Forms

Public Class ComboBoxEditRegion

    Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" _

     (ByVal hWnd1 As IntPtr, ByVal hWnd2 As IntPtr, ByVal lpsz1 As String, ByVal lpsz2 As String) As IntPtr

    Public Shared Function GetEditHandle(ByVal comboCurrent As ComboBox) As IntPtr

        ' Get the handle of the inner Edit control.

        Dim editHandle As IntPtr = FindWindowEx(comboCurrent.Handle, IntPtr.Zero, vbNullString, vbNullString)

        Return editHandle

    End Function

End Class

Open in new window

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.


Author Comment

ID: 21800746
Thanks for the info. I was trying not to use the API and simply use the .NET framework but that would probably do it! I've attached a variation of the code from my previous comment. Using the ScrollBar.ArrowButton.DownNormal element appears to give the correct size for the combobox button but I'm not 100% sure if that is the actual visual element that gets drawn for a combobox. (It looks the same!) Your code has the advantage that it'll (should:-)) work even if Visual styles are turned off.

        Dim vsr As VisualStyleRenderer = New VisualStyleRenderer(VisualStyleElement.ScrollBar.ArrowButton.DownNormal)

        Dim g As System.Drawing.Graphics = Me.CreateGraphics

        Dim arrowButtonSize As Size = vsr.GetPartSize(g, ThemeSizeType.True)

        vsr = New VisualStyleRenderer(VisualStyleElement.ComboBox.DropDownButton.Normal)

        Dim dropDownButtonSize As Size = vsr.GetPartSize(g, ThemeSizeType.True)

        If dropDownButtonSize.Width <= arrowButtonSize.Width Then

            Return arrowButtonSize


            Return dropDownButtonSize

        End If

Open in new window

LVL 96

Accepted Solution

Bob Learned earned 250 total points
ID: 21801964
There are many ways to solve a problem, and I like your approach.  I believe that you have the right element.

Author Closing Comment

ID: 31466900
Thanks for your help.

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Visual Studio .Net 2012 and Crystal Reports 9 48
Convert an ASPX page into PDF 7 25
Advice in Xamarin 21 56
Exit the loop 4 35
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Internet Business Fax to Email Made Easy - With  eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now