Solved

VB.net Draw permanent line

Posted on 2012-04-04
6
527 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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 

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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
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…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

773 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