Results 1 to 11 of 11

Thread: Turning PaintEventArgs into a BackroundWorker

  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.

  2. #2
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Turning PaintEventArgs into a BackroundWorker

    Paint events can't be handled like this. It's very unlikely that any paint events are the cause of any major delays though. I think you need to look for your problems somewhere else.
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Aug 2011
    Posts
    105

    Re: Turning PaintEventArgs into a BackroundWorker

    Well if i have visual basic dynamically adding data to an excel file, is there a way to create a dynamically updating graph imported to visual basic from excel?

  4. #4
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Turning PaintEventArgs into a BackroundWorker

    If your data is coming from the VB program, why do you need to import it back from Excel rather than using it directly? If anything is going to slow things down, that is!
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Aug 2011
    Posts
    105

    Re: Turning PaintEventArgs into a BackroundWorker

    Yes my data is being produced by a backgroundworker in visual basic, and I would LOVE to do that however i need visual basic to display a dynamically scrolling graph, and im not sure how to do that in visual basic (im using VB 2010).
    Last edited by public; Apr 4th, 2013 at 05:26 PM.

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Aug 2011
    Posts
    105

    Re: Turning PaintEventArgs into a BackroundWorker

    Or is there a way to set up a chart and set it so that only a certain number of x-axis values appear on the chart, and the min and max x values are constantly increasing by 1 making it look like the graph is scrolling? Ive got a basic program listed below, if anyone could do what im asking with it, itd be greatly appreciated:

    Code:
    Public Class Form1
    
        Dim rndm As New Random
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
            Timer1.Interval = 1000
    
    
            Chart1.Series(0).Points.AddXY(Now, 100)
    
    
            Chart1.Series(0).XValueType = DataVisualization.Charting.ChartValueType.Time
            Chart1.Series(0).ChartType = DataVisualization.Charting.SeriesChartType.Line
    
            Chart1.ChartAreas(0).AxisY.Minimum = 100
            Chart1.ChartAreas(0).AxisY.Maximum = 130
            Chart1.ChartAreas(0).AxisY.Interval = 10
    
            Chart1.ChartAreas(0).AxisX.LabelStyle.Format = "mm:ss"
    
            Timer1.Start()
    
        End Sub
    
        Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
            Chart1.Series(0).Points.AddXY(Now, rndm.Next(100, 131))
        End Sub
    
    End Class

  7. #7
    Frenzied Member circuits2's Avatar
    Join Date
    Sep 2006
    Location
    Kansas City, MO
    Posts
    1,027

    Re: Turning PaintEventArgs into a BackroundWorker

    I'm not sure how you could put PaintEventArgs in a backgroundworker because a backgroundworker cannot access the UI thread.
    Show the love! Click (rate this post) under my name if I was helpful.

    My CodeBank Submissions: How to create a User Control | Move a form between Multiple Monitors (Screens) | Remove the MDI Client Border | Using Report Viewer with Visual Studio 2012 Express

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Aug 2011
    Posts
    105

    Re: Turning PaintEventArgs into a BackroundWorker

    Quote Originally Posted by circuits2 View Post
    I'm not sure how you could put PaintEventArgs in a backgroundworker because a backgroundworker cannot access the UI thread.
    What im trying to achieve is a dynamically scrolling graph, but i cant figure out how to do it for the life of me.

  9. #9
    Frenzied Member circuits2's Avatar
    Join Date
    Sep 2006
    Location
    Kansas City, MO
    Posts
    1,027

    Re: Turning PaintEventArgs into a BackroundWorker

    Since you are doing the data manipulation in a backgroundworker, why not use the progresschanged event to "scroll" your chart?
    Show the love! Click (rate this post) under my name if I was helpful.

    My CodeBank Submissions: How to create a User Control | Move a form between Multiple Monitors (Screens) | Remove the MDI Client Border | Using Report Viewer with Visual Studio 2012 Express

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Aug 2011
    Posts
    105

    Re: Turning PaintEventArgs into a BackroundWorker

    Quote Originally Posted by circuits2 View Post
    Since you are doing the data manipulation in a backgroundworker, why not use the progresschanged event to "scroll" your chart?
    Im unfamiliar with the progresschanged event also i dont even know how one would scroll a chart.

    Actually i just though of how to scroll a chart, i have to shift the variables one data point to the right every time i get a new piece of data, however i dont know how to limit the max number of X-Axis items, or how to remove the X-Axis labels for each line.

  11. #11
    Frenzied Member circuits2's Avatar
    Join Date
    Sep 2006
    Location
    Kansas City, MO
    Posts
    1,027

    Re: Turning PaintEventArgs into a BackroundWorker

    The axis labels are a collection like everything else. You can set the x-axis labels each time you increment the data.

    This gives you an idea of accessing the labels. I haven't dealt with charts enough to be more insightful on the code portion:

    http://www.gigasoft.com/netcharting.html
    Show the love! Click (rate this post) under my name if I was helpful.

    My CodeBank Submissions: How to create a User Control | Move a form between Multiple Monitors (Screens) | Remove the MDI Client Border | Using Report Viewer with Visual Studio 2012 Express

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