Solved

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

Posted on 2008-06-13
6
1,026 Views
Last Modified: 2013-11-26
Hi
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.
0
Comment
Question by:microdrainage
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
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.
0
 

Author Comment

by:microdrainage
ID: 21793071
Hi

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.

Thanks
Ian
        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

0
 
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

0
Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

 

Author Comment

by:microdrainage
ID: 21800746
Hi
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.
Cheers

Ian
        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
        Else
            Return dropDownButtonSize
        End If

Open in new window

0
 
LVL 96

Accepted Solution

by:
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.
0
 

Author Closing Comment

by:microdrainage
ID: 31466900
Thanks for your help.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

733 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