Solved

VB.net Draw permanent line

Posted on 2012-04-04
6
533 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

 

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Suggested Solutions

Title # Comments Views Activity
Calling web service and its methods dynamically 2 73
VB.Net Tranfer data between to VB.Net apps 8 38
vb.net 1 month apart 11 35
Access/Visual Basic Question 3 23
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

792 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