Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27333

Turning PaintEventArgs into a BackroundWorker

$
0
0
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


Viewing all articles
Browse latest Browse all 27333

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>