Solved

VB.net Draw permanent line

Posted on 2012-04-04
6
522 Views
Last Modified: 2012-04-05
Hi
When I use the following code the line drawn seems to dissapear when
the next line is drawn. How do I make it permanent

 Sub oDrawLine()

        Dim Gr As Graphics = Me.TreeView_From.CreateGraphics()
        ' Create pen.

        Dim orangePen As Pen
        orangePen = New Pen(Color.DarkOrange, 2)

       Dim point1 As New Point(10, 10)
        Dim point2 As New Point(100, 500)

        Gr.DrawLine(orangePen, point1, point2)

    End Sub
0
Comment
Question by:murbro
  • 3
  • 2
6 Comments
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 37805410
Draw it in the Paint event.
0
 

Author Comment

by:murbro
ID: 37805450
Hi. Thanks. Where do I find that and how would the code look?
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 37805874
There is an OnPaint event.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:murbro
ID: 37806045
Hi. This is all a little obscure. What triggers the OnPaint event? There is no event  like that behind a TreeView. I a need a little more detail please.Thanks for the help
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 37806621
You can either:

(1) INHERIT from TreeView, set UserDraw to True, and override OnPaint().

(2) Use NATIVEWINDOW to trap the WM_PAINT message:
Public Class Form1

    Private WithEvents TVS As TreeViewSink

    Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        TVS = New TreeViewSink(Me.TreeView1)
    End Sub

    Private Sub TVS_Paint(TV As System.Windows.Forms.TreeView) Handles TVS.Paint
        Using G As Graphics = TV.CreateGraphics
            Debug.Print("Drawing Line")
            G.DrawLine(Pens.Red, 0, 0, TreeView1.Width, TreeView1.Height)
        End Using
    End Sub

End Class

Public Class TreeViewSink
    Inherits NativeWindow

    Private TV As TreeView
    Private Ignore As Boolean = False
    Private Const WM_PAINT As Integer = &HF

    Public Event Paint(ByVal TV As TreeView)

    Public Sub New(ByVal TV As TreeView)
        If Not IsNothing(TV) AndAlso TV.IsHandleCreated Then
            Me.TV = TV
            Me.AssignHandle(TV.Handle)
        End If
    End Sub

    Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
        Select Case m.Msg
            Case WM_PAINT
                If Not Ignore Then
                    Ignore = True
                    TV.Refresh()
                End If
                Ignore = False

                RaiseEvent Paint(Me.TV)

        End Select

        MyBase.WndProc(m)
    End Sub

End Class

Open in new window

0
 

Author Closing Comment

by:murbro
ID: 37812405
Thanks very much
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now