Hello everyone,

I'm fairly new at programmig in VB 2008 and I seem to have an issue in a program I'm developing. Basically I need to draw a bar graph with data that changes dynamically (in a real application it may change every 500 ms). The code I wrote works fine with static data but when it changes in time, almost in every update a part the Bar graph goes blank and then the graph appears back. This happens very fast but it is noticeable. Does any of you have an idea on how to prevent this? Here is the code I wrote. I'm simulating the data change using a Timer (Timer1) which interval property has been set to 500ms. Thanks in advance:

'****************************************************

Option Explicit On
Imports System.Drawing.Drawing2D

Public Class Bar1

    Dim rectValue As Rectangle
    Dim BarEngHeight As Single
    Dim BarValue As Single
    Dim ZeroPos As Int32

    Const BarPixHeight As Int32 = 510
    Const BarUpperLim As Single = 15.0
    Const BarLowerLim As Single = 0.0
    Const BarTop As Int32 = 50

    Private Sub Bar1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        BarEngHeight = BarUpperLim - BarLowerLim
        ZeroPos = BarPixHeight * (BarUpperLim / BarEngHeight)

    End Sub


    Private Sub Bar1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint

        Dim Top_Top As Int32
        Dim Top_Height As Int32
        Dim Value_Top As Int32
        Dim Value_Height As Int32

        Call BarUpdate(Top_Top, Top_Height, Value_Top, Value_Height)

        e.Graphics.Clear(Color.FromArgb(&HFFECE9D8))

        Using circle_brush As New SolidBrush(Color.White)
            e.Graphics.FillRectangle(circle_brush, 38, Top_Top, 17, Top_Height)
        End Using

        Using circle_brush As New SolidBrush(Color.Black)
            e.Graphics.DrawRectangle(Pens.Black, 37, Top_Top, 19, Top_Height + Value_Height)
        End Using

        Dim color_blend As New ColorBlend(3)
        color_blend.Colors = New Color() {Color.Salmon, Color.LightYellow, Color.Red}

        color_blend.Positions = New Single() {0.0, 0.4, 1.0}

        rectValue = New Rectangle(38, Value_Top, 17, Value_Height)

        Try
            Using circle_brush As New LinearGradientBrush(rectValue, Color.DarkGray, Color.LightGray, LinearGradientMode.Horizontal)
                circle_brush.InterpolationColors = color_blend
                e.Graphics.FillRectangle(circle_brush, rectValue)
            End Using
        Catch ex As Exception

        End Try


    End Sub


    Public Sub BarUpdate(ByRef Top_Top As Int32, ByRef Top_Height As Int32, _
                        ByRef Value_Top As Int32, ByRef Value_Height As Int32)

        Dim TopHeight As Int32
        Dim ValuePos As Int32
        Dim ValueHeight As Int32

        If BarValue > BarUpperLim Then BarValue = BarUpperLim
        If BarValue < BarLowerLim Then BarValue = BarLowerLim

        Try
            TopHeight = ZeroPos * (1 - (BarValue - BarLowerLim) / BarEngHeight)
            ValuePos = BarTop + TopHeight
            ValueHeight = ZeroPos * (BarValue - BarLowerLim) / BarEngHeight

            Top_Top = BarTop
            Top_Height = TopHeight
            Value_Top = ValuePos
            Value_Height = ValueHeight

        Catch ex As Exception

        End Try

    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

        Dim Rnd As New Random()

        BarValue = 10.0 + 2.0 * (Rnd.NextDouble() - 0.5)
        Me.Refresh()

    End Sub
End Class


'****************************************************

Before asking for help I already tried drawing that bar on a PictureBox, but the results were the same. However, I solved the issue yesterday by activating the DoubleBuffer property of the form. Thanks anyway.

This article has been dead for over six months. Start a new discussion instead.