My problem is like below, so I want all the checkboxes Bold,Italic and Underline to work out if I checked all of them.
I try searching similar problem from this site to help me but their question just to confusing..
procedure TForm1.CheckBox1Click(Sender: TObject);
begin
if Checkbox1.Checked = True then
Label1.Font.Style := [fsBold] else
Label1.Font.Style := [];
end;
procedure TForm1.CheckBox2Click(Sender: TObject);
begin
if Checkbox2.Checked = True then
Label1.Font.Style := [fsItalic] else
Label1.Font.Style := [];
end;
procedure TForm1.CheckBox3Click(Sender: TObject);
begin
if Checkbox3.Checked = True then
Label1.Font.Style := [fsUnderline] else
Label1.Font.Style := [];
end;
end;
The font style is a set of different TFontStyles, so for each checkbox you need to add the respective style to the set, if it is checked or remove it, if it is unchecked, e.g.
if Checkbox1.Checked then
Label1.Font.Style := Label1.Font.Style + [fsBold];
else
Label1.Font.Style := Label1.Font.Style - [fsBold]
PS: You should always use Boolean values directly and not compare them to True/False
Related
I wrote this redundant code consisting of 30 lines:
if Button = TMouseButton.mbLeft then
begin
if pnlEndColor.ShowCaption then
begin
pnlStartColor.ShowCaption := False;
pnlEndColor.ShowCaption := False;
pnlStartColor.Color := ThisColor;
pnlEndColor.Color := ThisColor;
end
else
begin
pnlStartColor.ShowCaption := False;
pnlStartColor.Color := ThisColor;
end;
end
else if Button = TMouseButton.mbRight then
begin
if pnlStartColor.ShowCaption then
begin
pnlStartColor.ShowCaption := False;
pnlEndColor.ShowCaption := False;
pnlStartColor.Color := ThisColor;
pnlEndColor.Color := ThisColor;
end
else
begin
pnlEndColor.ShowCaption := False;
pnlEndColor.Color := ThisColor;
end;
end;
I manually refactored the code by extracting it to a small method by applying just logic:
procedure TForm1.SetPanelColors(Panel1, Panel2: TPanel; const aColor: TColor);
begin
if Panel2.ShowCaption then
begin
Panel1.ShowCaption := False;
Panel2.ShowCaption := False;
Panel1.Color := aColor;
Panel2.Color := aColor;
end
else
begin
Panel1.ShowCaption := False;
Panel1.Color := aColor;
end;
end;
Then I used the method by these 4 lines of code (Savings of 26 lines compared to the previous redundant code):
if Button = TMouseButton.mbLeft then
SetPanelColors(pnlStartColor, pnlEndColor, ThisColor)
else
SetPanelColors(pnlEndColor, pnlStartColor, ThisColor);
How could such a refactoring of redundant code be automated? Are there any libraries or general resources for such a purpose?
Here is how I am creating My PageControl.
PageCtrlSub := TPageControl.Create(Self);
PageCtrlSub.Parent := GroupSub;
PageCtrlSub.Align := alClient;
SubFormCnt := 0;
TblOdSub.First;
while not TblOdSub.Eof do
begin
SubPartNo := TblOdSub.FieldByName('sub_part_no').AsString;
AddNewSubTab(SubPartNo,Prc1Rs);
TblOdSub.Next;
end;
Here is how I am Creating my TabSheet and Form on the tabSheet.
procedure TFrmSub.AddNewSubTab(PartNo : String; PrcRs : TPriceRec);
var
i : Integer;
begin
inc(SubFormCnt);
TabSheet := TTabSheet.Create(PageCtrlSub);
TabSheet.Caption := 'Sub '+ intToStr(SubFormCnt);
TabSheet.PageControl := PageCtrlSub;
Form := TFrmSubExchange.Create(Self);
Form.Name := 'SForm' + IntToStr(SubFormCnt);
Form.Parent := TabSheet;
for i := 0 to Componentcount-1 do
begin
if (Components[i] is TFrmSubExchange) and (Components[i].Name = 'SForm' + IntToStr(SubFormCnt)) then
TFrmSubExchange(Components[i]).DataChangedSub(PartNo, PrcRs);
end;
Form.Show;
end;
I have a TCaption on each form that is created. When the user changes tab and press a button I need to know the text stored in the TCaption.caption property on the form of the active tab?
Thanks in Advance
Without seeing the DFM for TFrmSubExchange, this is just a guess, but you can try something like this:
procedure TFrmSub.SomeButtonClick(Sender: TObject);
var
s: string;
begin
s := (PageCtrlSub.ActivePage.Controls[0] as TFrmSubExchange).Caption1.Caption;
...
end;
Issue: I have two checkboxes (only one has to check at a time) and one edit box. For the both the checkboxes when they are checked, value in the edit box has to change to '0' otherwise original value will remain in the edit box. Below are the two onclick events for the two checkboxes. when I write chkFacetoFace.checked := false the respective onclick handler is called and I was unable to achieve the desired result. Can anyone please help me on resolving this issue ?
procedure TForm1.chkFacetoFaceClick(Sender: TObject);
begin
if chkFacetoFace.Checked then
begin
edtConvFee.Text := '0.00';
chkWaiveOff.Checked := False;
end
else
begin
edtConvFee.Text := '5.00';
end;
end;
procedure TForm1.chkWaiveOffClick(Sender: TObject);
begin
if chkWaiveOff.Checked then
begin
edtConvFee.Text := '0.00';
chkFacetoFace.Checked := False;
end
else
begin
edtConvFee.Text := '5.00';
end;
end;
As TLama commented:
You can temporarily unassign the OnClick handlers.
Value of EditBox.Text is set in not intended cases. You should check both Checkboxe's Checked states before changing EditBox.Text. See this example:
.
procedure TForm1.chkWaiveOffClick(Sender: TObject);
begin
if chkWaiveOff.Checked then
begin
edtConvFee.Text := '0.00';
chkFacetoFace.Checked := False;
end
else if not chkFaceToFace.Checked then // <-- additional check for the respective checkbox
begin
edtConvFee.Text := '5.00';
end;
end;
#David Heffernan said "Check box is wrong here."
try this:
procedure TForm1.chk1Click(Sender: TObject);
begin
inherited;
chk1.OnClick := nil;
chk2.OnClick := nil;
chk2.OnClick := nil;
if TCheckBox(Sender).Name = 'chk1' then
begin
chk2.Checked := False;
chk3.Checked := False;
end
else if TCheckBox(Sender).Name = 'chk2' then
begin
chk1.Checked := False;
chk3.Checked := False;
end
else if TCheckBox(Sender).Name = 'chk3' then
begin
chk1.Checked := False;
chk2.Checked := False;
end;
chk1.OnClick := chk1Click;
chk2.OnClick := chk1Click;
chk3.OnClick := chk1Click;
end;`
I am doing this program in Delphi 7 and using a Page-Control do any of you have a quick way of resetting the Check Boxes and Combo Boxes that is op the page ? With out calling each Check Box and changing its Properties ? Because their is about 150 Check Boxes in the program and don't want to type every ones name out to reset it to unchecked ?
I Tried to use the following code :
var
i : Integer;
cb : TCheckBox;
cbx : TComboBox;
begin
ADOQuery1.SQL.Clear;
for i := 1 to (ComponentCount) do
Begin
if Components[i] is TCheckBox then
begin
cb := TCheckBox(Components[i]);
cb.checked := false;
end;
if Components[i] is TComboBox then
begin
cbx := TComboBox(Components[i]);
cbx.ItemIndex := -1;
end;
end;
End;
But I get a error List out od Bounds ? Any ideas why ?
Off the top of my head....This should run.
procedure ResetControls(aPage:TTabSheet);
var
loop : integer;
begin
if assigned(aPage) then
begin
for loop := 0 to aPage.controlcount-1 do
begin
if aPage.Controls[loop].ClassType = TCheckBox then
TCheckBox(aPage.Controls[loop]).Checked := false
else if aPage.Controls[loop].ClassType = TComboBox then
TComboBox(aPage.Controlss[loop]).itemindex := -1;
end;
end;
end;
edit: Corrected as pointed out by Remy
You could do something like this within the form:
for i := 0 to ComponentCount-1 do
if Components[i] is TCheckBox then begin
cb := TCheckBox(Components[i]);
cb.checked := false;
end;
end;
procedure ResetControls(Container: TWinControl);
var
I: Integer;
Control: TControl;
begin
for I := 0 to Container.ControlCount - 1 do
begin
Control := Container.Controls[I];
if Control is TCheckBox then
TCheckBox(Control).Checked := False
else
if Control is TComboBox then
TComboBox(Control).ItemIndex := -1;
//else if ........ other control classes
ResetControls(Control as TWinControl); //recursive to process child controls
end;
end;
Is it possible to configure Multicolor Multiline TMemo in Delphi XE2?.
When I am writing codes like :
procedure TForm1.BitBtn1Click(Sender: TObject);
var
FirstVariable, SecondVariable, ThirdVariable :BOOL;
begin
if FirstVariable then
begin
Memo1.Font.Color := clGreen;
Memo1.Lines.Add('FirstVariable = True');
end
else if SecondVariable then
begin
Memo1.Font.Color := clBlue;
Memo1.Lines.Add('SecondVariable = True');
end
else
begin
Memo1.Font.Color := clRed;
Memo1.Lines.Add('ThirdVariable = True');
end;
end;
font color for all the previously existing lines are getting changed according to condition of the variables.
No, it is not possible. But you can use a RICHEDIT control instead, e.g., the TRichEdit wrapper.
RichEdit1.SelAttributes.Color := clGreen;
RichEdit1.Lines.Add('First line.');
RichEdit1.SelAttributes.Color := clBlue;
RichEdit1.Lines.Add('Second line.');
RichEdit1.SelAttributes.Color := clRed;
RichEdit1.Lines.Add('Third line.');