By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,593 Members | 2,006 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,593 IT Pros & Developers. It's quick & easy.

Line Help

P: 9
I am working a problem VB-2005 and I am a little confused. If I used the following, I can draw lines. But if I try to use what lesson calls for I cannot draw lines.
graphicsObject.DrawLine(New Pen(Color.Black), _
e.X, e.Y, DIAMETER, DIAMETER)

Versus

graphicsObject.DrawLine(New Pen(Color.Black), _
x1, y1, x2, y2)
is this because I used the following statement ealier in the program?

' set diameter of MouseDown line
Private Const DIAMETER As Integer = 8
Nov 28 '07 #1
Share this Question
Share on Google+
7 Replies


Expert 5K+
P: 8,434
You haven't made it clear which one works, and which doesn't. Or what happens when it doesn't work.
Nov 28 '07 #2

Expert 100+
P: 487
You never point which event you are coding this and what is the value of x1,y1 and x2,y2!
Is it x1,y1 and x2,y2 are constant or following the mouse pointer?
Nov 28 '07 #3

P: 9
graphicsObject.DrawLine(New Pen(Color.Black), _
e.X, e.Y, DIAMETER, DIAMETER)

This one works (and it is following the mouse) my eventual goal is going to be to calculate the distance between end points of this line.
Nov 28 '07 #4

Expert 100+
P: 487
First post what you did in your code and describe about your problem in detail. (Do not simply post 2 or 3 line of code.)
Nov 28 '07 #5

P: 9
First post what you did in your code and describe about your problem in detail. (Do not simply post 2 or 3 line of code.)
Here is what I have so far, most of it was based off of an example. I am suppose to use the following, but when I do I can not draw the line:

Expand|Select|Wrap|Line Numbers
  1. graphicsObject.DrawLine(New Pen(Color.Black), x1, y1, x2, y2)
  2.  
  3. Public Class LineLengthForm
  4.  
  5.     Dim x1 As Integer
  6.     Dim x2 As Integer
  7.     Dim y1 As Integer
  8.     Dim y2 As Integer
  9.  
  10.     ' specify whether moving the mouse should erase
  11.     Private shouldErase As Boolean = False
  12.  
  13.     ' specify whether moving the mouse should draw
  14.     Private shouldDraw As Boolean = False
  15.  
  16.     ' set diameter of MouseDown line
  17.     Private Const DIAMETER As Integer = 8
  18.  
  19.     ' create and initialize Graphics object
  20.     Private graphicsObject As Graphics = CreateGraphics()
  21.  
  22.     ' handles LineLength's MouseDown event
  23.     Private Sub LineLengthForm_MouseDown(ByVal sender As Object, _
  24.     ByVal e As System.Windows.Forms.MouseEventArgs) _
  25.     Handles Me.MouseDown
  26.  
  27.         ' draw on Form if the left button is held down
  28.         If e.Button = Windows.Forms.MouseButtons.Left Then
  29.             shouldDraw = True
  30.             ' erase black lines if right button is held down
  31.         ElseIf e.Button = Windows.Forms.MouseButtons.Right Then
  32.             shouldErase = True
  33.         End If
  34.     End Sub ' MouseDown
  35.  
  36.     ' handles LineLengthForm's MouseMove event
  37.     Private Sub LineLengthForm_MouseMove(ByVal sender As Object, _
  38.     ByVal e As System.Windows.Forms.MouseEventArgs) _
  39.     Handles Me.MouseMove
  40.  
  41.         ' draw line if mouse button is pressed
  42.         If shouldDraw = True Then
  43.             graphicsObject.DrawLine(New Pen(Color.Black), _
  44.             e.X, e.Y, DIAMETER, DIAMETER)
  45.             ' mouse pointer "erases" if right mouse button is pressed
  46.         ElseIf shouldErase = True Then
  47.             graphicsObject.DrawLine(New Pen(BackColor), _
  48.             e.X, e.Y, DIAMETER, DIAMETER)
  49.         End If
  50.     End Sub 'LineLength_MouseMove
  51.  
  52.     ' handle LineLength's MouseUp event
  53.     Private Sub LineLengthForm_MouseUp(ByVal sender As Object, _
  54.     ByVal e As System.Windows.Forms.MouseEventArgs) _
  55.     Handles Me.MouseUp
  56.  
  57.         shouldDraw = False ' do not draw on the form
  58.         shouldErase = False ' do not erase
  59.     End Sub 'LineLength_MouseUp
Nov 28 '07 #6

Expert 100+
P: 487
Expand|Select|Wrap|Line Numbers
  1. graphicsObject.DrawLine(New Pen(Color.Black), _
  2.             e.X, e.Y, DIAMETER, DIAMETER)
here you have to change like
Expand|Select|Wrap|Line Numbers
  1. graphicsObject.DrawLine(New Pen(Color.Black), _
  2.             X1, Y1, e.X, e.Y)
and in mouse down event

Expand|Select|Wrap|Line Numbers
  1. Private Sub LineLengthForm_MouseDown(ByVal sender As Object, _
  2.     ByVal e As System.Windows.Forms.MouseEventArgs) _
  3.     Handles Me.MouseDown
  4.  
  5.         ' draw on Form if the left button is held down
  6.         If e.Button = Windows.Forms.MouseButtons.Left Then
  7.             shouldDraw = True
  8.             ' erase black lines if right button is held down
  9.         ElseIf e.Button = Windows.Forms.MouseButtons.Right Then
  10.             shouldErase = True
  11.         End If
  12.         X1 = e.X
  13.         Y1 = e.Y
  14.     End Sub ' MouseDown
and in mouse up X1 and Y1 to 0
and same thing apply to errase logic.
Note : This code will draw line to start and end point of mouse pointer. so it will not be a single line (it will look like shaded line, you don't draw a Straight line).
Nov 29 '07 #7

P: 9
Thanks for the insight!!!!!!!!!!!
Nov 30 '07 #8

Post your reply

Sign in to post your reply or Sign up for a free account.