Log in

View Full Version : Use the form in a class (Delphi)


Sirber
22nd September 2003, 18:06
Hi

Immagine I have a form with a button on it. When I click the button, I would like to send the entire form to a function outside of it, but I don't know how.

Here what I want, but in VB:

private sub button_click()
DoSomething(Me)
end sub

***

public sub DoSomething(Sender As Form)
Sender.button.caption = "I did it"
end sub


Thanks a lot!!!

aquaplaning
23rd September 2003, 11:28
procedure TForm1.Button1Click(Sender: TObject);
begin
DoSomething(self);
end;

procedure TForm1.DoSomething(form: TForm);
begin
(form as TForm1).Button1.Caption := 'I did it';
end;

Sirber
25th September 2003, 00:50
Thanks a lot!!!! :D :D :D

[edit]

This don't work if I want to use the form in another module (.pas), like in a class :(

unmei
25th September 2003, 06:33
are you sure this other module (unit) knows what the type Tform1 is ? ie is Tform1 public and did you include Form1 unit in one of the the uses clauses of the other unit ?

i have not tried this with forms (i wouldnt know a use for it), but from a theoretical point, there is no reason why it should stop working in a different unit.

Sirber
25th September 2003, 15:41
"form: TForm" create a new form, and don't use the form I send to it. Delphi, at runtime, tells me that my controls aren't defined, and aren't listed.

aquaplaning
25th September 2003, 16:03
post your code and the exact errormessage
then perhaps we can help you...

Sirber
25th September 2003, 22:11
It's not really an error, and the code don't show it. It's just that I can't use the form with it's controls outside of it.

In VB I could use a form(.frm) inside a module(.bas), but not in Delphi :(

esby
30th September 2003, 11:54
well i don't understand quite what is your problem,
but i would do something like that from the first example:


unit myForm1;
...
implementation
...
procedure TForm1.Button1Click(Sender: TObject);
begin
DoSomething(self);
end;

/// in your other unit

implementation

uses myform1;
...
procedure DoSomething(form:TForm1);
begin
form.Button1.Caption := 'I did it';
end;

Of course if you just want to modify the button,
you could use :

procedure doSomething(sender:Tobject);
begin
if assigned(Tbutton(sender)) // this way we check we have a button
then Tbutton(sender).caption := 'I did it';
// we could check if we have another type of component too
// and do other actions
// meaning one procedure handling all type of click
// we can even check sender.name vs the one you gave it
// and do differents actions per button and have all buttons
// handled by a single procedure
end;



and associate doSomething with Tform1.button1.onclic()
either at creation or at run time.

esby

PS: after reading the thread again,
maybe you just want to associate the onclick event listener to a procedure doSomething(sender:Tobject);

in this case, just put on your Tform.oncreate

button1.onclick := doSomething;

Sirber
30th September 2003, 21:03
Thanks, I'll try.

esby
6th October 2003, 12:47
a little erratum

From what i tested recently,
with
procedure doSomething(sender:Tobject);

and button.onclick := doSomething

won't work together because procedure != object procedure,

but you can use a trick to have it working.

unit UMyStupidUnit;
(...)

type TStupidButton = class(Tbutton)
procedure doSomething(sender:Tobject);
end;


implementation

procedure TStupidButton.doSomething(sender:Tobject);

begin
caption := 'i was here';
end;


with that, you ll be able to do:

implementation

uses UMyStupidUnit;

procedure form.create;
begin
mybutton.onclick := TStupidButton(mybutton).doSomething;
end;