Drawline Issue VB 2008

evocube

Member
Joined
Apr 26, 2009
Messages
9
Programming Experience
1-3
I have been messing with this program for a while now. I am trying to project a line from the corner of picturebox2. Program has several issues but the biggest one is:
I get the line to draw from the corner when drawing to the form but when i try and draw to picturebox1 it is not even close to the corner of picturebox2.

Here is my code:
VB.NET:
Public Class Form1


    Dim Go As Boolean

    Dim LeftSet As Boolean

    Dim TopSet As Boolean


    ' These will hold the mouse position  

    Dim HoldLeft As Integer

    Dim HoldTop As Integer


    ' These will hold the offset of the mouse in the element  

    Dim OffLeft As Integer

    Dim OffTop As Integer

    Private Sub obj1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox2.MouseUp
        Go = False
        LeftSet = False
        TopSet = False

    End Sub


    Private Sub obj1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox2.MouseDown
        Go = True
    End Sub

    Private Sub obj1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox2.MouseMove

        ' Check if the mouse is down  

        If Go = True Then


            ' Set the mouse position  
            HoldLeft = (Control.MousePosition.X - Me.Left)

            HoldTop = (Control.MousePosition.Y - Me.Top)

            ' Find where the mouse was clicked ONE TIME  

            If TopSet = False Then
                OffTop = HoldTop - sender.Top
                ' Once the position is held, flip the switch  
                ' so that it doesn't keep trying to find the position  

                TopSet = True

            End If
            If LeftSet = False Then

                OffLeft = HoldLeft - sender.Left

                ' Once the position is held, flip the switch  

                ' so that it doesn't keep trying to find the position  

                LeftSet = True

            End If



            ' Set the position of the object  
            sender.Left = HoldLeft - OffLeft

            sender.Top = HoldTop - OffTop


        End If

    End Sub


    Private Sub Form1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox2.Click
        ' Draw line in picture box at radian angle

        Dim myPen As New System.Drawing.Pen(Color.Black)
        Dim formGraphics As System.Drawing.Graphics
        Dim degree As String
        Dim intdegree As Double

        Dim cx1 As New Integer
        Dim cy1 As New Integer
        'Dim p2x As New Integer
        ' Dim p2y As New Integer

        degree = "45"
        'angletxt.Text
        intdegree = Convert.ToDouble(degree)
        Dim conradian = (Math.PI / 180) * (intdegree)
        Dim r2p As Integer = PictureBox2.Right
        Dim b2p As Integer = PictureBox2.Bottom
        'p2x = PictureBox2.Location.X '+ r2p
        'p2y = PictureBox2.Location.Y '+ b2p
        cy1 = Convert.ToInt32((Math.Cos(conradian)) + r2p)
        cx1 = Convert.ToInt32((Math.Sin(conradian)) + b2p)

        ' cy1 = PictureBox2.Location.X + r2p
        ' cx1 = PictureBox2.Location.Y + b2p
        ' p2x = Convert.ToInt32(Math.Cos(cy1) * 50)
        ' p2y = Convert.ToInt32(Math.Sin(cx1) * 50)
        formGraphics = PictureBox1.CreateGraphics()
        formGraphics.DrawLine(myPen, r2p, b2p, cx1, cy1)
        'formGraphics.DrawLine(myPen, cx1, cy1, r2p, b2p)
        myPen.Dispose()
        formGraphics.Dispose()

Just an FYI on this program (if you care). I am trying to make it so a line is drawn from picturebox2 to the other side of picturebox2 and bounce off. These are suppose to be drawn at specific angles.
 
I haven't looked at your code but I would guess that you're using the wrong frame of reference on at least one control. When you draw on a control, the location of the drawing is relative to the upper left of that control. If you want to draw the same thing on multiple controls then you need to change the frame of reference for each control. The easiest way to do that is with the PointToScreen and PointToClient methods. Here's an example:

Draw Common "Picture" on All Controls
 
Back
Top