Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8931683
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T09:09:56+00:00 2026-06-15T09:09:56+00:00

Listboxes do not auto-resize. The best we’ve got is: SendMessage(my_listbox, LB_SETHORIZONTALEXTENT, 1000, 0); MS

  • 0

Listboxes do not auto-resize. The best we’ve got is:

SendMessage(my_listbox, LB_SETHORIZONTALEXTENT, 1000, 0);

MS helpfully notes that “…a list box does not update its horizontal extent dynamically.”

(why the heck not…but I digress)

How can the width be set dynamically to avoid truncating the text of a message longer than 1000px?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T09:09:57+00:00Added an answer on June 15, 2026 at 9:09 am

    if I understand the question… 🙂

    You’ll essentially need to measure all of the items in the list box and calculate the maximum width of the list box contents, and then adjust the width of the listbox.

    Here’s some code from this project (that adds a automatic horizontal scrollbar to listboxes). This snippet is called when the font changes but it demonstrates (roughly) what’s needed:

    static void OnSetFont( HWND hwnd, HFONT hFont )
    //
    //  Font has changed!
    //  We need to measure all of our items and reset the horizontal extent of the listbox
    {
        CData *pData = reinterpret_cast< CData * >( ::GetProp( hwnd, g_pcszDataProperty ) );
    
        pData->m_hFont = hFont;
    
        //
        //  Set up a HDC...
        HDC hdc = GetDC( hwnd );
        HGDIOBJ hOld = SelectObject( hdc, pData->m_hFont );
    
    
        //
        //  Record the average width for use as our 'fudge factor' later.
        TEXTMETRIC tm;
        GetTextMetrics( hdc, &tm );
        pData->m_nAvergeCharWidth = tm.tmAveCharWidth;
    
    
        pData->m_nMaxWidth = 0;
    
        //
        //  This is used as our item buffer. Saves us from having to handle the reallocation
        //  for different string lengths
        CArray< TCHAR, TCHAR > arrBuffer;
    
        //
        //  Quick reference to make the code below read better
        CArray< int, int > &arrWidth = pData->m_arrItemWidth;
    
        //
        //  The main loop. Iterate over the items, get their text from the listbox and measure
        //  it using our friendly little helper function.
        const UINT uCount = arrWidth.GetSize();
        for( UINT u = 0; u < uCount; u++ )
        {
            const int nLength = ::SendMessage( hwnd, LB_GETTEXTLEN, u, 0 );
            arrBuffer.SetSize( nLength + 1 );
            ::SendMessage( hwnd, LB_GETTEXT, u, (WPARAM)arrBuffer.GetData() );
    
    
            const int nItemWidth = BaseMeasureItem( pData, hwnd, hdc, arrBuffer.GetData() );
    
            pData->m_arrItemWidth.SetAt( u, nItemWidth );
            if( nItemWidth > pData->m_nMaxWidth )
            {
                pData->m_nMaxWidth = nItemWidth;
            }
        }
    
    
        //
        //  Now, either set the horizontal extent or not, depending on whether we think we need it.
        if( pData->m_nMaxWidth > pData->m_nClientWidth )
        {
            ::SendMessage( hwnd, LB_SETHORIZONTALEXTENT, pData->m_nMaxWidth + pData->m_nAvergeCharWidth, 0 );
        }
        else
        {
            ::SendMessage( hwnd, LB_SETHORIZONTALEXTENT, 0, 0 );
        }
    
        //
        //  The usual release of resources.
        SelectObject( hdc, hOld );
        ReleaseDC( hwnd, hdc );
    }
    

    and…

    static int BaseMeasureItem( CData *pData, HWND hwnd, HDC hdc, LPCTSTR pcszText )
    //
    //  Measure and item and adjust the horizontal extent accordingly.
    //  Because the HDC is already set up we can just do it.
    //  We return the width of the string so our caller can use it.
    {
        SIZE size;
        ::GetTextExtentPoint32( hdc, pcszText, _tcslen( pcszText ), &size ); 
    
        if( size.cx > pData->m_nMaxWidth )
        {
    
            pData->m_nMaxWidth = size.cx;
    
            if( pData->m_nMaxWidth > pData->m_nClientWidth )
            {
                ::SendMessage( hwnd, LB_SETHORIZONTALEXTENT, pData->m_nMaxWidth + pData->m_nAvergeCharWidth, 0 );
            }
    
        }
    
        return size.cx;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently implementing the application that displays hierarchy using ListBoxes (please do not
I have two ListBoxes. First ListBox items are list of Products. and second ListBox
I'm trying to create a custom UserControl that will mimic auto-complete as it works
I am trying replicate auto-complete functionality that you find on the Google homepage. So,
So here is the scenario, I've got an operation that clears an ObservableCollection, runs
I have two listboxes. The listbox1 contains a list of the DB names. Listbox2,
I'm looking for a reporting/printing solution that does not involve RDLC/SSRS. I'd like to
I have a Form with two ListBoxes on them. I have removeFromBoxWaitin g that
I am trying to execute this code for a listbox but its not working,
I want set Listbox background to transparent but not working Is there any idea?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.