CListBox scrolling
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: CListBox scrolling

  1. #1
    Join Date
    Oct 2000
    Posts
    11

    CListBox scrolling

    I have already posted a message on this, but people do not seem to understand what i mean.
    I have 5 list boxes (I need 5 listboxes not 1 ListCtrl) and i want to control their scolling with one (my own) scrollbar.
    I want the list boxes to scroll one down when the user clicks on the arrow .. can u see what i mean.
    i am trying to use SetTopIndex(int i) ... example
    all listoboxes show item 1 ... user clicks on down arrow ... I want listboxes to have item 2 at the top of the box now ..
    iuse Settopindex .. but item 2 is most normally visible when item 1 is ... so i need to FORCE settopndex to do its job
    whatever ..
    and i have tried to hide index 2 .. but it does not work
    any help
    thankyou all





  2. #2
    Join Date
    May 1999
    Location
    West Sussex, England
    Posts
    1,939

    Re: CListBox scrolling

    I chucked together a quick project to test what you want to do. I used a CFormView inherited class. This is th view class, that does, what I think you want. It doesn't deal with which item is hilighted (Use SetCurSel() for that). The proerties of the list boxe's were:

    No integral height - unchecked
    sort - unchecked
    vertical scroll - unchecked

    I also had my own vertical scroll bar on the form to control the scrolling.
    Here's the code

    // listboxView.cpp : implementation of the CListboxView class
    //

    #include "stdafx.h"
    #include "listbox.h"

    #include "listboxDoc.h"
    #include "listboxView.h"

    #ifdef _DEBUG
    #define new DEBUG_NEW
    #undef THIS_FILE
    static char THIS_FILE[] = __FILE__;
    #endif

    /////////////////////////////////////////////////////////////////////////////
    // CListboxView

    IMPLEMENT_DYNCREATE(CListboxView, CFormView)

    BEGIN_MESSAGE_MAP(CListboxView, CFormView)
    //{{AFX_MSG_MAP(CListboxView)
    ON_WM_VSCROLL()
    //}}AFX_MSG_MAP
    // Standard printing commands
    ON_COMMAND(ID_FILE_PRINT, CFormView::OnFilePrint)
    ON_COMMAND(ID_FILE_PRINT_DIRECT, CFormView::OnFilePrint)
    ON_COMMAND(ID_FILE_PRINT_PREVIEW, CFormView::OnFilePrintPreview)
    END_MESSAGE_MAP()

    /////////////////////////////////////////////////////////////////////////////
    // CListboxView construction/destruction

    CListboxView::CListboxView()
    : CFormView(CListboxView::IDD)
    {
    //{{AFX_DATA_INIT(CListboxView)
    // NOTE: the ClassWizard will add member initialization here
    //}}AFX_DATA_INIT
    // TODO: add construction code here

    }

    CListboxView::~CListboxView()
    {
    }

    void CListboxView:oDataExchange(CDataExchange* pDX)
    {
    CFormView:oDataExchange(pDX);
    //{{AFX_DATA_MAP(CListboxView)
    DDX_Control(pDX, IDC_SCROLLBAR1, m_Scroll);
    DDX_Control(pDX, IDC_LIST5, m_List5);
    DDX_Control(pDX, IDC_LIST4, m_List4);
    DDX_Control(pDX, IDC_LIST3, m_List3);
    DDX_Control(pDX, IDC_LIST2, m_List2);
    DDX_Control(pDX, IDC_LIST1, m_List1);
    //}}AFX_DATA_MAP
    }

    BOOL CListboxView::PreCreateWindow(CREATESTRUCT& cs)
    {
    // TODO: Modify the Window class or styles here by modifying
    // the CREATESTRUCT cs

    return CFormView::PreCreateWindow(cs);
    }

    void CListboxView::OnInitialUpdate()
    {
    CFormView::OnInitialUpdate();
    // popultate the controls
    CString text ;
    for (int i = 0 ; i < 20 ; i++)
    {
    text.Format("Entry %1d", i) ;
    m_List1.AddString(text) ;
    m_List2.AddString(text) ;
    m_List3.AddString(text) ;
    m_List4.AddString(text) ;
    m_List5.AddString(text) ;
    }
    SetScrollRange() ;
    ResizeParentToFit();

    }

    /////////////////////////////////////////////////////////////////////////////
    // CListboxView printing

    BOOL CListboxView::OnPreparePrinting(CPrintInfo* pInfo)
    {
    // default preparation
    return DoPreparePrinting(pInfo);
    }

    void CListboxView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
    {
    // TODO: add extra initialization before printing
    }

    void CListboxView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
    {
    // TODO: add cleanup after printing
    }

    void CListboxView::OnPrint(CDC* pDC, CPrintInfo* /*pInfo*/)
    {
    // TODO: add customized printing code here
    }

    /////////////////////////////////////////////////////////////////////////////
    // CListboxView diagnostics

    #ifdef _DEBUG
    void CListboxView::AssertValid() const
    {
    CFormView::AssertValid();
    }

    void CListboxView:ump(CDumpContext& dc) const
    {
    CFormView:ump(dc);
    }

    CListboxDoc* CListboxView::GetDocument() // non-debug version is inline
    {
    ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CListboxDoc)));
    return (CListboxDoc*)m_pDocument;
    }
    #endif //_DEBUG

    /////////////////////////////////////////////////////////////////////////////
    // CListboxView message handlers

    void CListboxView::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
    {
    CRect rect ;
    int count = m_List1.GetCount() ;
    int height = m_List1.GetItemHeight(0) ;
    int visible ;

    m_List1.GetWindowRect(&rect) ;
    visible = rect.Height() / height ; // number of items visible

    switch (nSBCode)
    {
    case SB_THUMBPOSITION :
    case SB_THUMBTRACK :
    break ;
    case SB_PAGEDOWN :
    break ;
    case SB_PAGEUP :
    break ;
    case SB_LINEDOWN :
    // scroll down a line in list boxes
    {
    int top = m_List1.GetTopIndex() ;
    if (top < count - visible)
    {
    top++ ;
    m_List1.SetTopIndex(top) ;
    m_List2.SetTopIndex(top) ;
    m_List3.SetTopIndex(top) ;
    m_List4.SetTopIndex(top) ;
    m_List5.SetTopIndex(top) ;
    m_Scroll.SetScrollPos(top) ;
    }
    }
    break ;
    case SB_LINEUP :
    // scroll up a line in list boses
    {
    int top = m_List1.GetTopIndex() ;
    top-- ;
    if (top < 0)
    top = 0 ;
    m_List1.SetTopIndex(top) ;
    m_List2.SetTopIndex(top) ;
    m_List3.SetTopIndex(top) ;
    m_List4.SetTopIndex(top) ;
    m_List5.SetTopIndex(top) ;
    m_Scroll.SetScrollPos(top) ;
    }
    break ;
    }
    }

    void CListboxView::SetScrollRange()
    {
    // set the scroll range for the scroll bar
    CRect rect ;
    int count = m_List1.GetCount() ;
    int height = m_List1.GetItemHeight(0) ;
    int visible ;

    m_List1.GetWindowRect(&rect) ;
    visible = rect.Height() / height ; // number of items visible
    if (visible >= count)
    {
    // all items visible, disable scroll bar
    m_Scroll.EnableWindow(false) ;
    }
    else
    {
    // allow scroll of items available
    m_Scroll.SetScrollRange(0, count - visible, TRUE) ;
    }

    }





    Roger Allen
    Roger.Allen@<a rel="nofollow" href="...ytical.com</a>
    Ok, Points make prizes, and ratings make points.
    Did I help?
    Please use meaningful question titles - "Help me" does not let me know whether I can help with your question, and I am unlikely to bother reading it.
    Please remember to rate useful answers. It lets us know when a question has been answered.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center