Forum > Beginners
how to assign onClick event for checbox
vks:
Hello..
I am dynamically creating checkboxes based on database values.
How to create the OnClick event for that CheckBoxes ?
I tried assigning procedure to onclick property of check box.
--- Code: ---procedure ChkClk(Sender: TCheckBox);
begin
if ( Sender.Checked=True) then
ShowMessage('checked')
Else
ShowMessage('not checked');
end;
--- End code ---
--- Code: ---CheckBox.OnClick := ChkClk(CheckBox);
--- End code ---
But it shows error 'untype' found. expected "<procedure variable type of procedure(TObject) of object;Register>".
Please do help me solve it.
Zoran:
1. Events are not plain procedures, but methods (declared as "procedure of object" - which means, procedures declared inside the class definition, procedures that belong to an object). So, you should declare your ChkClk procedure inside your TForm1 (or whatever) class:
--- Code: --- TForm1 = class(TForm)
private
{ private declarations }
procedure ChkClk(Sender: TObject); // declared inside the TForm1 class
...
end;
--- End code ---
and implemented later in same unit (note "TForm1." prefix in front of method name):
--- Code: ---procedure TForm1.ChkClk(Sender: TObject);
...
--- End code ---
2. if you use mode objfpc (which is default in Lazarus, so you probably do), you should use the adress operator (that is @ operator) in front of the method variable:
--- Code: ---CheckBox1.OnClick := @ChkClk;
--- End code ---
3. In your code Sender.Checked is wrong, because Sender is of TObject type, which does not have Checked property. However, you can check if Sender is actually of TCheckBox type and use cast:
--- Code: ---procedure TForm1.ChkClk(Sender: TObject);
begin
if Sender is TCheckBox then begin
if TCheckBox(Sender).Checked then
ShowMessage('checked')
else
ShowMessage('not checked');
end;
end;
--- End code ---
vks:
Hey Zoran..
hats off.. your suggestion helped me a lot.
Thanks so much :D :D :D :)
vks:
Hello,
Can't I assign a custom string value to any of checkbox properties, apart from checkbox.caption?
howardpc:
Only to those checkbox properties that are strings (or equivalent to strings), such as
Hint
HelpKeyword
AccessibleValue
AccessibleDescription
Navigation
[0] Message Index
[#] Next page