trying to scroll listbox with two commandbutton

patricktoulon1

Board Regular
Joined
Jan 23, 2025
Messages
76
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
 
re
i see your proposition
you said for changing hwnd multipage to multipge.pages(x) with unknow child
VBA Code:
Private Sub MultiPage1_MouseMove(ByVal Index As Long, ByVal Button As Integer, ByVal Shift As Integer, ByVal x As Single, ByVal y As Single)
 SetMouseWheel MultiPage1.Pages(MultiPage1.Value)
End Sub
its not working like that you want
unknow+child give you always a handle of userform client area
im my proposition i use windowfrompoint its very sure
Thank you for sharing your code above. I finding it really helpful going over this all with you both because it helps clarify things in my own mind.

My code shouldn't be taking the child hwnd of the Page. The GW_CHILD branch of the code should only be engaged when a UserForm is being passed to it. My very sloppy shorthand way of checking whether an object is a userform is to check it's TypeName against it's name - they are identical.
VBA Code:
    If TypeName(RHS) = RHS.Name Then
Granted, it is inexact, and I see that there might be some problems with this approach. I'm just to lazy to do anything else at the moment!
I will reread your points about teh Textbox / focus and give it some thought.
 
Upvote 0
Sorry, something just occurred to me as I started to download your file that you very kindly uploaded.

So you know that there are other (potentially very useful) events available to us through the InkCollector. The two that spring immediately to mind are: CursorOutOfRange and CursorInRange
Have I menioned them anywhere before or in my code? I'm wondering if they might be able to help with your reFocus issue. Could you not conceivably use the CursorOutOfrange event to SetFocus back on the Textbox?
 
Upvote 0
re
yes i 'll see on MS page this property but i did'nt found a elements for use
and the problem is that even if I do a "textbox1.setfocus" it doesn't do it
and I don't know why because when I enter the textbox again the previous IC is destroyed and therefore the handle is that of the client area of the userform when we are on the textbox
it gives the impression that setfocus handle and actualcontrol.setfocus contradict each other

i'me forced to click on textbox for take focus

we will find the solution, we are not far from it

thanks for return
 
Upvote 0
ok i'me tested the event IC_CursorOutOfRange
it's work only with listbox and frame
for the multipage it's work only if i take handle multipage but with area multipage handle it's dont work

it's not a prolème with my code because my rectangle function work perfectely

if i use inkcollector with handle normaly this events work but the scroll effect dont work very well in 64
 
Upvote 0
Hi - just had a look. SetFocus on the TextBox is not working because VBA does not recognise the control as TextBox is the first place. The VBA Interpreter doen't even get to the point of executing SetFocus method. Instead if specify MSForms.TextBox, that appears to work.

And I should add that I didn't need to click back into the TextBox in order for it to have focus. I hovered over the other controls and they worked, and then I hovered back over the TextBox control and that worked - I didn't click anything.
 
Upvote 0
hello Dan_W
i dont understood
where do I need to specify that it is a MsForms.TextBox
 
Upvote 0
re
if i give the focus to control (After given the focus to handle of control in setupMouseWheel)
when i'me hover on and i'me return on textbox that work on all control except the multipage

in fact I realize that if no control has the focus it does not work to give the focus back to the testbox another must have had it before it
except that by giving the focus it adds a problem to the multipage which does not have the scroll because it is the page which must have the focus
but page have'nt property focus
 
Upvote 0
I think I've found the definitive solution.
it's work with :
  1. W7 office 2007
  2. W10 office 2007
  3. W10 office2013 32
  4. W10 office 2021 64
  5. W11 office 2021 64

The problem was that I needed to correlate the window handle with the focus control.
I added the progress bar to the management.
Now, when you move from one control to another, the new hovered control takes focus (control AND !!! Valid handle ).
I'm cleaning everything up and providing you with a file.

It works with:
*Textbox
*ListBox
*ComboBox
*Frame
*Multipage
*ProgressBar
 
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