?
Solved

How to run a function automatically once a form is ready for user input

Posted on 2009-02-11
6
Medium Priority
?
293 Views
Last Modified: 2012-05-06
Hi experts,

On a form, I have a button and a panel (panmessage) whose caption is used to display messages.
When the user clicks the button, a function is run.

Is there any way in which I can automate that button-click by calling the function once the form is fully drawn?
The problem I have is knowing where to call the function!

Once running, the function will indicate its progress by displaying brief messages on the panel using the brief_message and paws procedures below. The function finishes by auto-closing the form.

In other words, I simply want to show the user what is going on until the function completes.

Is the solution very simple or very complex?!

tiggermark
procedure TUpdaterForm.brief_message(m:string);
begin
  panmessage.Show;  // show message m for PAWSTIME secs
  panmessage.Caption:=m;
  paws(PAWSTIME);
  panmessage.Caption:='';
  panmessage.Hide;
end;
 
procedure paws(secs:double);
var i:double;
begin
  i:=secondoftheday(now());
  while ((i+secs)>=(secondoftheday(now()))) do
    application.ProcessMessages;
end;

Open in new window

0
Comment
Question by:tiggermark
  • 3
  • 2
6 Comments
 
LVL 13

Expert Comment

by:rfwoolf
ID: 23614746
First a form is created, then it is shown, then it is activated.
Activated will occur again every time the form receives focus in the application.
In other words you probably want to run your procedure in the form's OnShow event.
You can call the button's onclick event by saying
mybutton.click,
or also mybuttonclick(sender); //where sender is equal to an object
0
 
LVL 38

Accepted Solution

by:
Geert Gruwez earned 1000 total points
ID: 23614963
the form is shown as last
if you want to do some extra action,
send the form a message, this message will be put last in the queue

is this done because of some procedure running a long time
and not using a thread ?
const
  wmstart = WM_USER + 1;
 
type
  TUpdaterForm = class(TForm)
    panMessage: TPanel;
    procedure FormCreate(Sender: TObject);
  private
    procedure Start(var Msg: TMessage); message wmstart;
    procedure brief_message(m:string);
  end;
 
procedure TUpdaterForm.FormCreate(Sender: TObject);
begin
  PostMessage(Handle, wmstart, 0, 0);
end;
 
procedure TUpdaterForm.Start(var Msg: TMessage);
begin
  brief_message('test');
end;
 
procedure TUpdaterForm.brief_message(m:string);
begin
  panmessage.Show;  // show message m for PAWSTIME secs
  panmessage.Caption:=m;
  panmessage.Update;
  paws(PAWSTIME);
  panmessage.Caption:='';
  panmessage.Hide;
end;
 
procedure paws(secs:double);
var i:double;
begin
  i:=secondoftheday(now());
  while ((i+secs)>=(secondoftheday(now()))) do
    application.ProcessMessages;
end;

Open in new window

0
 

Author Comment

by:tiggermark
ID: 23621520
Hi rtwoolf,
I had already tried combinations of what you suggest, but the form never finishes painting properly if a procedure is run from any of the form events. That is precisely the problem.

But thank you, Geert,
you have provided exactly what was needed.
The PostMessage line is now in FormActivate and a do_event procedure inside the form.start procedure.
Once the form is drawn, the do_event starts automatically.

This is being used in a MySQL database application that, at startup, reads the latest application revision from a table in the database over the internet.
The updater form appears if there is a newer version is found, and (now, thanks to you) automatically downloads the latest version (brief_messages showing the user what is happening), and once done, closing the current older application and restarting the new one.

Thank you very much indeed for the help.
Don't know why it works though!
I am quite ignorant of windows messaging.
Does the PostMessage procedure call the Start procedure?
I would much appreciate some insight as to how this messaging works!

tiggermark

0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 38

Expert Comment

by:Geert Gruwez
ID: 23621545
a form object has a queue with messages
one of these messages is painting the form on screen
when you add a message to the queue, it comes in as last and is processed last (fifo)


0
 
LVL 38

Expert Comment

by:Geert Gruwez
ID: 23621743
you want an explanation on how messages work ?
i could do this myself, but ... to make it simple

every form has a window handle, every window handle has a message queue
windows puts messages on this queue and the forms handles them one by one and removes them from that queue

that's it basically

if you want detailed info, read this:
http://delphi.about.com/od/windowsshellapi/a/aa020800a.htm
0
 

Author Comment

by:tiggermark
ID: 23622350
Thank you very much indeed, Geert.
I subscribe to Zarco Gajic's Delphi newsletter and found him most helpful and will enjoy looking into tis more (when there is a little more time).
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Screencast - Getting to Know the Pipeline
Suggested Courses

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question