Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

messagedialog at a different way? 1

Status
Not open for further replies.

Raven078

Programmer
Jan 14, 2003
22
0
0
NL
Hello,

We want to make use of a dialogwindow like messagedlg, but it is not supposed to disable the mainform. Any idea how we can do this?
 
Use the CreateMessageDialog function.

If you are unsure how to use this then search the Delphi Help for 'CreateMessageDialog'.

Andrew
 
I don't think that'll do what he wants, he'll need to create a modeless dialog if you want to be able to change focus back to the original form and leave the dialog open.

There are 10 types of people in the world. Those that understand binary and those that don't.
 
Hi Raven078,

I would suggest you try GExperts, to what I
remember there should be a MessageDlg creation
expert. If I remember bad, you can always try
creating your own dialog form, which is not
difficult in most cases.

Regarding this last aspect, to correctly
mimick the common dialogs, remember the
borders and do them bad, so they'll be
recognized as system dialogs(<g>)

As an alternative, there're some
components( like JVCL ) that interface to
system dialogs in a very good way, if I was you,
I'd give them an eyeshot.

Cheers,

Andrew
 
StellaIndigo, the CreateMessageDialog function returns a TForm. Note that CreateMessageDialog does not display the dialog form.

It is up to the programmer how the dialog form is displayed. Using ShowModal will obviously display a Modal dialog. If the Visible property of the form is set to True a non modal dialog is displayed.

I think something like this will do the trick for Raven078:
Code:
const
  UserMessage = 'Reply Yes or No';
var
  dlg: TForm;
...
...
...
  dlg := CreateMessageDialog(UserMessage, mtConfirmation, [mbYes,mbNo] );
  dlg.Visible := true;
...
...
...
...
  dlg.Free;

Andrew
 
I have tried to use the createmessagedialog function, like your example above, but I can't get any result out of the buttons.
 
towerbase, your example will display the dialog in Modeless form but you can't get a response from any of the buttons because ShowModal handles the closure and response back to the calling object.

raven078, the quickest way to do this is create a new form with the properties similar to a dialog box.

Stella

There are 10 types of people in the world. Those that understand binary and those that don't.
 
I suppose I should have coded the function completely.

Let's suppose that raven078 wants a modless dialog saying 'Reply Yes or No' that has two buttons Yes and No. He wants the procedure YesClick to be called when the Yes button is clicked and the NoClick procedure to be called when the No button is clicked. Both the YesClick and NoClick procedures will be in his main form (not the dialog form) which we call Form1. In this example, the two procedures YesClick and NoClick will simply call ShowMessage with an appropriate text.
The call to create the modeless dialog will be in the OnClick handler for TButton1.

This is what you need to code in Form1:

step 1. Add the following three lines in the public part of TForm1:
Code:
 dlg: TForm;
 procedure YesClick(Sender: TObject);
 procedure NoClick(Sender: TObject);
The OnClick handler for Button1 will be something like:
Code:
procedure TForm1.Button1Click(Sender: TObject);
var
  c: integer;
const
  UserMessage = 'Reply Yes or No';
begin
  dlg := CreateMessageDialog(UserMessage, mtConfirmation, [mbYes,mbNo] );
  for c := 0 to dlg.ComponentCount - 1 do
    if dlg.Components[c].Name = 'Yes' then
      (dlg.Components[c] as TButton).OnClick := YesClick
    else if dlg.Components[c].Name = 'No' then
      (dlg.Components[c] as TButton).OnClick := NoClick;
  dlg.Visible := true;
end;
We then add the following two procedures to handle the Yes and No clicks:
Code:
procedure TForm1.YesClick(Sender: TObject);
begin
  ShowMessage ( 'Yes clicked' );
end;

procedure TForm1.NoClick(Sender: TObject);
begin
  ShowMessage ( 'No clicked' );
end;

At some appropriate point the dlg form will be freed.

I suggest that is easier than creating your own form in the style of a Message Dialog. Why reinvent the wheel?


Andrew





 
sneaky and I like it. ;o)

There are 10 types of people in the world. Those that understand binary and those that don't.
 
As is often the case, there is a neater way of coding something. The button event handler would be better as:
Code:
procedure TForm1.Button1Click(Sender: TObject);
var
  c: integer;
const
  UserMessage = 'Reply Yes or No';
begin
  dlg := CreateMessageDialog(UserMessage, mtConfirmation, [mbYes,mbNo] );
  (dlg.FindComponent ( 'Yes' ) as TButton).OnClick := YesClick;
  (dlg.FindComponent ( 'No' ) as TButton).OnClick := NoClick;
  dlg.Visible := true;
end;

Andrew
 
Now the tricky part of the problem:

I have more than one button that shows a dialog. Can I use the form &quot;dlg&quot; more than once at the same time?

When I twice press a button that shows a dialog, I'll get a dialog twice, but for some reason the procedures YesClick and NoClick don't run completly anymore.

I also don't understand how the dialog makes a different in the button who called this dialog and how the mainform makes a different in which dialog sended a result.
 
Raven078

You should not assign more that one MessageDialog to the 'dlg' variable at a time.

If you need multiple MessageDialog forms to be visible at the same time then you should have multiple 'dlg' variables. For example 'dlg1', 'dlg2' and so on.

It would be sensible to disable the button that created the dialog once the dialog had been created. When you free the dialog you could enable the button.

I don't understand your last paragraph.

Andrew

 
If you need multiple MessageDialog forms to be visible at the same time then you should have multiple 'dlg' variables. For example 'dlg1', 'dlg2' and so on.

I already thought so, but is there anyway to do this automaticly instead of creating them all? (like dlg or something)

It would be sensible to disable the button that created the dialog once the dialog had been created. When you free the dialog you could enable the button.

I already did this.

I don't understand your last paragraph.

I ment...When I only use one dialog that popped up more then once, how does the mainform know wich one sended a result, but that doesn't matter since they have the same eventhandlers. Certainly not when I use multiple dialogs (with) there own eventhandlers. I was a little confused about using a form that does not physicly exist already.

Where can I find out what properties,functions etc. they have en how I change/use them, since they don't exist yet in designtime? For example the Formstyle.
I can do dlg.Formstyle := fsStayonTop; but it is not advised to do this on runtime, normally you use the object inspector.

I'm sorry for all this silly questions, I only worked with forms I designed myself before. Thanks a lot anyway, you helped very much.
 
Instead of everything going italic, i ment:

I already thought so, but is there anyway to do this automaticly instead of creating them all? (like dlg or something)
 
It depends on what you mean by &quot;automatically&quot;. You could have an array of dialogs as you suggest such as dlg.

You can find out what properties and functions are available by having a look at the source code for the CreateMessageDialog function (which is how I found out what they are).

Define and design what you need and then have a go at coding and testing it. If you really get stuck come back to Tek-Tips.

Andrew
 
TGML strikes again. My previous message should have looked like this:

It depends on what you mean by &quot;automatically&quot;. You could have an array of dialogs as you suggest such as
Code:
dlg[i]

You can find out what properties and functions are available by having a look at the source code for the CreateMessageDialog function (which is how I found out what they are).

Define and design what you need and then have a go at coding and testing it. If you really get stuck come back to Tek-Tips.

Andrew
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top