none
Problem with Drawline RRS feed

  • Question

  •  

    I am putting picture boxes on a panel and want to connect them with lines.

     

    I am using

         g.DrawLine(Pens.Blue, XMid, YMid, XMid2, YMid2)

    to draw the lines on the panel.

     

    The first time it executes -- it does not draw a line and does not give an error message.

    The second time it executes, I get a line

    The third and subsequent time, it gives the error message:

      A first chance exception of type 'System.ArgumentException' occurred in System.Drawing.dll

     

    The X and Y values are all within the bounds of the panel

     

     

    Problem #2:

    When it's done, any lines disappear.

     

    Wednesday, November 7, 2007 8:14 PM

Answers

  • You're on the right track with the graphics.  Your current problem is not related to drawing lines.  You should be able to add picture boxes in design time.

    Thursday, November 8, 2007 3:45 PM

All replies

  • You aren't using CreateGraphics to get g are you?

    Wednesday, November 7, 2007 8:50 PM
  • I am using:

    g = Me.PanelA.CreateGraphics

     

    in the form_load event

    Wednesday, November 7, 2007 8:53 PM
  • Don't do that if you don't like the way it behaves.  The behaviour you describe is expected.  Use the graphics provided in the Paint event or draw to a Bitmap and then display the Bitmap in the Paint event.

    Wednesday, November 7, 2007 9:02 PM
  • Are you saying that I should display a bitmap of a line?

     

    When I use similiar code on the form itself, the lines behave fine.

     

    Wednesday, November 7, 2007 9:10 PM
  • If you use similar code on the form, what happens when you minimize the form and restore it, or when another window obscures you form temporarily?  You can't create a global graphics for a control.  You shouldn't create a global graphics for anything.  You should either use the graphics provided in the Paint events or draw to bitmaps and display the bitmaps in the paint events.

    Wednesday, November 7, 2007 9:20 PM
  • Thanks.

     

    It now works OK on the first panel, I am now using

       e.Graphics.DrawLine(myPen, XMid, YMid, XMid2, YMid2)

    to draw inside of

      Private Sub PanelA_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs)  

      Handles PanelA.Paint

     

    I am trying to use two other panels on the same form to draw other lines and add picture boxes.

    It is not displaying either picture boxes or lines on these other panels

    Thursday, November 8, 2007 2:58 PM
  • You're on the right track with the graphics.  Your current problem is not related to drawing lines.  You should be able to add picture boxes in design time.

    Thursday, November 8, 2007 3:45 PM
  • I can add pictureboxes at design time, my problem is adding them at run time.

     

    I can add theses pictureboxes to one panel, but not the other 2 panels

     

    You are correct about the graphics, I can draw lines on these other two panels

     

    What I am tring to do is show the data sort of like an organization chart with boxes and connecting lines.

    There are three levels of data, I want to use the panel pictureboxes and lines for drill-down.

     

     

    Thursday, November 8, 2007 4:08 PM
  • There has to be a difference in the code in the two panels that don't show the picture boxes and the one that does.

    Thursday, November 8, 2007 4:21 PM
  • I use the same function for all three panels to add in pictureboxes-- I just pass in a different control.

    When I trace this, it passes through all three panels. It just does add them.

     

    Private Function AddPicBox(ByVal ImgIndex As Integer, ByVal X As Integer, ByVal Y As Integer, ByVal Msg As String, ByVal CNTL As Control) As String

     

    Dim aPic As New System.Windows.Forms.PictureBox

    Dim Nam As String, Pname As String

     

    Pname = LCase(CNTL.Name)

    myNodeCounter = myNodeCounter + 1

    Nam = "p" & Trim(myNodeCounter.ToString)

    aPic.Name = Nam

    aPic.Tag = Msg

    aPic.Top = Y

    aPic.Left = X

    aPic.SizeMode = PictureBoxSizeMode.AutoSize

    aPic.Image = ImageList1.Images(ImgIndex)

    aPic.Visible = True

    aPic.BringToFront()

    aPic.Parent = CNTL

     

    If Pname = "panela" Then

    PanLeft.Controls.Add(aPic)

    AddHandler aPic.MouseHover, AddressOf myPICHandler_MouseHover

    AddHandler aPic.MouseDown, AddressOf mypicHandler_MouseDown

    AddHandler aPic.MouseMove, AddressOf mypicHandler_MouseMove

    AddHandler aPic.MouseUp, AddressOf myPicAHandler_MouseUp

    ElseIf Pname = "panelb" Then

    PanelB.Controls.Add(aPic)

    AddHandler aPic.MouseHover, AddressOf myPICHandler_MouseHover

    AddHandler aPic.MouseDown, AddressOf mypicHandler_MouseDown

    AddHandler aPic.MouseMove, AddressOf mypicHandler_MouseMove

    AddHandler aPic.MouseUp, AddressOf myPicBHandler_MouseUp

    Else

    PanelC.Controls.Add(aPic)

    AddHandler aPic.MouseHover, AddressOf myPICHandler_MouseHover

    AddHandler aPic.MouseDown, AddressOf mypicHandler_MouseDown

    AddHandler aPic.MouseMove, AddressOf mypicHandler_MouseMove

    AddHandler aPic.MouseUp, AddressOf myPicCHandler_MouseUp

    End If

     

    Return Nam

    End Function

    Thursday, November 8, 2007 4:37 PM