Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

VB.net Draw permanent line

Posted on 2012-04-04
6
Medium Priority
?
561 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:Murray Brown
  • 3
  • 2
6 Comments
 
LVL 83

Expert Comment

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

Author Comment

by:Murray Brown
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:Murray Brown
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 86

Accepted Solution

by:
Mike Tomlinson earned 2000 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:Murray Brown
ID: 37812405
Thanks very much
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

972 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