Solved

Setting width of ComboBox dropdown list

Posted on 1998-09-15
4
427 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 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Title # Comments Views Activity
Installing Microsoft R server on REDHAT or SUSE Linux 4 162
How to determine file rename from EventLog ? 2 62
sameEnds challenge 3 196
Is there a simple front-end menu system. 9 147
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
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.
Suggested Courses

751 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