0

I have a databound chart in a winforms application, written in vb.net. I have setup the 3 points in my data and set their colours, but when the chart is filled at runtime, it ignores the pre-defined points and chooses random colours.

In order to get around this, I have added 3 lines of code to manually set the colours of the points after the data is generated. I have tried putting this in the Load event and the Shown event, but each time it fails due to the reference being outside of the index.

I think this is happening because the form isn't shown on screen yet, and so the points don't actually exist just yet.

I tried putting the code in a button and clicking it once the chart had loaded, and this changed the colours successfully.

Code in Load event:

Me.Prod_ChecklistGraphTableAdapter.Fill(Me.ProdGraph.prod_ChecklistGraph)

Code in Shown or below the above line in Load event:

chtActiveStatus.Series(0).Points(0).Color = Color.FromArgb(180, 204, 112)
chtActiveStatus.Series(0).Points(1).Color = Color.FromArgb(255, 200, 61)
chtActiveStatus.Series(0).Points(2).Color = Color.FromArgb(255, 61, 61)

My question is, how can I make this code change the colours before the chart is shown to the user and preventing it from flickering from one colour to the right colour? Or alternatively, how can I set the chart properties so that it doesn't ignore the pre-definded Points and colours I have set up?

RazorKillBen
  • 561
  • 2
  • 20
  • 1
    You have to do it in the constructor or Load event to prevent it from being visible. But that's incompatible with the data binding you depend on, that's lazy and doesn't occur until the control becomes visible. I'd give up on data binding before considering the hackorama in [this Q+A](https://stackoverflow.com/q/2523778/17034). – Hans Passant Nov 08 '19 at 10:58
  • Thanks @HansPassant - do you think it'd be better to give up with the databinding from the SQL view, and just have the vb code pull the data and manually update the graph on creation in the `Load` or `Shown` events? – RazorKillBen Nov 12 '19 at 14:17

1 Answers1

0

This happen because your .Fill method is working (get thread busy and blocking the rendering).

Try call your

Me.Prod_ChecklistGraphTableAdapter.Fill(Me.ProdGraph.prod_ChecklistGraph)

in async mode

Or Try this:

    Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown
        changeColoursAsync(Nothing, Nothing)
    End Sub

    Private Sub changeColoursAsync(sender As Object, e As EventArgs)

        If sender Is Nothing Then

            Dim starter As Timer = New Timer
            starter.Interval = 1
            AddHandler starter.Tick, AddressOf changeColoursAsync
            starter.Start()
            Exit Sub

        End If

        CType(sender, Timer).Stop()
        CType(sender, Timer).Dispose()


        chtActiveStatus.SuspendLayout()

        chtActiveStatus.Series(0).Points(0).Color = Color.FromArgb(180, 204, 112)
        chtActiveStatus.Series(0).Points(1).Color = Color.FromArgb(255, 200, 61)
        chtActiveStatus.Series(0).Points(2).Color = Color.FromArgb(255, 61, 61)

        Application.DoEvents()

        chtActiveStatus.ResumeLayout()

        Console.WriteLine("Timer tick")

    End Sub
G3nt_M3caj
  • 2,497
  • 1
  • 14
  • 16
  • Thanks @G3nt_M3caj - do you mean to call the `.Fill` line in the `Form_Shown` event also? Or to add that line to the `changeColoursAsync` sub you added above? – RazorKillBen Nov 12 '19 at 14:15
  • You can call changeColoursAsync in Form1_Shown event (as the code above show) and Prod_ChecklistGraphTableAdapter.Fill in Form1_Show event. :) – G3nt_M3caj Nov 12 '19 at 14:28
  • Sorry @G3nt_M3caj - I think I may be misunderstanding. I have added the `Prod_ChecklistGraphTableAdapter.Fill` before the `Async` call and it works, but it still shows the default colour scheme briefly for about a second and a half before flicking to the right colours. – RazorKillBen Nov 12 '19 at 21:24
  • Hi @RazorKillBen. Prod_ChecklistGraphTableAdapter.Fill should to be called after changeColoursAsync is called. – G3nt_M3caj Nov 13 '19 at 08:21