none
Change height with mouse RRS feed

  • Question

  • Hi guys,

    I am new to visual basic 2017. I have managed to programmatically draw a vertical rectangle / line:

    Public Class Form1


    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim myPen As Pen
    myPen = New Pen(Drawing.Color.Blue, 5)

    Dim myGraphics As Graphics = Me.CreateGraphics

    myGraphics.DrawRectangle(myPen, 100, 100, 1, 50)
    End Sub
    End Class

    ---------------

    Is it possible to change its height by a user with the help of the mouse during runtime?

    Thanks!!!!
    Sunday, November 19, 2017 12:31 PM

Answers

  • Being busy with the position of the mouse seems difficult. 

    However, it ain't if you store the position of the mouse at mouse down (the event), then move the mouse and let it go up. 

    You know exactly the positions where it started from and went up. 


    Success
    Cor

    • Marked as answer by Boddah7 Wednesday, November 22, 2017 2:41 PM
    Sunday, November 19, 2017 12:40 PM
  • This is what Cor means.

    And this example draws on the graphics surface available in the paint event (e.graphics). You should use that instead of CreateGraphics as you show. Of course you can do much more. However this is the basic framework to draw with the mouse.

    Public Class Form5
        Private MouseDownY, MouseDownLineY2 As Single
        Private MouseState As Integer
    
        Private Structure Line2
            Public x1 As Single
            Public y1 As Single
            Public x2 As Single
            Public y2 As Single
        End Structure
        Private MyLine As New Line2
    
        Private Sub Form5_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            DoubleBuffered = True
    
            With MyLine
                .x1 = 100
                .y1 = 0
                .x2 = .x1
                .y2 = 100
            End With
        End Sub
    
        Private Sub Form5_MouseDown(sender As Object, e As MouseEventArgs) Handles Me.MouseDown
            MouseDownY = e.Y
            If e.Button = MouseButtons.Left Then
                MouseState = 1
                MouseDownLineY2 = MyLine.y2
            End If
        End Sub
    
        Private Sub Form5_MouseMove(sender As Object, e As MouseEventArgs) Handles Me.MouseMove
            If MouseState = 1 Then
                Dim dy As Single = e.Y - MouseDownY
    
                MyLine.y2 = MouseDownLineY2 + dy
                Invalidate()
            End If
        End Sub
    
        Private Sub Form5_MouseUp(sender As Object, e As MouseEventArgs) Handles Me.MouseUp
            MouseState = 0
            Invalidate()
        End Sub
    
        Private Sub Form5_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
            With e.Graphics
                .Clear(Color.Black)
                .SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
    
                .DrawLine(New Pen(Color.Red, 5), MyLine.x1, MyLine.y1, MyLine.x2, MyLine.y2)
    
            End With
        End Sub
    End Class

    • Proposed as answer by Cor Ligthert Sunday, November 19, 2017 10:32 PM
    • Marked as answer by Boddah7 Wednesday, November 22, 2017 2:41 PM
    Sunday, November 19, 2017 7:05 PM

All replies

  • Being busy with the position of the mouse seems difficult. 

    However, it ain't if you store the position of the mouse at mouse down (the event), then move the mouse and let it go up. 

    You know exactly the positions where it started from and went up. 


    Success
    Cor

    • Marked as answer by Boddah7 Wednesday, November 22, 2017 2:41 PM
    Sunday, November 19, 2017 12:40 PM
  • You could look at VB.NET PowerPack or the following to get started.

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Sunday, November 19, 2017 12:42 PM
    Moderator
  • This is what Cor means.

    And this example draws on the graphics surface available in the paint event (e.graphics). You should use that instead of CreateGraphics as you show. Of course you can do much more. However this is the basic framework to draw with the mouse.

    Public Class Form5
        Private MouseDownY, MouseDownLineY2 As Single
        Private MouseState As Integer
    
        Private Structure Line2
            Public x1 As Single
            Public y1 As Single
            Public x2 As Single
            Public y2 As Single
        End Structure
        Private MyLine As New Line2
    
        Private Sub Form5_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            DoubleBuffered = True
    
            With MyLine
                .x1 = 100
                .y1 = 0
                .x2 = .x1
                .y2 = 100
            End With
        End Sub
    
        Private Sub Form5_MouseDown(sender As Object, e As MouseEventArgs) Handles Me.MouseDown
            MouseDownY = e.Y
            If e.Button = MouseButtons.Left Then
                MouseState = 1
                MouseDownLineY2 = MyLine.y2
            End If
        End Sub
    
        Private Sub Form5_MouseMove(sender As Object, e As MouseEventArgs) Handles Me.MouseMove
            If MouseState = 1 Then
                Dim dy As Single = e.Y - MouseDownY
    
                MyLine.y2 = MouseDownLineY2 + dy
                Invalidate()
            End If
        End Sub
    
        Private Sub Form5_MouseUp(sender As Object, e As MouseEventArgs) Handles Me.MouseUp
            MouseState = 0
            Invalidate()
        End Sub
    
        Private Sub Form5_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
            With e.Graphics
                .Clear(Color.Black)
                .SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
    
                .DrawLine(New Pen(Color.Red, 5), MyLine.x1, MyLine.y1, MyLine.x2, MyLine.y2)
    
            End With
        End Sub
    End Class

    • Proposed as answer by Cor Ligthert Sunday, November 19, 2017 10:32 PM
    • Marked as answer by Boddah7 Wednesday, November 22, 2017 2:41 PM
    Sunday, November 19, 2017 7:05 PM
  • This is what Cor means.


    And Cor was to lazy (or maybe shy) to show code because he knows Tommy can do that much better than him.



    Success
    Cor

    Sunday, November 19, 2017 10:37 PM
  • Cor,

    I thought maybe that was the case so I added something.  :)

    However, I do try not to hog all the fun and leave some for others so feel free.

    Sunday, November 19, 2017 11:04 PM