Link to home
Start Free TrialLog in
Avatar of FrankTheRat
FrankTheRat

asked on

Update 2nd form from main form

My application produces a set of data that changes over time. I am trying to display this data in a floating dialog box, which is a stayontop toolwindow. Ultimately I will want to display several of these floating windows (3 or 4).

Examples of the code I'm using are as follows:

procedure TForm1.StartButtonClick(Sender: TObject);
begin
  Form2.Show;  {display the floating window....}
  Timer1.Enabled := true;  {off we go....}
end;

procedure TForm1.Timer1Timer(Sender: TObject);
begin
{Evaluate new data....}
  ...
  ...do some stuff...
  ...
{Update TChart on form 2....}
  with Form2.Chart1 do
  begin
    ...
    ...do some stuff...
    ...
  end;
end;


Although the application compiles and run's OK, the dialog box (Form2) is never updated, regardless of whether it, or the main form (Form1) has the focus.
I therefore assume that Form2 never processes messages. I tried showing Form2 as modal, but of course then Form1 didn't respond, presumably for the same reason.

I'm after some advice on the best approach to take. Specific code is not required, although I won't be offended if anyone includes some!

I do not particularly want to run Form2 in its own thread if I can avoid it; I already have several threads running and do not want to add more thread management overhead.
Do I need to force Form2 to process messages or am I doing something really stupid?

Thanks,
   FrankTheRat.


ASKER CERTIFIED SOLUTION
Avatar of kretzschmar
kretzschmar
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
That should work fine... provided the Timer1Timer procedure finishes each time it's called, and you wait for the timer to go off again

e.g.

procedure Timer1Timer(...)
begin
  DoStuff;
  UpdateChart;
end;

is OK, but

proceure Timer1Timer(...)
begin
  while true do
  begin
    DoStuff
    UpdateChart;
  end;
end;

never gives Form2 a chance.
Avatar of sfock
sfock

try some Application.ProcessMessages within the do stuff
Avatar of FrankTheRat

ASKER

Thanks for trying. Just tried your suggestion. Neither
  Form2.Refresh;
nor
  Form2.Chart1.Refresh;
has any effect.

I copied the TChart to form1 and update both as follows:
{Update TChart on form 1....}
 with Chart1 do
 begin
   ...
   ...do some stuff...
   ...
 end;
{Update TChart on form 2....}
 with Form2.Chart1 do
 begin
   ...
   ...do the same stuff...
   ...
 end;

The Chart on Form1 updates correctly, nothing on form 2.

Still a confused Rat......

FrankTheRat
did you tried Form2.Chart1.Update; ?
wbr, mo.
Hi,

Perhaps we need more info about your OnTimer procedure because the following works fine here. There is one Label and one TChart (Bars style with one series) on the form2.

uses unit2;

var
  i: integer;

procedure TForm1.Button1Click(Sender: TObject);
begin
  i := 1;
  Form2.Show;
  Timer1.Enabled := true;
end;

procedure TForm1.Timer1Timer(Sender: TObject);
begin
  Form2.Label1.Caption := IntToStr(i);
  with Form2.Series1 do begin
    Clear;
    Add(i+10,'aaa',clRed);
    Add(i+20,'bbb',clRed);
    Add(i+15,'ccc',clRed);
  end;
  i := i + 1;
end;

Regards, Geo
OK, you've all convinced me; I'm doing something stupid. Testing, I'll get back to you tomorrow.

Please wait......>_

FrankThe(Stupid)Rat
You are all, of course, completely correct; it works fine if you do it right.

I haven't yet discovered what I was doing wrong but I've just re-written the code from scratch and it works. Maybe we'll never know; I don't have the time to debug an error that no longer exists.

Thank you for your help, I have asked the moderators how I go about sharing the points between you
(kretzschmar, andrewjb, sfock, mocarts, geobul).

Thanks again,
    FrankTheFool
Hi FrankTheRat,

I've refunded 80 points to enable you to accept the comment for one expert and to post "Points for <expertname>" Q's for the other experts in the same topic area.

Please:
1) Post the link to the original Q in the "Points for <expertname>" and
2) Add in the original Q a comment with the link to the "Points for <expertname>", thus the email notif will warn the expert.

modulo

Community Support Moderator
Experts Exchange
kretzschmar, thanks, I accept your answer, you get 20 points.....

andrewjb, thanks, you get 20 points here:
https://www.experts-exchange.com/questions/20523837/Points-for-andrewjb.html

sfock, thanks, you get 20 points here:
https://www.experts-exchange.com/questions/20523841/Points-for-sfock.html

mocarts, thanks, you get 20 points here:
https://www.experts-exchange.com/questions/20523844/Points-for-mocarts.html

geobul, thanks, you get 20 points here:
https://www.experts-exchange.com/questions/20523850/Points-for-geobul.html