Question draw functions problem

sabrosa

New member
Joined
Apr 8, 2014
Messages
1
Programming Experience
Beginner
Hello guys,

i'm a beginner in this forum and in vb.net programing.

So i have a couple of questions to make and i hope you can help me out with this.

i have found this exemple VB Helper: HowTo: Draw a positioning grid and snap line segments to its points in VB .NET (in the bottom of the page are the project that i'm talking)

that example have this code:

VB.NET:
Public Class Form1
    ' Drawing parameters.
    Private m_GridX As Integer = 20
    Private m_GridY As Integer = 20
    Private m_PageWid As Integer = 400
    Private m_PageHgt As Integer = 300
    Private m_ShadowThickness As Integer = 20


    ' Pens.
    Private m_PenOldLine As Pen = Pens.Black
    Private m_PenNewLine As Pen = Pens.Red
    Private m_PenGrid As Pen = Pens.Black
    Private m_PenRulerNormal As Pen = Pens.Blue
    Private m_PenRulerDrawing As Pen = m_PenNewLine


    ' Options.
    Private m_ShowGrid As Boolean = False
    Private m_SnapToGrid As Boolean = False


    ' For drawing a new line.
    Private m_Drawing As Boolean = False
    Private m_X1 As Single
    Private m_Y1 As Single
    Private m_X2 As Single
    Private m_Y2 As Single


    ' For showing position on the rulers.
    Private m_MouseX As Integer = -10
    Private m_MouseY As Integer = -10


    ' Existing lines.
    Private m_Points1() As PointF = {}
    Private m_Points2() As PointF = {}


    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        m_ShowGrid = mnuOptionsShowGrid.Checked
        m_SnapToGrid = mnuOptionsSnapToGrid.Checked


        ' Arrange the controls.
        Dim wid As Integer = Me.ClientSize.Width - picLeftRuler.Left - picLeftRuler.Width - 3
        Dim hgt As Integer = Me.ClientSize.Height - picTopRuler.Top - picTopRuler.Height - 3


        picLeftRuler.Size = New Size(picLeftRuler.Width, hgt)
        picTopRuler.Size = New Size(wid, picTopRuler.Height)
        picCanvas.Size = New Size(wid, hgt)


        picLeftRuler.Location = New Point(picLeftRuler.Left, picTopRuler.Top + picTopRuler.Height + 2)
        picTopRuler.Location = New Point(picLeftRuler.Left + picLeftRuler.Width + 2, picTopRuler.Top)
        picCanvas.Location = New Point(picTopRuler.Left, picLeftRuler.Top)
    End Sub


    Private Sub mnuFileExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuFileExit.Click
        Me.Close()
    End Sub


    Private Sub mnuOptionsShowGrid_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuOptionsShowGrid.Click
        m_ShowGrid = Not m_ShowGrid
        mnuOptionsShowGrid.Checked = m_ShowGrid


        ' Redraw.
        picCanvas.Invalidate()
    End Sub


    Private Sub mnuOptionsSnapToGrid_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuOptionsSnapToGrid.Click
        m_SnapToGrid = Not m_SnapToGrid
        mnuOptionsSnapToGrid.Checked = m_SnapToGrid
    End Sub


    ' Start drawing a line.
    Private Sub piccanvas_mousedown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles picCanvas.MouseDown
        ' Only on left mouse down.


        If e.Button <> Windows.Forms.MouseButtons.Left Then Exit Sub
        m_Drawing = True


        ' Start drawing.
        m_X1 = e.X
        m_Y1 = e.Y
        SnapToGrid(m_X1, m_Y1)
        m_X2 = m_X1
        m_Y2 = m_Y1


        picCanvas.Invalidate()
        ShowMousePosition(m_X2, m_Y2)
    End Sub


    ' Continue drawing.
    Private Sub picCanvas_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles picCanvas.MouseMove
        m_X2 = e.X
        m_Y2 = e.Y
        SnapToGrid(m_X2, m_Y2)


        ' Show the mouse position on the rulers.
        ShowMousePosition(m_X2, m_Y2)


        ' Redraw.
        If m_Drawing Then picCanvas.Invalidate()
    End Sub


    ' Finish drawing.
    Private Sub picCanvas_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles picCanvas.MouseUp
        If Not m_Drawing Then Exit Sub
        m_Drawing = False


        ' Show the mouse position in the non-drawing color.
        ShowMousePosition(m_X2, m_Y2)


        ' Save the new line.
        Dim new_index As Integer = m_Points1.Length
        ReDim Preserve m_Points1(new_index)
        ReDim Preserve m_Points2(new_index)
        m_Points1(new_index) = New PointF(m_X1, m_Y1)
        m_Points2(new_index) = New PointF(m_X2, m_Y2)


        picCanvas.Invalidate()
    End Sub


    ' Snap the point to the nearest grid location.
    Private Sub SnapToGrid(ByRef X As Integer, ByRef Y As Integer)
        ' If grid snap is off, do nothing.
        If Not m_SnapToGrid Then Exit Sub


        Dim ix As Integer = CInt(X / m_GridX)
        Dim iy As Integer = CInt(Y / m_GridY)
        X = ix * m_GridX
        Y = iy * m_GridY
    End Sub


    ' Draw the lines.
    Private Sub picCanvas_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles picCanvas.Paint
        e.Graphics.Clear(picCanvas.BackColor)


        ' Draw the grid.
        If m_ShowGrid Then
            For x As Integer = 0 To picCanvas.ClientSize.Width Step m_GridX
                For y As Integer = 0 To picCanvas.ClientSize.Height Step m_GridY
                    e.Graphics.DrawLine(m_PenGrid, x, y, x + 0.5F, y + 0.5F)
                Next y
            Next x
        End If


        ' Draw existing lines.
        For i As Integer = 0 To m_Points1.Length - 1
            e.Graphics.DrawLine(m_PenOldLine, m_Points1(i), m_Points2(i))
        Next i


        ' Draw the new line.
        If m_Drawing Then
            e.Graphics.DrawLine(m_PenNewLine, m_X1, m_Y1, m_X2, m_Y2)
        End If
    End Sub


    ' Draw the top ruler.
    Private Sub picTopRuler_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles picTopRuler.Paint
        e.Graphics.Clear(picTopRuler.BackColor)


        Dim y1 As Integer = picTopRuler.ClientSize.Height
        Dim y2 As Integer = (2 * picTopRuler.ClientSize.Height) \ 3
        Dim y3 As Integer = picTopRuler.ClientSize.Height \ 3
        Dim y4 As Integer = 0
        Dim x As Integer = 0
        For i As Integer = 0 To picTopRuler.ClientSize.Width \ m_GridX
            If i Mod 10 = 0 Then
                e.Graphics.DrawLine(m_PenGrid, x, y1, x, y4)
            ElseIf i Mod 5 = 0 Then
                e.Graphics.DrawLine(m_PenGrid, x, y1, x, y3)
            Else
                e.Graphics.DrawLine(m_PenGrid, x, y1, x, y2)
            End If
            x += m_GridX
        Next i


        ' Show the mouse position.
        If m_Drawing Then
            e.Graphics.DrawLine(m_PenRulerDrawing, m_MouseX, y1, m_MouseX, 0)
        Else
            e.Graphics.DrawLine(m_PenRulerNormal, m_MouseX, y1, m_MouseX, 0)
        End If
    End Sub


    ' Draw the left ruler.
    Private Sub picLeftRuler_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles picLeftRuler.Paint
        e.Graphics.Clear(picLeftRuler.BackColor)


        Dim x1 As Integer = picLeftRuler.ClientSize.Width
        Dim x2 As Integer = (2 * picLeftRuler.ClientSize.Width) \ 3
        Dim x3 As Integer = picLeftRuler.ClientSize.Width \ 3
        Dim x4 As Integer = 0
        Dim y As Integer = 0
        For i As Integer = 0 To picLeftRuler.ClientSize.Height \ m_GridY
            If i Mod 10 = 0 Then
                e.Graphics.DrawLine(m_PenGrid, x1, y, x4, y)
            ElseIf i Mod 5 = 0 Then
                e.Graphics.DrawLine(m_PenGrid, x1, y, x3, y)
            Else
                e.Graphics.DrawLine(m_PenGrid, x1, y, x2, y)
            End If
            y += m_GridY
        Next i


        ' Show the mouse position.
        If m_Drawing Then
            e.Graphics.DrawLine(m_PenRulerDrawing, x1, m_MouseY, x4, m_MouseY)
        Else
            e.Graphics.DrawLine(m_PenRulerNormal, x1, m_MouseY, x4, m_MouseY)
        End If
    End Sub


    ' Show the mouse position on the rulers.
    Private Sub ShowMousePosition(ByVal X As Integer, ByVal Y As Integer)
        m_MouseX = X
        m_MouseY = Y
        picTopRuler.Invalidate()
        picLeftRuler.Invalidate()
    End Sub
End Class


This code do this:


1? QUESTION:

In the last example the Grid points are fixed in the code as we can see
VB.NET:
Private m_GridX As Integer = 20
Private m_GridY As Integer = 20

and i want to define the the grid points length through the textbox's in the upper right corner ( as shown in the image above)
how do i do that?

2? QUESTION:

When i have the mouse down, in the picturebox (picCanvas), and move he draw lines, but what i want is to begin drawing when i click the button, on the upper right corner, how do i do that?

Thanks for the time spend't reading this and for my english
Regards to everyone
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,346
Location
Sydney, Australia
Programming Experience
10+
1. The whole point of variables is that their value can vary. If you want to change the grid size then set the value of one or both of those variables to something else and then redraw. I would suggest using a NumericUpDown rather than a TextBox because the input is already validated as numeric but if you must use a TextBox then you can use Integer.TryParse to validate and convert the input.

2. Are you saying that you want to draw as the user drags the mouse? If so then you will have you call Invalidate and Update on each MouseMove event so that your drawing updates each time the mouse moves.
 
Top Bottom