Siva
Siva

Reputation: 1411

Horizontal scroll not coming for listbox?

I am having a listbox where I set both the properties i.e, vertical and horizontal scroll to true.I am able to get vertical scroll bar but not able to get horizontal scroll bar when added a lengthy string.

Can anyone please let me know how get horizontal scroll bar for a listbox.

Upvotes: 4

Views: 6540

Answers (3)

ScottMcP-MVP
ScottMcP-MVP

Reputation: 10415

You have to specify the horizontal scroll extent (max width in pixels). Do that by calling CListBox::SetHorizontalExtent.

Upvotes: 3

Webmasterjeff
Webmasterjeff

Reputation: 71

In MFC, I had a listbox that was too big and extended past the right border of the window containing it. Once I made the listbox x dimension fit inside the window, the scrollbar started working properly again.

For some reason if the listbox is too big, Windows did not handle the scrollbar visibility properly as a side-effect of the sizing.

Upvotes: 0

Siva
Siva

Reputation: 1411

Adding this piece of code in OnInitDialog resolved my issue.

BOOL OnInitDialog()
{
  CString str;
    CSize sz;
    int dx = 0;
    CDC* pDC = m_listbox.GetDC();
    for(int i=0; i < m_listbox.GetCount();i++)
    {
        m_listbox.GetText(i,str);
        sz = pDC->GetTextExtent(str);

        if(sz.cx > dx)
            dx = sz.cx;

    }
    m_listbox.ReleaseDC(pDC);

    if(m_listbox.GetHorizontalExtent() < dx )
    {
        m_listbox.SetHorizontalExtent(dx);
        ASSERT(m_listbox.GetHorizontalExtent() == dx);

    }

  return TRUE;
}

Upvotes: 7

Related Questions