trying to scroll listbox with two commandbutton

patricktoulon1

New Member
Joined
Jan 23, 2025
Messages
38
Office Version
  1. 2013
Platform
  1. Windows
hello i'me trying to scroll a listbox with a commandbutton but that dont work
VBA Code:
Option Explicit

Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
                              (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, _
                              lParam As Any) As Long

Private Const WM_VSCROLL = &H115
Const SB_LINEDOWN = 1
Const SB_LINEUP = 0

'event mouseUp for then button  because i need  a listbox focused

Private Sub CommandButton1_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
  ListBox1.SetFocus
   SendMessage ListBox1.[_GethWnd], WM_VSCROLL, SB_LINEUP, ByVal 0&

End Sub

Private Sub CommandButton2_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
  ListBox1.SetFocus
   SendMessage ListBox1.[_GethWnd], WM_VSCROLL, SB_LINEDOWN, ByVal 0&

End Sub

Private Sub UserForm_Activate()
ListBox1.List = Evaluate("row(1:30)")
End Sub
can you help me please
 
hello @Jaafar Tribak
ok thancks but i 've a lot of red line in code
can you post an example file in deposefile
how can 'i give you two files
thancks for your interesting
It is there already in the workbook example link I posted.
Check out this one:
ScrollTextBox_V1.xlsm

The red lines in code are most likely due to compilation differences between x32bit and x64bit ... That's nothing to worry about. Just ignore that. The code should hopefully work fine in either bit versions
 
Upvote 1
re
Woawouh!! it's magic
and what's more, it doesn't change the curline if it's beautiful
Now I need to understand how it works.
great!!!
I have to say, the structure of the code is not particularly easy to follow. While I’ve added as many comments as possible, it still may present some difficulties in understanding.

In short, the code should be understood as follows:

The CWheelScroll Class is instanciated once , regardless of how many textboxes the user wants to hook. I thought this would make the class much easier to use, lesser code and more intuitive for the user (no collections needed, no various instances, no need to worry about releasing on close etc ...)

Once the Class is intanciated, the user passes two identically sized arrays to the instance: One array will hold the TextBoxes and the other one will hold their respective ScrollLines per mousewheel notch. This is done via the EnableMouseWheelScroll Method.

Once the unique class instance recieves the data held in the 2 arrays , It creates one new class for each textbox. Notice that the creation of these instances take place inside the initial instance (not inside the class client ie; not inside the userform module)
VBA Code:
    vTextBoxes = Array(TextBox1, TextBox2, TextBox3, TextBox4)
    vScrollLines = Array(1, 10, 4, 6)
 
    ' Initiate class instance and enable wheel-scrolling.
    With MouseWheel
       .Init Form:=Me
       .EnableMouseWheelScroll TextBoxesArray:=vTextBoxes, ScrollLinesArray:=vScrollLines
    End With

Since we are using Event\RaiseEvent\WithEvents in order to communicate between the class and the userform in a OOP manner via a generic callback event procedure shared between all the textboxes, I decided to temporarly cache the ObjPtr of the textboxes and their respective scrolllines value in the userform window Property list (That's why you see lots of SetProp\GetProp\RemoveProp being used throughout. The temporary caching is needed so that each class instance is properly released\terminated when done.

As for the IPrivateMembers interface, it is there just to hide the Properties of the CwheelScroll Class that the client userform should not see.

Also, you may wonder why I hooked the Application.CommandBars! The reason is, the CwheelScroll Class cannot tell when the userform is being closed as the QueryClose and Terminate events are not exposed when hooking the userform via WithEvents in a class module. I need to make sure that when disabling the mosewheel scrolling as well as when closing the userform, all the class instances are properly terminated and their memory released. The Application CommandBars OnUpdate event comes in handy and can fire when the userform is closed.

That's the basic idea.
 
Upvote 0
yes i 'me understood for the class instances
it' a motor to scroll i'me search to understand

If I'm not mistaken, you use the cursor and you send the message and you put the cursor back in its place instantly with each spin of the wheel
and the fact that it's in a class frees up a little resource for possible code outside of scrolling and since it's subclassing (child instance) the parent class doesn't block too many resource
 
Upvote 0
yes i 'me understood for the class instances
it' a motor to scroll i'me search to understand

If I'm not mistaken, you use the cursor and you send the message and you put the cursor back in its place instantly with each spin of the wheel
and the fact that it's in a class frees up a little resource for possible code outside of scrolling and since it's subclassing (child instance) the parent class doesn't block too many resource
Yes . That's the SendInput\GetCursorPos\SetCursorPos\ShowCursor trick I suggested in earlier posts.

You mentioned *subclassing*. Is that as in Windows subclassing? If that's what you mean then the answer is no ... The code doesn't subclass any windows. So no unexpected crashings are to be feared.

The way the code is designed is that it starts a Do\Loop only when the mouse pointer enters the hooked textbox. The loop is exited as soon as the mouse cursor leaves the hooked textbox. This should lessen the impact on resources.
 
Upvote 0

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top