cristianbuse / VBA-UserForm-MouseScroll

Use the Mouse Scroll Wheel to scroll VBA UserForms and Controls
MIT License
70 stars 12 forks source link

Scroll textboxes 1 line per 1 lines #18

Closed Arnnaud closed 1 year ago

Arnnaud commented 1 year ago

Hi,

Could you explain how to modify the code so when one scrolls in textboxes, it moves the textbox 1 line per 1 line ?

Thank you very much!

cristianbuse commented 1 year ago

Hi @Arnnaud ,

The suggestion is the same as per our previous email exchange. However, thanks to you I've found a bug - the text scroll is scrolling one extra line when scrolling down. Thanks!

Simply replace this:

    'Lines to scroll up/down
    If scrollAmount.lines <> 0 Then
        deltaLines = -scrollAmount.lines
    Else
        deltaLines = -scrollAmount.pages * linesPerPage
    End If
    '
    'Jump to top/bottom line of the "page"
    Const topOffsetPt As Single = 3 'the extra 3 points at the top of a tbox
    If deltaLines > 0 Then
        tbox.CurY = PointsToHiMeter(topOffsetPt + linesPerPage * lineHeight)
    ElseIf deltaLines < 0 Then
        tbox.CurY = PointsToHiMeter(topOffsetPt)
    End If

with this:

    'Lines to scroll up/down
    If scrollAmount.lines <> 0 Then
        deltaLines = -scrollAmount.lines
    Else
        deltaLines = -scrollAmount.pages * linesPerPage
    End If
    deltaLines = Sgn(deltaLines)
    '
    'Jump to top/bottom line of the "page"
    Const topOffsetPt As Single = 3 'the extra 3 points at the top of a tbox
    If deltaLines > 0 Then
        tbox.CurY = PointsToHiMeter(topOffsetPt + (linesPerPage - 1) * lineHeight)
    ElseIf deltaLines < 0 Then
        tbox.CurY = PointsToHiMeter(topOffsetPt)
    End If

within the TBoxScrollY method of the MouseScroll module. This will fix the bug and also achieve your 1 line scroll.

Arnnaud commented 1 year ago

Hi @cristianbuse , Thank you for your answer! It does work with your fix, except if the textbox is too small... For example a textbox with 40 in height property, and 10 in font size, on can read 3 lines but I guess it actually does not show 3 complete lines... But the same textbox with same font size and 45 in height property, then it will work. It would be very useful it it were possible to fix it because scrolling is precisely useful with small textboxes... Thank you again for you tool and your answers! Best regards Arnaud

cristianbuse commented 1 year ago

Thanks @Arnnaud ,

I was able to replicate the issue. I will look into this in more detail next week. In short, there is an issue with the hiMetric calculation most likely having to do with rounding and the impact of having IntegralHeight on or off.

cristianbuse commented 1 year ago

Hi @Arnnaud ,

I've just pushed an update.

Could you please test again? You will still need to add deltaLines = Sgn(deltaLines) line yourself.

Thanks!

Arnnaud commented 1 year ago

Hi @cristianbuse I installed your update, and added deltaLines = Sgn(deltaLines) in the right place (just before 'Jump to top/bottom line of the "page"), but it still doesn't work : the problem is almost the same with "thin" textboxes (2 lines), you can now scroll 1 line, but once you scrolled one line, you can't scroll another anymore... And with big textboxes, with on scroll action, you scroll a few lines, and then it blocks too and you can't scroll the textbox anymore... So it is worse with the deltaLines = Sgn(deltaLines) added... :/ (

Thanks Best regards Arnaud

cristianbuse commented 1 year ago

Hi @Arnnaud ,

Thanks for checking!

It is quite disappointing it does not work as I've spent many hours to make it work on my computer. There are too many bugs and glitches and I'm convinced now that it's also dependant on monitor pixel size and other system settings. I'm prepared to give up on achieving scroll using the current approach (CurY and CurLine). I will have a think about this during the following days.

cristianbuse commented 1 year ago

Hi @Arnnaud ,

I've just pushed a new update. It still uses CurY and CurLine for logic but doesn't compute the line height in points anymore.

Could you please test again? Just uncomment the 'deltaLines = Sgn(deltaLines) line.

Thanks!

Arnnaud commented 1 year ago

Hi @cristianbuse,

I just tested, it looks fine !!! Thank you very, very much because it seems it wasn't easy at all and you did it very quickly understanding what I was looking for... I am very impressed, and I am really happy to use such a great tool!

Thank you @cristianbuse !

cristianbuse commented 1 year ago

Thank you @Arnnaud for the kind words and for your feedback and help in improving this repo!

cristianbuse commented 11 months ago

Starting 76a384fb50d5bb2a5f6de49e287fe2fb774472d6 both Modal and Modeless forms are supported while debugging also works.