I like to know how to fire events for a control created at runtime. for example,if I have created a Editbox at runtime,how could I fire the Onchange Event for that control. thanks for your answers.
: I like to know how to fire events for a control created at runtime. : for example,if I have created a Editbox at runtime,how could I : fire the Onchange Event for that control. : thanks for your answers. : You need to write the OnChange() code normally at design time. Then you can create the EditBox at run-time and set its OnChange() like you would any other "normal" property (like Left or Name). Here is a small example using a button: [code] type TForm1 := class(TForm) Button2: TButton; procedure Button2Click(Sender: TObject); private public // Events of the new button procedure Button1Click(Sender: TObject); procedure Button3Click(Sender: TObject); end;
implementation
procedure TForm1.Button1Click(Sender: TObject); begin (Sender as TButton).Caption := 'Clicked'; // Change the clicked button's caption. No matter which button it is (Sender as TButton).OnClick := Button3Click; // Link the OnClick event to another code end;
procedure TForm1.Button2Click(Sender: TObject); var B: TButton; begin B := TButton.Create(Self); B.BoundsRect := Rect(8, 8, 208, 32); // Place the button & set its size B.Caption := 'Not Clicked'; B.OnClick := Button1Click; // Link the event end;
procedure TForm1.Button3Click(Sender: TObject); begin (Sender as TButton).Caption := 'Clicked Again'; (Sender as TButton).OnClick := nil; // Completely removes the OnClick end; [/code] The form for this example only has 1 button: Button2 with the Button2Click() event. That Button creates a new button and places it in the top-left corner of the form, and links Button1Click() to its OnClick(). If the user clicks this new button, it will change its caption and its OnClick(). I suggest that you first copy-paste the code into a small test program, so you can see it run for youself and see what happens. I've included the form's type declaration, to show you how that should look. Remember events are always methods, and never procedures.
: : I like to know how to fire events for a control created at runtime. : : for example,if I have created a Editbox at runtime,how could I : : fire the Onchange Event for that control. : : thanks for your answers. : : : You need to write the OnChange() code normally at design time. Then you can create the EditBox at run-time and set its OnChange() like you would any other "normal" property (like Left or Name). Here is a small example using a button: : [code] : type : TForm1 := class(TForm) : Button2: TButton; : procedure Button2Click(Sender: TObject); : private : public : // Events of the new button : procedure Button1Click(Sender: TObject); : procedure Button3Click(Sender: TObject); : end; : : implementation : : procedure TForm1.Button1Click(Sender: TObject); : begin : (Sender as TButton).Caption := 'Clicked'; : // Change the clicked button's caption. No matter which button it is : (Sender as TButton).OnClick := Button3Click; : // Link the OnClick event to another code : end; : : procedure TForm1.Button2Click(Sender: TObject); : var : B: TButton; : begin : B := TButton.Create(Self); : B.BoundsRect := Rect(8, 8, 208, 32); // Place the button & set its size : B.Caption := 'Not Clicked'; : B.OnClick := Button1Click; // Link the event : end; : : procedure TForm1.Button3Click(Sender: TObject); : begin : (Sender as TButton).Caption := 'Clicked Again'; : (Sender as TButton).OnClick := nil; : // Completely removes the OnClick : end; : [/code] : The form for this example only has 1 button: Button2 with the Button2Click() event. That Button creates a new button and places it in the top-left corner of the form, and links Button1Click() to its OnClick(). If the user clicks this new button, it will change its caption and its OnClick(). : I suggest that you first copy-paste the code into a small test program, so you can see it run for youself and see what happens. I've included the form's type declaration, to show you how that should look. Remember events are always methods, and never procedures. :
Thanks for your reply Zibadian. If only one button is created at runtime,it is Ok with what you have given. But many such buttons are created at runtime,the same Click event will be fired for all the buttons created. How will you identify which button is selected and how to will you write different functions in the Click event for the created buttons. If you could give the code in C++ Builder it will be a great help for me.
: : : I like to know how to fire events for a control created at runtime. : : : for example,if I have created a Editbox at runtime,how could I : : : fire the Onchange Event for that control. : : : thanks for your answers. : : : : : You need to write the OnChange() code normally at design time. Then you can create the EditBox at run-time and set its OnChange() like you would any other "normal" property (like Left or Name). Here is a small example using a button: : : [code] : : type : : TForm1 := class(TForm) : : Button2: TButton; : : procedure Button2Click(Sender: TObject); : : private : : public : : // Events of the new button : : procedure Button1Click(Sender: TObject); : : procedure Button3Click(Sender: TObject); : : end; : : : : implementation : : : : procedure TForm1.Button1Click(Sender: TObject); : : begin : : (Sender as TButton).Caption := 'Clicked'; : : // Change the clicked button's caption. No matter which button it is : : (Sender as TButton).OnClick := Button3Click; : : // Link the OnClick event to another code : : end; : : : : procedure TForm1.Button2Click(Sender: TObject); : : var : : B: TButton; : : begin : : B := TButton.Create(Self); : : B.BoundsRect := Rect(8, 8, 208, 32); // Place the button & set its size : : B.Caption := 'Not Clicked'; : : B.OnClick := Button1Click; // Link the event : : end; : : : : procedure TForm1.Button3Click(Sender: TObject); : : begin : : (Sender as TButton).Caption := 'Clicked Again'; : : (Sender as TButton).OnClick := nil; : : // Completely removes the OnClick : : end; : : [/code] : : The form for this example only has 1 button: Button2 with the Button2Click() event. That Button creates a new button and places it in the top-left corner of the form, and links Button1Click() to its OnClick(). If the user clicks this new button, it will change its caption and its OnClick(). : : I suggest that you first copy-paste the code into a small test program, so you can see it run for youself and see what happens. I've included the form's type declaration, to show you how that should look. Remember events are always methods, and never procedures. : : : : : : Thanks for your reply Zibadian. : If only one button is created at runtime,it is Ok with what : you have given. : But many such buttons are created at runtime,the same Click event : will be fired for all the buttons created. : How will you identify which button is selected and how to will you : write different functions in the Click event for the created buttons. : If you could give the code in C++ Builder it will be a great help for me. : : I'm a purely Delphi programmer, so the last request is a bit difficult for me. The Sender parameter identifies the object, which fired the event. This holds true for all events. You can write different functions for different types of buttons, but only at design-time. It is theoretically possible to design a program, which can add its own completely new functions at run-time, but that is nearly impossible given the complexity. Depending on what the buttons are supposed to do, you can either use the caption to differentiate the behaviour (example: previously opened files buttons), or you can use the Tag property in combination with a case-of (C++: switch statement).
Comments
: for example,if I have created a Editbox at runtime,how could I
: fire the Onchange Event for that control.
: thanks for your answers.
:
You need to write the OnChange() code normally at design time. Then you can create the EditBox at run-time and set its OnChange() like you would any other "normal" property (like Left or Name). Here is a small example using a button:
[code]
type
TForm1 := class(TForm)
Button2: TButton;
procedure Button2Click(Sender: TObject);
private
public
// Events of the new button
procedure Button1Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
end;
implementation
procedure TForm1.Button1Click(Sender: TObject);
begin
(Sender as TButton).Caption := 'Clicked';
// Change the clicked button's caption. No matter which button it is
(Sender as TButton).OnClick := Button3Click;
// Link the OnClick event to another code
end;
procedure TForm1.Button2Click(Sender: TObject);
var
B: TButton;
begin
B := TButton.Create(Self);
B.BoundsRect := Rect(8, 8, 208, 32); // Place the button & set its size
B.Caption := 'Not Clicked';
B.OnClick := Button1Click; // Link the event
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
(Sender as TButton).Caption := 'Clicked Again';
(Sender as TButton).OnClick := nil;
// Completely removes the OnClick
end;
[/code]
The form for this example only has 1 button: Button2 with the Button2Click() event. That Button creates a new button and places it in the top-left corner of the form, and links Button1Click() to its OnClick(). If the user clicks this new button, it will change its caption and its OnClick().
I suggest that you first copy-paste the code into a small test program, so you can see it run for youself and see what happens. I've included the form's type declaration, to show you how that should look. Remember events are always methods, and never procedures.
: : for example,if I have created a Editbox at runtime,how could I
: : fire the Onchange Event for that control.
: : thanks for your answers.
: :
: You need to write the OnChange() code normally at design time. Then you can create the EditBox at run-time and set its OnChange() like you would any other "normal" property (like Left or Name). Here is a small example using a button:
: [code]
: type
: TForm1 := class(TForm)
: Button2: TButton;
: procedure Button2Click(Sender: TObject);
: private
: public
: // Events of the new button
: procedure Button1Click(Sender: TObject);
: procedure Button3Click(Sender: TObject);
: end;
:
: implementation
:
: procedure TForm1.Button1Click(Sender: TObject);
: begin
: (Sender as TButton).Caption := 'Clicked';
: // Change the clicked button's caption. No matter which button it is
: (Sender as TButton).OnClick := Button3Click;
: // Link the OnClick event to another code
: end;
:
: procedure TForm1.Button2Click(Sender: TObject);
: var
: B: TButton;
: begin
: B := TButton.Create(Self);
: B.BoundsRect := Rect(8, 8, 208, 32); // Place the button & set its size
: B.Caption := 'Not Clicked';
: B.OnClick := Button1Click; // Link the event
: end;
:
: procedure TForm1.Button3Click(Sender: TObject);
: begin
: (Sender as TButton).Caption := 'Clicked Again';
: (Sender as TButton).OnClick := nil;
: // Completely removes the OnClick
: end;
: [/code]
: The form for this example only has 1 button: Button2 with the Button2Click() event. That Button creates a new button and places it in the top-left corner of the form, and links Button1Click() to its OnClick(). If the user clicks this new button, it will change its caption and its OnClick().
: I suggest that you first copy-paste the code into a small test program, so you can see it run for youself and see what happens. I've included the form's type declaration, to show you how that should look. Remember events are always methods, and never procedures.
:
Thanks for your reply Zibadian.
If only one button is created at runtime,it is Ok with what
you have given.
But many such buttons are created at runtime,the same Click event
will be fired for all the buttons created.
How will you identify which button is selected and how to will you
write different functions in the Click event for the created buttons.
If you could give the code in C++ Builder it will be a great help for me.
: : : for example,if I have created a Editbox at runtime,how could I
: : : fire the Onchange Event for that control.
: : : thanks for your answers.
: : :
: : You need to write the OnChange() code normally at design time. Then you can create the EditBox at run-time and set its OnChange() like you would any other "normal" property (like Left or Name). Here is a small example using a button:
: : [code]
: : type
: : TForm1 := class(TForm)
: : Button2: TButton;
: : procedure Button2Click(Sender: TObject);
: : private
: : public
: : // Events of the new button
: : procedure Button1Click(Sender: TObject);
: : procedure Button3Click(Sender: TObject);
: : end;
: :
: : implementation
: :
: : procedure TForm1.Button1Click(Sender: TObject);
: : begin
: : (Sender as TButton).Caption := 'Clicked';
: : // Change the clicked button's caption. No matter which button it is
: : (Sender as TButton).OnClick := Button3Click;
: : // Link the OnClick event to another code
: : end;
: :
: : procedure TForm1.Button2Click(Sender: TObject);
: : var
: : B: TButton;
: : begin
: : B := TButton.Create(Self);
: : B.BoundsRect := Rect(8, 8, 208, 32); // Place the button & set its size
: : B.Caption := 'Not Clicked';
: : B.OnClick := Button1Click; // Link the event
: : end;
: :
: : procedure TForm1.Button3Click(Sender: TObject);
: : begin
: : (Sender as TButton).Caption := 'Clicked Again';
: : (Sender as TButton).OnClick := nil;
: : // Completely removes the OnClick
: : end;
: : [/code]
: : The form for this example only has 1 button: Button2 with the Button2Click() event. That Button creates a new button and places it in the top-left corner of the form, and links Button1Click() to its OnClick(). If the user clicks this new button, it will change its caption and its OnClick().
: : I suggest that you first copy-paste the code into a small test program, so you can see it run for youself and see what happens. I've included the form's type declaration, to show you how that should look. Remember events are always methods, and never procedures.
: :
:
:
:
: Thanks for your reply Zibadian.
: If only one button is created at runtime,it is Ok with what
: you have given.
: But many such buttons are created at runtime,the same Click event
: will be fired for all the buttons created.
: How will you identify which button is selected and how to will you
: write different functions in the Click event for the created buttons.
: If you could give the code in C++ Builder it will be a great help for me.
:
:
I'm a purely Delphi programmer, so the last request is a bit difficult for me. The Sender parameter identifies the object, which fired the event. This holds true for all events. You can write different functions for different types of buttons, but only at design-time. It is theoretically possible to design a program, which can add its own completely new functions at run-time, but that is nearly impossible given the complexity.
Depending on what the buttons are supposed to do, you can either use the caption to differentiate the behaviour (example: previously opened files buttons), or you can use the Tag property in combination with a case-of (C++: switch statement).