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;`
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?
I'm using Delphi 7 and trying to create a WebBrowser inside a Form, both at run time, but can't make it work. Here is the code:
procedure TForm1.Button1Click(Sender: TObject);
var
Form: TForm;
Brws: TWebBrowser;
begin
Form := TForm.Create(nil);
try
Form.Width := 500;
Form.Height := 500;
Form.BorderStyle := bsDialog;
Form.Position := poScreenCenter;
Form.Caption := 'Select the Option';
Brws := TWebBrowser.Create(Form);
Brws.ParentWindow := Form.Handle;
TWinControl(Brws).Parent := Form;
Brws.Align := alClient;
Brws.AddressBar := False;
Brws.MenuBar := False;
Brws.StatusBar := False;
Application.ProcessMessages;
if Form.ShowModal = mrOk then
Brws.Navigate('https://www.google.com');
finally
Form.Free;
end;
end;
The result is like WebBrowser is not responding. I got a white screen and no error messages.
Please, what am I missing? Thanks!
You are displaying the Form using its ShowModal() method, which is a synchronous (aka blocking) function that does not exit until the Form is closed. So, you are never reaching the call to Navigate() while the Form is open.
You have two options:
Use Show() instead of ShowModal(). Show() signals the Form to display itself, and then exits immediately, allowing subsequent code to run while the Form is open. As such, you will have to get rid of the try...finally and instead use the Form's OnClose event to free the Form when it is closed, eg:
procedure TForm1.Button1Click(Sender: TObject);
var
Form: TForm;
Brws: TWebBrowser;
begin
Form := TForm.Create(Self);
Form.Width := 500;
Form.Height := 500;
Form.BorderStyle := bsDialog;
Form.Position := poScreenCenter;
Form.Caption := 'Select the Option';
Form.OnClose := BrowserFormClosed;
Brws := TWebBrowser.Create(Form);
TWinControl(Brws).Parent := Form;
Brws.Align := alClient;
Brws.AddressBar := False;
Brws.MenuBar := False;
Brws.StatusBar := False;
Form.Show;
Brws.Navigate('https://www.google.com');
end;
procedure TForm1.BrowserFormClosed(Sender: TObject;
var Action: TCloseAction);
begin
Action := caFree;
end;
Otherwise, if you want to keep using ShowModal() then move the call to Navigate() into the Form's OnShow or OnActivate event instead, eg:
procedure TForm1.Button1Click(Sender: TObject);
var
Form: TForm;
Brws: TWebBrowser;
begin
Form := TForm.Create(nil);
try
Form.Width := 500;
Form.Height := 500;
Form.BorderStyle := bsDialog;
Form.Position := poScreenCenter;
Form.Caption := 'Select the Option';
Form.OnShow := BrowserFormShown;
Brws := TWebBrowser.Create(Form);
TWinControl(Brws).Parent := Form;
Brws.Align := alClient;
Brws.AddressBar := False;
Brws.MenuBar := False;
Brws.StatusBar := False;
Form.ShowModal;
finally
Form.Free;
end;
end;
procedure TForm1.BrowserFormShown(Sender: TObject);
var
Form: TForm;
Brws: TWebBrowser;
begin
Form := TForm(Sender);
Brws := TWebBrowser(Form.Components[0]);
Brws.Navigate('https://www.google.com');
end;
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
How do I create a UIPickerview in Delphi xe6 for iOS ? When making a selection for a combobox, a UI picker view will appear. How do I create a similar pickerview, but with more control over it? E.g., being able to place it anywhere on the form, customize it, not have to go through a combobox, etc. ?
I have found where in it's unit class where it is created.
FMX.Listbox
constructor TCustomComboBox.Create(AOwner: TComponent);
var
PickerService: IFMXPickerService;
begin
inherited;
if TPlatformServices.Current.SupportsPlatformService(IFMXPickerService, IInterface(PickerService)) then
begin
FListPicker := PickerService.CreateListPicker;
FListPicker.Parent := Self;
FListPicker.OnValueChanged := DoOnValueChangedFromDropDownList;
FListPicker.OnHide := DoClosePicker;
FListPicker.OnShow := DoPopup;
end;
FDropDownKind := TDropDownKind.Custom;
DropDownCount := 8;
FItemWidth := 0;
CanFocus := True;
FDroppedDown := False;
FPopup := TPopup.Create(Self);
FPopup.StyleLookup := 'combopopupstyle';
FPopup.PlacementTarget := Self;
FPopup.Stored := False;
FPopup.Parent := Self;
FPopup.Locked := True;
FPopup.DesignVisible := False;
FPopup.DragWithParent := True;
FPopup.OnClosePopup := DoClosePopup;
FPopup.OnPopup := DoPopup;
FListBox := CreateListBox;
FListBox.Parent := Popup;
FListBox.Stored := False;
FListBox.Align := TAlignLayout.Client;
FListBox.ShowCheckboxes := False;
FItemIndex := -1;
SetAcceptsControls(False);
DropDownKind := TDropDownKind.Native;
end;
I don't need the popup, so I then read about accessing properties and methods via a 'hack-ish' kind of way
Type
THackPicker = class(TCustomComboBox);
....
var
FListBox : TComboListBox;
begin
try
FListBox := THackPicker(FListBox).createListbox;
FListBox := TCustomComboBox.createListbox;
FListBox.Parent := Layout1;
FListBox.Stored := False;
FListBox.Align := TAlignLayout.Client;
FListBox.Items := ComboBox1.Items;
FListBox.OnClick := Button2Click;
except
on E : Exception do begin
showMessage(e.Message);
end;
end;
end;
App crashes here. I'm assuming this is not the correct way to go about it. Any help or direction would be much appreciated!
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;