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).
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.


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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
set up trigger (or something) to rename files (starting with ATL) in a linux directory 6 53
Tab names are off by one 5 54
ShiftLeft challenge 21 80
Not needed 13 109
Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
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.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

813 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

10 Experts available now in Live!

Get 1:1 Help Now