I have an edit1 and some checkbox1 upto checkbox6 in a form. I want to insert values in edit1 component such as january when checkbox1 is clicked, february when checkbox2 is clicked... upto june for checkbox6. If a checkbox is unchecked, edit1 should not have the corresponding value. If for instance checkbox1, checkbox2 and checkbox3 are checked, I should have values like January february march in edit1. However, if I uncheck checkbox2, values in edit1 should be january and march. If none of the checkbox is checked, then edit1 should not have any values. How do i do this in Delphi 7?
Assign a single OnClick
event handler to all 6 TCheckBox
controls, and have it check all 6 TCheckBox
states and update the TEdit
accordingly, eg:
procedure TMyForm.CheckBoxClick(Sender: TObject);
var
S: String;
procedure IncludeMonth(const MonthName: String);
begin
if S <> '' then
S := S + ', ' + MonthName
else
S := MonthName;
end;
begin
if CheckBox1.Checked then IncludeMonth('January');
if CheckBox2.Checked then IncludeMonth('February');
if CheckBox3.Checked then IncludeMonth('March');
if CheckBox4.Checked then IncludeMonth('April');
if CheckBox5.Checked then IncludeMonth('May');
if CheckBox6.Checked then IncludeMonth('June');
Edit1.Text := S;
end;
Alternatively:
procedure TMyForm.CheckBoxClick(Sender: TObject);
var
Months: TStringList;
begin
Months := TStringList.Create;
try
if CheckBox1.Checked then Months.Add('January');
if CheckBox2.Checked then Months.Add('February');
if CheckBox3.Checked then Months.Add('March');
if CheckBox4.Checked then Months.Add('April');
if CheckBox5.Checked then Months.Add('May');
if CheckBox6.Checked then Months.Add('June');
Edit1.Text := Months.CommaText;
finally
Months.Free;
end;
end;