how to scroll editbox and listbox by code?

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?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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!
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.
rpatnaAuthor Commented:
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).
OWASP: Threats Fundamentals

Learn the top ten threats that are present in modern web-application development and how to protect your business from them.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rpatnaAuthor Commented:
this code working in one direction only. not in up direction by listbox scroll.
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.

rpatnaAuthor Commented:
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..
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.