Solved

how to scroll editbox and listbox by code?

Posted on 2003-11-04
7
654 Views
Last Modified: 2013-11-20
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?
0
Comment
Question by:rpatna
  • 4
  • 3
7 Comments
 
LVL 8

Expert Comment

by:martynjpearson
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!
Martyn
0
 
LVL 8

Expert Comment

by:martynjpearson
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.
Martyn
0
 

Author Comment

by:rpatna
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()
{
      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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 8

Accepted Solution

by:
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
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
 

Author Comment

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

Expert Comment

by:martynjpearson
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()
{
     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
 

Author Comment

by:rpatna
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..
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
cat dog challenge 18 124
mixString challenge 36 113
Whole sheet autoscrub still needed 19 48
Not needed 13 95
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

863 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

23 Experts available now in Live!

Get 1:1 Help Now