Solved

Setting width of ComboBox dropdown list

Posted on 1998-09-15
4
422 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
  • 2
4 Comments
 

Author Comment

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

Accepted Solution

by:
piano_boxer earned 60 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

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

Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
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.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

863 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

24 Experts available now in Live!

Get 1:1 Help Now