Ahmed Sammer Ahmed Sammer - 1 month ago 12
Vb.net Question

draw coordinate Two dimensions graph of (x-y) visual basic

hey every one I want to make a program that able to draw coordinate graph of Two dimensions of (x-y).when I enter a value in (x) text box and (y) text box and hit draw button it well draw the graph in the blue picture box . I searched in web sit but I found only one method that draw using the mouse and this not what I want .This is the image of the program and and it supposed to draw in white line like this image

Private Sub PictureBox2_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox2.MouseMove
Static last As Point
If e.Button = Windows.Forms.MouseButtons.Left Then
PictureBox2.CreateGraphics.DrawLine(Pens.White, last.X, last.Y, e.X, e.Y)
End If
last = e.Location
End Sub

this is the code that I found that draw using the mouse


It would be good to have a bit more details of your code, especially how and in what class you store the X and Y coordinate. By the way you draw a line between 2 points so you would need two sets of X and Y coordinate boxes on your form. After that it is as easy as what you found on the internet using the DrawLine method (https://msdn.microsoft.com/en-us/library/system.drawing.graphics.drawline(v=vs.110).aspx), you just needs to fire it from the Click event of your "draw" button.

Hi again Ahmed Here is a simple sample of a form that would draw a line when button is clicked. Of course it would need more bootstrapping to make sure user only enter integer value for the number of pixels and in its basic form, (0,0) is the top left of the panel but it can easily be converted for a bottom left approach... enter image description here


Class Form1

   Private Sub cmdDraw_Click(sender As Object, e As EventArgs) Handles cmdDraw.Click
      Dim x1 As Integer = Integer.Parse(txtX1.Text)
      Dim y1 As Integer = Integer.Parse(txtY1.Text)
      Dim x2 As Integer = Integer.Parse(txtX2.Text)
      Dim y2 As Integer = Integer.Parse(txtY2.Text)
      pnlMap.CreateGraphics.DrawLine(New Pen(Color.Black), x1, y1, x2, y2)
   End Sub

End Class