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
 
ListBox in VB6 has hWnd property, ListBox in VBA does not have this property. You can use FindWindow(Ex) to find handle, but in my opinion ListBox in VBA does not support messages
I suggest
Code:
Private Sub CommandButton1_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
    ListBox1.TopIndex = ListBox1.TopIndex + 1
End Sub

Private Sub CommandButton2_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
    ListBox1.TopIndex = ListBox1.TopIndex - 1
End Sub
 
Upvote 0
So it's just fun and not a real need? Because instead of clicking the CommandButton the user can click on the ListBox scrollbar, right? The real need is when, for example, instead of clicking the scrollbar the user wants to use the mouse wheel.
 
Upvote 0
I actually gave an example with command buttons, but it will actually be done through VBA via API with PeekMessage.
So, yes, for combo boxes, it's top index for frames, scrolltop, etc.
But I'm looking to integrate it for textboxs as well, and they don't have a scroll function.
I already have a working module managing scrolling for list boxes, combo boxes, frames, multipages, and UserForms with the wheel.WITHOUT HOOKING !!!
I just want to add textboxs management in my module
But since the textbox don't have top index or scrolltop, etc., I want to do it with sendmessageA,

and why not also replace those for the controls already managed to reduce my code
 
Upvote 0
Have you condidered using the CurLine Property ? It is a Hidden Read\Write Property whose value you can change for scrolling the textbox..

Alternatively, you could determinate the position and bounderies of the textbox scrollbar arrows (Up and Down Arrows) on the screen, then send the WM_LBUTTONDOWN\WM_LBUTTONUP messages to the TextBox Parent window (ie:=Its container) .The lParam argument should contain the packed mouse X,Y coordinates which will be within the target scrollbar arrow.
 
Upvote 0
hello
thancks for the answers but it's not that i want
i want realy work with native scrollbar of textbox
maybe with automationclient
 
Upvote 0
hello
And I tell you no

The sendkey and curline method move the lines down or up,
and scrolling is only effective if the curline is at the bottom or top of what's visible in the textbox.
 
Upvote 0
hello
And I tell you no

The sendkey and curline method move the lines down or up,
and scrolling is only effective if the curline is at the bottom or top of what's visible in the textbox.
So what's the exact effect you are after ? Are you trying to scroll the textbox scrollbar only without moving the text or current line?!!
I am not sure I understand ... Can you perhaps rephrase your question.
 
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