Letting the user create a line>? {RESOLVED}
Hello everyone!
I was wondering.
Is it possible to make a feature on my form that will allow me to create a line?
So that when the user clicks on a command button, it will allow him to draw a Line - (The same kind of Line as the Line in the component's section)
Does anyone know how I could do this?
Thank you all and have a great day!
Stilekid007 :D
Re: Letting the user create a line>?
Here's something I just knocked up to give you a starter hopefully:
Start a new project and a command button. paste this code into the form.
VB Code:
Option Explicit
Private m_InDrawMode As Boolean
Private m_ActivateDrawmode As Boolean
Private m_myLine As Line
Private Sub Command1_Click()
m_ActivateDrawmode = True
End Sub
Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
Static LineNumber As Long
If m_ActivateDrawmode Then
'create a new line and initialise it at the current mouse pointer
LineNumber = LineNumber + 1
Set m_myLine = Me.Controls.Add("VB.Line", "Line" & CStr(LineNumber))
m_myLine.X1 = X
m_myLine.X2 = X
m_myLine.Y1 = Y
m_myLine.Y2 = Y
m_myLine.Visible = True
m_InDrawMode = True
End If
End Sub
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
If m_InDrawMode Then
'move the end point of the line
m_myLine.X2 = X
m_myLine.Y2 = Y
End If
End Sub
Private Sub Form_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
m_InDrawMode = False
m_ActivateDrawmode = False
End Sub
now run the project and click the command button and then click on the form and hold the mouse down to draw the line.
Re: Letting the user create a line>?
You can also change these two subs of Blade's code to allow for quicker drawing of lines. Just click the command button to start drawing mode and click it again to return to normal.
VB Code:
Private Sub Command1_Click()
If (Not m_InDrawMode) Then _
m_ActivateDrawmode = Not m_ActivateDrawmode
End Sub
'---
Private Sub Form_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
m_InDrawMode = False
End Sub
Re: Letting the user create a line>?
heh, cool! nice change :)
makes the button act like a toggle on/off for drawing.
Re: Letting the user create a line>?
Hello to the both of you.
I appreciate you help!
That works nicely for drawing the Line.
Can I ask one more question?
Is there a way so that after I draw the first line - it will create a second line EXACTLY the same size and angle? (But so that the user can still move the line around once it is drawn?
Or maye even letting the user draw the first line and then he can draw a second line but the angle will be the same angle as the first one?
Does any of this make sense?
Thank you once again!
Stilekid007
Re: Letting the user create a line>?
I almost have it... its just that the slope is changing slightly as the mouse moves .... GRRR... :mad:
Re: Letting the user create a line>?
lol thank you and sorry about that sudden change;)
Re: Letting the user create a line>?
Here we go. I just figured it out, I was using X instead of X - .X1.
Basically it is y2=mx+c where m=(y2-y1)/(x2-x1) and c = y1.
VB Code:
Option Explicit
Private m_InDrawMode As Boolean
Private m_ActivateDrawmode As Boolean
Private mblnFirstLineDone As Boolean
Private m_Lines() As Line
Private Sub Command1_Click()
If (Not m_InDrawMode) Then _
m_ActivateDrawmode = Not m_ActivateDrawmode
End Sub
Private Sub Form_Load()
ReDim m_Lines(0)
Me.ScaleMode = vbPixels
End Sub
Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
If (m_ActivateDrawmode And Not m_InDrawMode) Then
'create a new line and initialise it at the current mouse pointer
If (mblnFirstLineDone) Then
ReDim Preserve m_Lines(UBound(m_Lines) + 1)
Else
mblnFirstLineDone = True
End If
Set m_Lines(UBound(m_Lines)) = Me.Controls.Add("VB.Line", "Line" & CStr(UBound(m_Lines)))
With m_Lines(UBound(m_Lines))
.X1 = X
.X2 = X
.Y1 = Y
If (UBound(m_Lines)) Then
.Y2 = X + m_Lines(UBound(m_Lines) - 1).Y2 / m_Lines(UBound(m_Lines) - 1).X2
Else
.Y2 = Y
End If
.Visible = True
End With
m_InDrawMode = True
End If
End Sub
Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
If m_InDrawMode Then 'move the end point of the line
With m_Lines(UBound(m_Lines))
' If it is the first line, allow user to change angle
If (UBound(m_Lines) = 0) Then
.Y2 = Y
Else
' If it is a subsequent line, force the angle
' to be the same
With m_Lines(0) ' Slope is of 1st line
' y = mx + c
' m = (y2-y1)/(x2-x1) + c
' ... y = ((y2-y1)/(x2-x1))x + c
Dim m As Double, c As Double
m = ((.Y2 - .Y1) / (.X2 - .X1))
c = m_Lines(UBound(m_Lines)).Y1
' x1 = (X - x0)
m_Lines(UBound(m_Lines)).Y2 = _
(m * (X - m_Lines(UBound(m_Lines)).X1)) + c
End With
End If
.X2 = X
End With
End If
End Sub
Private Sub Form_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
m_InDrawMode = False
End Sub
Re: Letting the user create a line>?
Oh wow PERFECT!
That works so awsome!
Thank you so much for that great code guys!
I will rate you! :thumb:
Stilekid007 :wave:
Re: Letting the user create a line>?
Quote:
Originally Posted by stilekid007
Oh wow PERFECT!
That works so awsome!
Thank you so much for that great code guys!
I will rate you! :thumb:
Stilekid007 :wave:
No worries :)
I'm glad high school maths came in useful for something for once :D
Re: Letting the user create a line>?
haha! Yea every once in awhile you find a place to use em. not often though LOL
Alright thanks again!
Stilekid007
Re: Letting the user create a line>? {RESOLVED}
Oh hey,
I forgot... Is there any simple code to remove the lines once they are drawn?
Re: Letting the user create a line>? {RESOLVED}
Yeah, add another Command button (Command2)
VB Code:
Private Sub Command2_Click()
Dim i As Long
For i = 0 To UBound(m_Lines)
Me.Controls.Remove "Line" & CStr(i)
Set m_Lines(i) = Nothing
Next i
ReDim m_Lines(0)
mblnFirstLineDone = False
End Sub
:)
Re: Letting the user create a line>? {RESOLVED}
Re: Letting the user create a line>? {RESOLVED}
let suppose i want to creat a line by clicking the mouse on the screen,when i click my mouse then the clicked area becomes the first parameter of the line and then the application prompts from the user that how much long do u want to create a line (as a last parameter.), i m asking this becuase i m going to creat a 3D home designing software.and i want to make it as efficient as there are in the market.for example the user clicks the screen area (after selecting the wall object from list) that becomes the first parameter of that wall and program prompts for last parameter.
regards.
Re: Letting the user create a line>? {RESOLVED}
Hey, I just got back from work - so I will try to write a reply in the morning to ya. I know how to do it ( I think ) ;)
Stilekid007
Re: Letting the user create a line>? {RESOLVED}
sir plz help me and also tell me that the code that u have mentioned before in thread is drawing line on form but i changed the form and placed there picture box but the line is starting from form not from picture box... regards
Re: Letting the user create a line>? {RESOLVED}
Ok, so you have a form and the pic box is on it and you want the line to go over the pic box and the form also. The problem your having is the Line is going under the pic box? Am I right in this asumption?
Stilekid007 :wave:
1 Attachment(s)
Re: Letting the user create a line>? {RESOLVED}
Ok, I am pretty sure that the line cannot go over the PictureBox and the form. Because....The PictureBox is a container control, like a Frame. So its either the Line on the Form and NOT on the PictureBox, or on the PictureBox and NOT on the form.
So I created a little program with an example of what you could do. (Attached)
I don't really know whether this will work for your current project but you can give it a shot.
What I did was create a picture box and made it the size of the form. The I put the command button in it so the pic box is like the form itself.
Check it out. Click on the command button then click and drag on the pic box.
If this doesn't help I would suggest posting a new thread so that other people will respond as this thread was already resolved. (There are alot of other people that might know how to do this kind of thing better.) I'm kinda a newbie ;)
Let me know how it works.
Stilekid007 :wave: