Results 1 to 11 of 11

Thread: Turning PaintEventArgs into a BackroundWorker

Threaded View

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 2011
    Posts
    105

    Turning PaintEventArgs into a BackroundWorker

    For the program listed below (a basic scrolling graph i found, and am altering), im trying to contain the PaintEventArgs to a backgroundworker such that it doesnt lock up my program, but im having trouble figuring out how to do this as im still somewhat new to this level of VB, if anyone could give me any tips or anything that would be greatly appreciated.

    Current Code:
    Code:
    Imports System.ComponentModel
    
    Public Class Form1
    
        Dim WithEvents VGraph As New BackgroundWorker
    
        Private rndGen As New Random
    
        Private _DataPoints As List(Of Integer)
        Public Property DataPoints() As List(Of Integer)
            Get
                If _DataPoints Is Nothing Then
                    _DataPoints = New List(Of Integer)
                End If
    
                Return _DataPoints
            End Get
            Set(ByVal value As List(Of Integer))
                _DataPoints = CType(value, List(Of Integer))
            End Set
        End Property
    
        Private _DrawOn As Image
        Public ReadOnly Property DrawOn() As Image
            Get
                Return _DrawOn
            End Get
        End Property
    
        Private Sub panDisplay_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs)
            Dim PanelToPaint As Panel = CType(sender, Panel)
            Dim DrawAt As Point
            Dim DrawMid As New Point(0, 140)
    
            Dim DrawPoints As New List(Of Point)
    
            'draw the image on the panel if it exist
            If DrawOn IsNot Nothing Then
                e.Graphics.DrawImage(DrawOn, New Point(5, 10))
            End If
    
            'Create a new bitmap
            _DrawOn = New Bitmap(PanelToPaint.Width, PanelToPaint.Height)
    
            'Create a Graphics object from the bitmap
            Dim imgGraphics As Graphics = Graphics.FromImage(DrawOn)
    
            'set start point
            DrawPoints.Add(DrawMid)
    
            'add points until off panel
            For Each DatPoint As Integer In DataPoints
                DrawAt = New Point(DrawMid.X + 5, 30)
                DrawMid = New Point(DrawAt.X + 5, 100)
    
                With DrawPoints
                    .Add(DrawAt)
                    .Add(DrawMid)
                End With
    
                If DrawAt.X > PanelToPaint.Width Then
                    Exit For
                End If
            Next
    
            'set image backcolor to same as panel
            imgGraphics.Clear(PanelToPaint.BackColor)
    
            'draw lines if there are any to draw
            If DrawPoints.Count > 2 Then
                imgGraphics.DrawLines(Pens.LimeGreen, DrawPoints.ToArray)
            End If
    
            'save the lines to the image
            imgGraphics.Save()
    
            'add random test data
            DataPoints.Insert(0, rndGen.Next(0, 280))
            Threading.Thread.Sleep(1000)
    
            're-fire the paint event
            'thus creating the endless loop
            PanelToPaint.Invalidate()
        End Sub
    
        Public Sub VGraph_DoWork(ByVal sender As Object, ByVal e As DoWorkEventArgs) Handles VGraph.DoWork
    
    
    
        End Sub
    
        'below code creates the controls for the example at runtime so you don't have to.
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
            With Me
    
                .DoubleBuffered = True
    
            End With
    
            AddHandler newPanel.Paint, AddressOf panDisplay_Paint
    
        End Sub
    
    End Class
    Last edited by public; Apr 4th, 2013 at 04:03 PM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width