Thread: Resizing Combo Box When Adding items to it

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    206

    Question Resizing Combo Box When Adding items to it

    Hi there,

    I'll cut to the chase...I've made a combo box in the little Help->About Menu dialog box that appears as standard on the menu bar.

    Here's the code:

    Code:
    INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
    {    
        const wchar_t* colour[4]
            = { L"Blue", L"Red", L"Orange", L"Yellow" };
    
        WPARAM nI;
    
        for (int i = 0; i < 4; i++)
        {
            nI = SendMessageW(GetDlgItem(hDlg, 1003), (UINT) CB_ADDSTRING, 0, (LPARAM)colour[i]);
        }
    
    ....etc (rest of message handling function)
    
        switch (message)
        {
        case WM_INITDIALOG:        
    
    ..... and so on
    This is the bit we're interested in:

    Code:
    const wchar_t* colour[4]
            = { L"Blue", L"Red", L"Orange", L"Yellow" };
    
        WPARAM nI;
    
        for (int i = 0; i < 4; i++)
        {
            nI = SendMessageW(GetDlgItem(hDlg, 1003), (UINT) CB_ADDSTRING, 0, (LPARAM)colour[i]);
        }
    All works. Actually looks quite nice too:

    Resizing Combo Box When Adding items to it-combo-box-jpg

    Only trouble is for a long while the bottom 3 strings were not showing. Only the first entry 'Blue' was shown. To fix this I had to MANUALLY change the height setting in the resource file where the Combo Box layout and flags are defined.

    Sometimes if working with DirectX for example when you're querying an adapter list and the resultant display modes/resolutions etc... there's no way to know how long the list of entries you need for the Combo Box is going to be. Thus surely one needs a way to dynamically resize the box when adding new entries.

    I don't know what this is though. I could try messing around in the settings in Visual Studio again but I thought it better to ask here first.

    Thanks

    Edit: Should also add that the 1003 in the GetDlgItem(hDlg, 1003) call equates to the identifier for the combo box.
    Last edited by shrink_tubing; 10-01-2023 at 10:30 PM.

  2. #2
    Registered User
    Join Date
    Jan 2010
    Posts
    206
    Update: I should add I rather foolishly put those lines of code to run every time a message is sent to the handling function. I've since updated it so they only run when WM_INITDIALOG message is received by the message handling function. Before I was getting ridiculous amounts of repeat entries because of this.

    The problem still persists however although I can now see only 4 entries which I have to use a vertical scrollbar to access. Still not functioning like a regular drop down combo box though.

  3. #3
    'Allo, 'Allo, Allo
    Join Date
    Apr 2008
    Posts
    639
    I can't remember if this is listboxes or comboboxes (or maybe both) but the height you set for the control in the resource file/createwindow includes the drop down height and thus, indirectly sets the number of items it can show. If you want more items in the list before having to scroll, just make it taller! There's no reason not to set it to a size that shows the max number you want to display though I'm not sure anybody wants to see the 100 resolution options popup in front of them all at once - especially since at certain resolutions/scaling that might not fully fit on the screen.
    10-15 is probably enough but that's a preference rather than any sort of rule. Combo/listboxes work and look the same pretty much everywhere so unless you're targeting people who don't usually use a phone, tablet or computer, the scroll bar isn't going to befuddle anybody.

    It you want UI to dynamically size for whatever reason (like you let people choose a bigger font for your UI text or you're handling DPI scaling changes), that's up to you to implement. Pretty sure none of the user controls size automatically to their contents or fonts.
    Full (ie manual) control of size and appearance is pretty much the only reason to write UI code in raw Win32 any more. Common value-add niceities like dynamic resizing are the purview of UI frameworks like QT and others that do the grunt work for you.

  4. #4
    Registered User
    Join Date
    Jan 2010
    Posts
    206
    Hi there,

    Adey that's basically what it was. I figured it out in the end. I just adjusted to height to a reasonable value and added a scroll bar. I'm not going to do any dynamic resizing tbh I feel it would be unnecessary.

    Much thanks for your reply.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Adding items to a Dialog Box
    By shrink_tubing in forum C++ Programming
    Replies: 2
    Last Post: 09-21-2022, 08:17 AM
  2. adding font to a combo box
    By supernater in forum Windows Programming
    Replies: 1
    Last Post: 03-13-2009, 06:11 AM
  3. adding font to a list box or combo box
    By supernater in forum Windows Programming
    Replies: 2
    Last Post: 03-13-2009, 05:53 AM
  4. Create new combo boxes based on items selected in preview combo box
    By RealityFusion in forum Windows Programming
    Replies: 2
    Last Post: 01-10-2007, 09:50 AM
  5. Adding Items to Combo Boxes
    By tenthirty in forum Windows Programming
    Replies: 10
    Last Post: 12-21-2001, 02:37 AM

Tags for this Thread