Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Setting width of ComboBox dropdown list

Posted on 1998-09-15
4
Medium Priority
?
433 Views
Last Modified: 2013-11-20
I have a problem when resizing the drop-down box in a combobox.  I calculate the length of the longest string that I am inserting with GetTextExtentPoint32(), then set the size with a CB_SETDROPPEDWIDTH message.  The list size does change, but it's always significantly wider than the longest string in the list.  To test, on the CBN_DROPDOWN notification, I check the width with CB_GETDROPPEDWIDTH, then iterate through each string in the list to check size.  They are the same, respectively, as when I set them; CB_GETDROPPEDWIDTH returns the same number as the longest string in the list, but the displayed list box is much wider than that string.

This is the code to set the width:
hDC = GetDC(m_handle);
GetTextExtentPoint32(hDC, pszText, strlen(pszText), &size);
ReleaseDC(m_handle, hDC);
SendMessage(m_handle, CB_SETDROPPEDWIDTH, (WPARAM) size.cx, 0L);
0
Comment
Question by:joeslow
[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
  • 2
4 Comments
 

Author Comment

by:joeslow
ID: 1322168
Adjusted points to 60
0
 
LVL 4

Accepted Solution

by:
piano_boxer earned 240 total points
ID: 1322169
The calculation of the width is wrong because the current selected font is wrong.

Just after the GetDC() call, set the font to the same as the parent:

SelectObject(hDC, GetFont(GetParent(m_handle)));

or

Just select a stock font:

SelectObject(hDC, GetStockObject(ANSI_VAR_FONT));
0
 
LVL 1

Expert Comment

by:Grailman
ID: 1322170
'size.cx' will probably not be accurate for very much text. A quick & dirty way to help is to subtract 1 unit per character when you set the box width:

SendMessage(m_handle, CB_SETDROPPEDWIDTH, (WPARAM) size.cx-strlen(pszText), 0L);

This seems to work pretty well if the string is not too long.

0
 
LVL 1

Expert Comment

by:Grailman
ID: 1322171
'size.cx' will probably not be accurate for very much text. A quick & dirty way to help is to subtract 1 unit per character when you set the box width:

SendMessage(m_handle, CB_SETDROPPEDWIDTH, (WPARAM) size.cx-strlen(pszText), 0L);

This seems to work pretty well if the string is not too long.

0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

664 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