Alex_86

hi, basically i am trying to make a data logger but have run into a bottleneck. at the moment i have a main form where the user will select to open a new plot the number of which is arbitrarily limited to 10 plots. what this does is create a new instance of another form which already has the picturebox etc sized. i wish to run it off one global timer so that the samples are read in (currently via a scrollbar) and then all plots are updated. this is the problem, how can this be done

i have tried many methods, firstly creating a routine which will update the graphs in a module, and called it with the pictureboxes passed to it. i tried using a timer on the main form to set off an event which all instances of the graph form would act upon, but as there is the possibility that none would be created the withevents didnt work.

to be honest i am totally lost on how to do this effectively, also i would eventually like to thread the timer so that the drawing will not effect the accuracy of the timing. can anyone please suggest some ways in which to update all graphs with one global timer.

thanks

Alex


Re: Visual Basic General updating multiple forms (graph plotting) simultaneously

Spidermans_DarkSide - MSP, VSIP

Hi,

You could do this from the first FORM like this, this example for two FORMs only ( Form1 & Form2 )>>

Code Snippet

Public Class Form1

'This highlighted line should be one line of code in your code window.>>

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

'1000 = 100 milliseconds = 1 second

'250 = 1/4 of a second

Timer1.Interval = 250

Timer1.Enabled = True

End Sub

'This highlighted line should be one line of code in your code window.>>

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

'Delete the following line

'if you like, it just shows the time

'at the top of FORM1 in the titlebar.>>

Me.Text = Now.ToLongTimeString

Me.Refresh()

Form2.Refresh()

End Sub

'You could also use the PAINT event to refresh the other FORM(s) too.>>

'This highlighted line should be one line of code in your code window.>>

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

Form2.Refresh()

End Sub

End Class

Regards,

S_DS






Re: Visual Basic General updating multiple forms (graph plotting) simultaneously

Alex_86

thank you so much, i cant believe i forgot about that. Anyway, i have put it so that the main form will refresh all open windows when the timer tick event occurs as such:

Code Snippet

Dim testing As Form

For Each testing In My.Application.OpenForms

testing.Refresh()

Next testing

however i run into a problem as to how to redraw the graph with the new data points. maybe i wasnt clear enough, what i am doing is drawing my own plot using drawline and a list of points. how am i able to get it so redraw itself with the refresh method you told me i tried to use the on paint event for the graph form, but all that did was get stuck in a loop, where it wouldnt exit.

thanks

Alex




Re: Visual Basic General updating multiple forms (graph plotting) simultaneously

DigBoy2000

...but all I did was get stuck in a loop.

If you has placed that snippet of loop code in your paint event, then that's exactly what would happen. By calling ALL open forms it is calling the one running the code, thus calling the OnPaint event again... and again, and again. changing the line to

If testing IsNot Me then testing.Refresh

If you are using custom graphics then maybe you can save the graphics path in a GraphicsPath object at the form's class scope and add it to the e.Graphics object from the OnPain event args.




Re: Visual Basic General updating multiple forms (graph plotting) simultaneously

Alex_86

no i didnt make that mistake, i made an even more stupid one, putting my call to the drawgraph subroutine in the picture box on paint event which it edits . however i realised the error of my ways pretty quickly and tried putting it in the form pain event where it still seems to loop continuously, yet actually draws the graph too. i was just wondering how by using form.refresh() is it possible for me to get it to redraw the graphics once, and not get itself stuck in a loop i can think of just using a variable that will stop it from repeating it, but there must be a more ellegant way here is mydrawgraph sub routing that is stored in a module.

Code Snippet

Public Sub DrawGraph(ByVal PicBox As PictureBox)

Dim bm As New Bitmap(PicBox.Width, PicBox.Height)

Dim gr As Graphics = Graphics.FromImage(bm)

Dim i As Integer = 0

gr.DrawLine(Pens.Black, 20, PicBox.Height - 20, PicBox.Width - 20, PicBox.Height - 20)

gr.DrawLine(Pens.Black, 20, 20, 20, PicBox.Height - 20)

gr.DrawString("0", New Font("Arial", 6), Brushes.Black, 20, PicBox.Height - 20)

For i = 1 To (Form1.slow_values.Count - 1)

gr.DrawLine(Pens.Blue, (20 + i), Form1.slow_values.Item(i - 1), (21 + i), Form1.slow_values.Item(i))

If (i Mod 1000) = 0 Then

gr.DrawLine(Pens.Black, 20 + i, PicBox.Height - 20, 20 + i, PicBox.Height - 15)

gr.DrawString(((i) / Form1.Timer1.Interval), New Font("Arial", 6), Brushes.Black, 20 + i, PicBox.Height - 10)

End If

Next i

PicBox.Image = bm

gr.Dispose()

End Sub

current call to draw graph

Code Snippet

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

DrawGraph(Me.PictureBox1)

End Sub

current timer event

Code Snippet

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

Dim testing As Form

For Each testing In My.Application.OpenForms

testing.Refresh()

Next testing

End Sub

thanks again

Alex





Re: Visual Basic General updating multiple forms (graph plotting) simultaneously

DigBoy2000

I think I understand a little better now what you are doing... maybe.

If you are calculating a graph image that will be used by many forms at once it would make sense for the timer.tick event -- on the main form -- to create the graph image once (perhaps have the DrawGraph procedure be a function that returns an image). Then you can iterate through the forms and set the image proprty of each form's picturebox to the graph image. This will automatically call the Invalidate event for the picturebox (and not the entire form).

If there are many different graphs being calculated then the same principle can apply but you just have to apply logic to which ones get created in what order and go to what form, etc.

I say stay away from refresh (or invalidate) and OnPaint code alltogether.

I hope this is relevant.





Re: Visual Basic General updating multiple forms (graph plotting) simultaneously

JohnWein

Take everything out of the Paint events and use this in the Timer1_Tick event:

Code Snippet

Timer1.Stop

Dim testing As Form

For Each testing In My.Application.OpenForms

testing.DrawGraph(Testing.PictureBox1)

testing.PictureBox1.Invalidate

'Application.DoEvents If you don't want all the forms to update simultaneously when you exit the Sub.

Next testing

Timer1.Start





Re: Visual Basic General updating multiple forms (graph plotting) simultaneously

Alex_86

Code Snippet

Dim testing As Form

Dim pic As Control

For Each testing In My.Application.OpenForms

pic = testing.Controls("pictureBox1")

Module1.DrawGraph(pic)

Next testing

thanks for all the help, this works pretty well, however i would still like to thread these or have some way to speed up the code, or have at least the timer on a different thread so that remains unaffected by the drawing.




Re: Visual Basic General updating multiple forms (graph plotting) simultaneously

JohnWein

If all the PictureBoxes display the same image, draw one image in the module and reference the image in the PictureBox.Image of each PictureBox to the drawn image. You can probably get more creative with the drawing of the bitmap also.