mouse Wheel event to increase the height

ahmedAlie 161 Reputation points
2022-01-10T17:00:34.943+00:00

HI
Use the mouse Wheel event to increase the height of the PictureBox or vice versa.
The problem is that I want to zoom in at a maximum h =100 and reduce at a minimum h =30 , not to infinity
my try

''MouseWheel Event
Dim factor As Double = 1.0
Dim mwe As HandledMouseEventArgs = DirectCast(e, HandledMouseEventArgs)

        If PIC_MALE.Height <= 30 Or PIC_MALE.Height >= 100 Then
            mwe.Handled = True
        End If

        If e.Delta > 0 Then

            factor += (increment / 100.0)
            PIC_MALE.Height += CInt(PIC_MALE.Height * e.Delta / 1000)
            PIC_MALE.Refresh()
        Else

            factor -= (increment / 100.0)
            PIC_MALE.Height += CInt(PIC_MALE.Height * e.Delta / 1000)
            PIC_MALE.Refresh()
        End If
Developer technologies | .NET | Other
Developer technologies | VB
0 comments No comments
{count} votes

Accepted answer
  1. Jiachen Li-MSFT 34,221 Reputation points Microsoft External Staff
    2022-01-11T07:28:21.337+00:00

    Hi @ahmedAlie ,
    I made some modifications on your code and got the result below.
    163834-20221111.gif
    Add judgment to guarantee maximum and minimum values.
    Here is my code.

    Public Sub Mouse_Scroll(sender As Object, e As MouseEventArgs) Handles Me.MouseWheel  
        If e.Delta > 0 Then  
            If PIC_MALE.Height + CInt(PIC_MALE.Height * e.Delta / 1000) > 100 Then  
                PIC_MALE.Height = 100  
            Else  
                PIC_MALE.Height += CInt(PIC_MALE.Height * e.Delta / 1000)  
            End If  
            PIC_MALE.Refresh()  
        Else  
            If PIC_MALE.Height + CInt(PIC_MALE.Height * e.Delta / 1000) < 30 Then  
                PIC_MALE.Height = 30  
            Else  
                PIC_MALE.Height += CInt(PIC_MALE.Height * e.Delta / 1000)  
            End If  
        End If  
        Label1.Text = PIC_MALE.Height  
    End Sub  
    

    Hope the code above could be helpful.
    Best Regards.
    Jiachen Li

    ----------

    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.