Results 1 to 19 of 19

Thread: Letting the user create a line>? {RESOLVED}

  1. #1

    Thread Starter
    Hyperactive Member stilekid007's Avatar
    Join Date
    Apr 2005
    Location
    DUDE! I'm your neighbor! HELLO!!!
    Posts
    388

    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
    Last edited by stilekid007; Jun 6th, 2005 at 10:18 AM.

  2. #2
    Fanatic Member Blade's Avatar
    Join Date
    Jan 1999
    Location
    Stoke-on-Trent, UK
    Posts
    527

    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:
    1. Option Explicit
    2.  
    3. Private m_InDrawMode As Boolean
    4. Private m_ActivateDrawmode As Boolean
    5. Private m_myLine As Line
    6.  
    7. Private Sub Command1_Click()
    8.  
    9.     m_ActivateDrawmode = True
    10.  
    11. End Sub
    12.  
    13. Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    14.    
    15.     Static LineNumber As Long
    16.  
    17.     If m_ActivateDrawmode Then
    18.         'create a new line and initialise it at the current mouse pointer
    19.         LineNumber = LineNumber + 1
    20.         Set m_myLine = Me.Controls.Add("VB.Line", "Line" & CStr(LineNumber))
    21.         m_myLine.X1 = X
    22.         m_myLine.X2 = X
    23.         m_myLine.Y1 = Y
    24.         m_myLine.Y2 = Y
    25.         m_myLine.Visible = True
    26.         m_InDrawMode = True
    27.     End If
    28. End Sub
    29.  
    30. Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    31.     If m_InDrawMode Then
    32.         'move the end point of the line
    33.         m_myLine.X2 = X
    34.         m_myLine.Y2 = Y
    35.     End If
    36. End Sub
    37.  
    38. Private Sub Form_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
    39.     m_InDrawMode = False
    40.     m_ActivateDrawmode = False
    41. 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.

  3. #3
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    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:
    1. Private Sub Command1_Click()
    2.     If (Not m_InDrawMode) Then _
    3.         m_ActivateDrawmode = Not m_ActivateDrawmode
    4. End Sub
    5.  
    6. '---
    7.  
    8. Private Sub Form_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
    9.     m_InDrawMode = False
    10. End Sub

  4. #4
    Fanatic Member Blade's Avatar
    Join Date
    Jan 1999
    Location
    Stoke-on-Trent, UK
    Posts
    527

    Re: Letting the user create a line>?

    heh, cool! nice change

    makes the button act like a toggle on/off for drawing.

  5. #5

    Thread Starter
    Hyperactive Member stilekid007's Avatar
    Join Date
    Apr 2005
    Location
    DUDE! I'm your neighbor! HELLO!!!
    Posts
    388

    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

  6. #6
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Letting the user create a line>?

    I almost have it... its just that the slope is changing slightly as the mouse moves .... GRRR...
    Last edited by penagate; Jun 6th, 2005 at 09:45 AM. Reason: Would ya believe, wrong smiley,...

  7. #7

    Thread Starter
    Hyperactive Member stilekid007's Avatar
    Join Date
    Apr 2005
    Location
    DUDE! I'm your neighbor! HELLO!!!
    Posts
    388

    Re: Letting the user create a line>?

    lol thank you and sorry about that sudden change

  8. #8
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    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:
    1. Option Explicit
    2.  
    3. Private m_InDrawMode        As Boolean
    4. Private m_ActivateDrawmode  As Boolean
    5. Private mblnFirstLineDone   As Boolean
    6. Private m_Lines()           As Line
    7.  
    8. Private Sub Command1_Click()
    9.     If (Not m_InDrawMode) Then _
    10.         m_ActivateDrawmode = Not m_ActivateDrawmode
    11. End Sub
    12.  
    13. Private Sub Form_Load()
    14.     ReDim m_Lines(0)
    15.     Me.ScaleMode = vbPixels
    16. End Sub
    17.  
    18. Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
    19.     If (m_ActivateDrawmode And Not m_InDrawMode) Then
    20.         'create a new line and initialise it at the current mouse pointer
    21.  
    22.         If (mblnFirstLineDone) Then
    23.             ReDim Preserve m_Lines(UBound(m_Lines) + 1)
    24.           Else
    25.             mblnFirstLineDone = True
    26.         End If
    27.  
    28.         Set m_Lines(UBound(m_Lines)) = Me.Controls.Add("VB.Line", "Line" & CStr(UBound(m_Lines)))
    29.  
    30.         With m_Lines(UBound(m_Lines))
    31.             .X1 = X
    32.             .X2 = X
    33.             .Y1 = Y
    34.             If (UBound(m_Lines)) Then
    35.                 .Y2 = X + m_Lines(UBound(m_Lines) - 1).Y2 / m_Lines(UBound(m_Lines) - 1).X2
    36.               Else
    37.                 .Y2 = Y
    38.             End If
    39.             .Visible = True
    40.         End With
    41.  
    42.         m_InDrawMode = True
    43.     End If
    44. End Sub
    45.  
    46. Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    47.     If m_InDrawMode Then    'move the end point of the line
    48.         With m_Lines(UBound(m_Lines))
    49.  
    50.             ' If it is the first line, allow user to change angle
    51.             If (UBound(m_Lines) = 0) Then
    52.                 .Y2 = Y
    53.  
    54.               Else
    55.                 ' If it is a subsequent line, force the angle
    56.                 ' to be the same
    57.  
    58.                 With m_Lines(0)                         ' Slope is of 1st line
    59.  
    60.                     ' y = mx + c
    61.                     ' m = (y2-y1)/(x2-x1) + c
    62.                     ' ... y = ((y2-y1)/(x2-x1))x + c
    63.  
    64.                     Dim m As Double, c As Double
    65.  
    66.                     m = ((.Y2 - .Y1) / (.X2 - .X1))
    67.                     c = m_Lines(UBound(m_Lines)).Y1
    68.  
    69.                     ' x1 = (X - x0)
    70.  
    71.                     m_Lines(UBound(m_Lines)).Y2 = _
    72.                         (m * (X - m_Lines(UBound(m_Lines)).X1)) + c
    73.                 End With
    74.  
    75.             End If
    76.  
    77.             .X2 = X
    78.         End With
    79.     End If
    80. End Sub
    81.  
    82. Private Sub Form_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
    83.     m_InDrawMode = False
    84. End Sub
    Last edited by penagate; Jun 6th, 2005 at 10:01 AM. Reason: Code came out ugly

  9. #9

    Thread Starter
    Hyperactive Member stilekid007's Avatar
    Join Date
    Apr 2005
    Location
    DUDE! I'm your neighbor! HELLO!!!
    Posts
    388

    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!

    Stilekid007

  10. #10
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    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!

    Stilekid007
    No worries
    I'm glad high school maths came in useful for something for once

  11. #11

    Thread Starter
    Hyperactive Member stilekid007's Avatar
    Join Date
    Apr 2005
    Location
    DUDE! I'm your neighbor! HELLO!!!
    Posts
    388

    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

  12. #12

    Thread Starter
    Hyperactive Member stilekid007's Avatar
    Join Date
    Apr 2005
    Location
    DUDE! I'm your neighbor! HELLO!!!
    Posts
    388

    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?

  13. #13
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Letting the user create a line>? {RESOLVED}

    Yeah, add another Command button (Command2)
    VB Code:
    1. Private Sub Command2_Click()
    2.     Dim i As Long
    3.     For i = 0 To UBound(m_Lines)
    4.         Me.Controls.Remove "Line" & CStr(i)
    5.         Set m_Lines(i) = Nothing
    6.     Next i
    7.     ReDim m_Lines(0)
    8.     mblnFirstLineDone = False
    9. End Sub

  14. #14

    Thread Starter
    Hyperactive Member stilekid007's Avatar
    Join Date
    Apr 2005
    Location
    DUDE! I'm your neighbor! HELLO!!!
    Posts
    388

    Re: Letting the user create a line>? {RESOLVED}

    THANKS MAN!

  15. #15
    Junior Member
    Join Date
    May 2005
    Posts
    16

    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.

  16. #16

    Thread Starter
    Hyperactive Member stilekid007's Avatar
    Join Date
    Apr 2005
    Location
    DUDE! I'm your neighbor! HELLO!!!
    Posts
    388

    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

  17. #17
    Junior Member
    Join Date
    May 2005
    Posts
    16

    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

  18. #18

    Thread Starter
    Hyperactive Member stilekid007's Avatar
    Join Date
    Apr 2005
    Location
    DUDE! I'm your neighbor! HELLO!!!
    Posts
    388

    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

  19. #19

    Thread Starter
    Hyperactive Member stilekid007's Avatar
    Join Date
    Apr 2005
    Location
    DUDE! I'm your neighbor! HELLO!!!
    Posts
    388

    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
    Attached Files Attached Files

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width