Mega Code Archive

 
Categories / Delphi / Examples
 

CheckEdit a special Editfield

Title: CheckEdit - a special Editfield Question: A few month ago, I need a TEdit, that corresponds with a Checkbox. If the Checkbox is checked the Editfield allowes input, otherwise it must be disabled. Answer: In fact I need a lot of those edits, so I made this little component. 8-) While writing and using it, I added some more features: It can interact also with two radiobuttons, and it can chance the background color to show the user the enabled/disabled state. Save the code to a file named CheckEdit.pas and compile it to a package of your choice. For more details about installing components refer to the online help. Usage: Set the new editfield on a from and connect it with a checkbox or with up to two Radiobuttons in the object inspector. If you click the checkbox, the editfield enables and disables with the state of the checkbox. unit CheckEdit; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ComCtrls, Buttons; type TCheckEdit = Class (TEdit) protected f_check: TCheckBox; f_radio1: TRadiobutton; f_radio2: TRadiobutton; f_color: TColor; private procedure Clicked (Sender: TObject); procedure SetRadio1 (rb: TRadioButton); procedure SetRadio2 (rb: TRadioButton); procedure SetCheck (cb: TCheckBox); published constructor Create(AOwner: TComponent); OVERRIDE; destructor Destroy; OVERRIDE; property Checkbox: TCheckbox read f_check write SetCheck; property RadioEnabled: TRadiobutton read f_radio1 write SetRadio1; property RadioDisabled: TRadiobutton read f_radio2 write SetRadio2; property BackColor: TColor read f_color write f_color; procedure SetStatus; end; // property CheckBox = The Checkbox that is connected to the edit // property RadioEnabled = Radiobutton that enable the edit // property RadioDisabled = Radiobutton that disenable the edit // property BackColor = Set the background color for disalbed // state // methode Setstatus = Set the status along with the // checkbox/radios. procedure Register; implementation procedure Register; begin RegisterComponents('Samples', [TCheckEdit]); end; constructor TCheckEdit.Create(AOwner: TComponent); // Create the component begin f_color:= clBtnFace; // default color for disabled edit inherited; end; destructor TCheckEdit.Destroy; begin inherited; end; procedure TCheckEdit.Clicked (Sender: TObject); // Handles the OnClick event of the Checkbox or the Radiobuttons var check: Boolean; begin if (Sender is TCheckBox) then check:= (Sender as TCheckBox).Checked else check:= (Sender as TRadioButton).Checked; if check then begin Enabled:= TRUE; Color:= clWindow; SetFocus; // Set Focus to the editfield end else begin Enabled:= FALSE; Color:= f_color; end; end; procedure TCheckEdit.SetCheck (cb: TCheckbox); // Set the Checkbox begin f_check:= cb; if Assigned (cb) then cb.OnClick:= Clicked; end; procedure TCheckEdit.SetRadio1 (rb: TRadioButton); // Set Radiobutton #1 begin f_radio1:= rb; if Assigned (rb) then rb.OnClick:= Clicked; end; procedure TCheckEdit.SetRadio2 (rb: TRadioButton); // Set Radiobutton #2 begin f_radio2:= rb; if Assigned (rb) then rb.OnClick:= Clicked; end; procedure TCheckEdit.SetStatus; // Set the whole status along the connected components begin if Assigned (f_check) then Clicked (f_check); if Assigned (f_radio1) then Clicked (f_radio1); if Assigned (f_radio2) then Clicked (f_radio2); end; end.