how to scroll editbox and listbox by code?

Posted on 2003-11-04
Last Modified: 2013-11-20
I have an application with one listbox and one edit box. both having VScroll.

I want to write code for:
At run time : if user scroll any one, it scroll another one.

so how to do?
Question by:rpatna
  • 4
  • 3

Expert Comment

ID: 9677701
Scrolling the controls is quite easy - if you are using CEdit (I assume it would be multiline), the function LineScroll() will scroll it. Similarly, you can use CListBox SetTopIndex() to scroll the list box accordingly.

Quite how you respond to events to perform the scrolling is a little more complex.

You can use CListBox::GetTopIndex() to determine the first visible in the list box (I believe you can trap the WM_VSCROLL message for the list box to determine when it has been scrolled). However, it gets more interesting with the edit control because one of two things could happen - the user could scroll the edit control with the scroll bar, or they could make the edit control have the input focus and then move the caret around with the cursor keys. Again I believe you could trap WM_VSCROLL, but also the EN_SELCHANGE message from the edit control, and in both cases use CEdit::GetFirstVisibleLine() to determine the first line that is visible in the control.

Hope this gives you some pointers on how to solve the problem!

Expert Comment

ID: 9677703
Oops, forgot to add that the user could also possibly use the cursor keys in the list control, which you would also need to trap - maybe via the OnSelChange handler.

Author Comment

ID: 9678505
Thanks Martyn,

Bydefault CEdit having WM_VSCROLL message handler, and Selchange in CListBox.
so I written

// CListBox  mLineNo;
// CEdit      mEditStr;

void UserAlgDlg::OnVscrollEditMain()

void UserAlgDlg::OnSelchangeLineno()

now it is scrolling through edit box VScroll.

I have two more problem.
now plz tell me how to add WM_VSCROLL message handler in CListBox.
basically I don't know how to add windows message handler in Controls like CListBox, CEdit.
2. after writing above code , it not scrolling if I scroll by using thumb(middle part of scroll).
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.


Accepted Solution

martynjpearson earned 50 total points
ID: 9678618
First off, I think you might need to change your code in OnSelchangeLineno() to move to the top before you call LineScroll(), otherwise it will scroll from the relative to the current position, not the top.

So, it should be something like :

// Scroll to the top

// Scroll to the right point according to the list position

In order to get scroll notifications from the list, it is probably easiest to derive your own class from CListBox (use ClassWizard, then click Add Class, New...) and then add a handler for WM_VSCROLL. I've just done this and I get notifications when I scroll both by dragging the bar and by pressing the arrow buttons at the top and bottom.

When you get a WM_VSCROLL, you can send a custom message to the parent dialog, the handler for which can update your edit control.

If you wish to use your own messages, you can do the following

1) First off, define the message in a header file that both the list box and the dialog code can see, like this :

2) In the WM_VSCROLL handler in the list box class, add these lines :

if (GetParent())

3) In the dialog header file, add the following just before the line DECLARE_MESSAGE_MAP() :

afx_msg LRESULT OnListScrolled(WPARAM wParam, LPARAM lParam);

4) In the dialog source file, add the following just before the line END_MESSAGE_MAP() :

5) Add the folliowing handler to your dialog source file :

LRESULT UserAlgDlg::OnListScrolled(WPARAM wParam, LPARAM lParam)
   // Do your code here to update the edit control

   // And return 0
   return 0;

Hope this helps

Author Comment

ID: 9680139
this code working in one direction only. not in up direction by listbox scroll.

Expert Comment

ID: 9685119
Did you add the line that moved it to the top before scrolling the box? The problem with the code

void UserAlgDlg::OnSelchangeLineno()

is that when the selection changes, the edit control is only ever scrolled down, never up. For example, say this is called and the top index is 5 - the edit control will be scrolled down by five lines. Now if it's called again with the top index 2, it will be scrolled down 2, when we really want it to scroll up 3. Hence, the code to scroll the control back to the top, then back down again.


Author Comment

ID: 9693812
1. still in up direction by listbox scroll is not working.
2. in edit box , by scroll thumb ,it is not working, but by scroll arrow it is working in both direction

at least solve the 2nd problem plz..

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
repeatEnd java challenge 42 83
Define unique primary key 9 81
Detect CR LF to each line 12 137
Unix Command -- Challenging  question 7 79
In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
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.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at

707 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now