how to scroll editbox and listbox by code?

Hi,
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?
rpatnaAsked:
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.

martynjpearsonCommented:
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!
Martyn
0
martynjpearsonCommented:
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.
Martyn
0
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()
{
      mLineNo.SetTopIndex(mEditstr.GetFirstVisibleLine());
}

void UserAlgDlg::OnSelchangeLineno()
{
      mEditstr.LineScroll(mLineNo.GetTopIndex());
}

now it is scrolling through edit box VScroll.

I have two more problem.
1.
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).
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

martynjpearsonCommented:
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
mEditstr.LineScroll(-mEditstr.GetFirstVisibleLine());

// Scroll to the right point according to the list position
mEditstr.LineScroll(mLineNo.GetTopIndex());

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 :
#define WM_LIST_SCROLLED WM_USER + 1

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

if (GetParent())
   GetParent()->PostMessage(WM_LIST_SCROLLED);

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() :
ON_MESSAGE(WM_LIST_SCROLLED, OnListScrolled)

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
Martyn
0

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.
0
martynjpearsonCommented:
Did you add the line that moved it to the top before scrolling the box? The problem with the code

void UserAlgDlg::OnSelchangeLineno()
{
     mEditstr.LineScroll(mLineNo.GetTopIndex());
}

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.

Martyn
0
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..
0
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.