How to Send to back for a control at runtime? - delphi

First, Please download this file ( download ).
how to i can set Form2 to "Send to back" for show Image1 to user ??
i use Image1.BringToFront; but this code not work!!
here is main unit:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls;
type
TForm1 = class(TForm)
Panel1: TPanel;
Image1: TImage;
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
uses unit2;
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
tFrm2:Tform2;
begin
tFrm2:=Tform2.Create(self);
tFrm2.Parent:=self;
tFrm2.Align:=alClient;
tFrm2.Show;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
// Image1.BringToFront;
end;
end.

The way you're doing it, many Form2 instances can be stacked over the image, so you can search for all child forms (I mean, all forms which parent is Form1) and hide each. Final result is image is shown again.
procedure TForm1.Button2Click(Sender: TObject);
var
I: Integer;
begin
for I := 0 to Screen.FormCount - 1 do
if (Screen.Forms[I].Parent = Self) then
Screen.Forms[I].Hide;
end;
Best regards.

Related

Custom Component - Get Application ActiveControl name

Hi I have a custom component with 2 Tedit boxes in a panel. When the focus leaves one edit it should set focus on the other edit unless a certain external component(s) has focus. This is straightforward as a VCL app:
unit dem;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StrUtils, StdCtrls, ExtCtrls;
type
TForm1 = class(TForm)
Panel1: TPanel;
Edit1: TEdit;
Edit2: TEdit;
Memo1: TMemo;
Button1: TButton;
Button2: TButton;
Button3: TButton;
procedure Edit2Exit(Sender: TObject);
procedure Edit1Exit(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Edit1Exit(Sender: TObject);
begin
if AnsiContainsStr(Memo1.Text, Screen.ActiveControl.Name) = True then Exit
else Edit2.SetFocus;
end;
procedure TForm1.Edit2Exit(Sender: TObject);
begin
Edit1.SetFocus;
end;
end.
This switches the focus (on exit) between the 2 edit boxes unless Button2 is clicked.
In my component (I have substituted TStrings(TStringlist) for Memo1 and the TEdits are actually TDBEdits. I need a way to get the application's activecontrol name so I can utilitise the above code. Obviously Screen.ActiveControl.Name does not work but what will? I would be grateful for any help.
Windows 11, delphi 7
Edit
In respect of Andreas Rejbrand’s and Remy Lebeau’s concerns about using OnExit I have rethought this and decided that this component is pointless. I can use something like this to solve the tabbing part
unit dem;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StrUtils, StdCtrls, TypInfo, ExtCtrls;
type
TForm1 = class(TForm)
Panel1: TPanel;
Edit1: TEdit;
Edit2: TEdit;
Memo1: TMemo;
EnterData: TButton;
Button2: TButton;
Button3: TButton;
procedure EnterDataClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure ToggleTab(ACont:TWinControl;yorn:Boolean);
var
x:Integer;
procedure HasTextProp(AControl: TControl; yorn:boolean);
begin
if IsPublishedProp(AControl, 'TabStop') = True then (AControl as TWinControl).TabStop := yorn;
end;
begin
for x:=0 to ACont.ControlCount-1 do
begin
HasTextProp(ACont.Controls[x],yorn);
end;
end;
procedure TForm1.EnterDataClick(Sender: TObject);
begin
ToggleTab(Form1,False);
Edit1.TabStop:=True;
Edit2.TabStop:= True;
Edit1.SetFocus;
end;
end.
And then using OnActiveControlChange to manage the flow. I am sorry to have wasted all of your times but I did learn NOT to use OnExit in this way so thank you for that.

Project raised exception class EListError with message 'List index out of bound (1)'. Process stopped. Use Step or Run to continue

I made simple clicker game and i want to show my speed of clicking, in graph.
unit Unit2;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls, TeEngine, Series, TeeProcs, Chart;
type
TForm2 = class(TForm)
Label1: TLabel;
Label2: TLabel;
Button1: TButton;
Button2: TButton;
Button3: TButton;
Button4: TButton;
Timer1: TTimer;
Chart1: TChart;
Series1: TLineSeries;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form2: TForm2;
clicks_total,i: integer;
implementation
{$R *.dfm}
procedure TForm2.FormShow(Sender: TObject);
begin
Timer1.Enabled:=true;
end;
procedure TForm2.Button1Click(Sender: TObject);
begin
clicks_total:=clicks_total+1;
Label2.Caption:=IntToStr(clicks_total);
end;
procedure TForm2.FormCreate(Sender: TObject);
begin
clicks_total:=0;
i:=1;
end;
procedure TForm2.Timer1Timer(Sender: TObject);
var clicks_total_support:integer;
begin
i:=i+1;
clicks_total_support:=clicks_total;
Chart1.Series[1].AddXY(i,clicks_total_support,'Hello',clBlue);
clicks_total_support:=1;
end;
end.
I want to make every second a point in graph where X will be only 1 greater and Y (height of point) will be that clicks user made in one second. But after 1 second running the code, it will crash with error that's in name of this question. Someone help?

Delphi - Panel & Button Click Effect

I have a CheckBox that shows and hides a Panel that has a Button & two TEdits (for entering an IP address & its port).
The problem is the Button has no effect, it stays gray, also the Panel still shows. I tried different methods, e.g. ModalResult := mrOk;, which didn't change anything.
Here is my code:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls;
type
TForm1 = class(TForm)
CheckBox1: TCheckBox;
Panel1: TPanel;
Edit1: TEdit;
Edit2: TEdit;
Button1: TButton;
procedure CheckBox1Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.CheckBox1Click(Sender: TObject);
begin
if CheckBox1.Checked = True then begin
Panel1.Visible := True;
end
else
if CheckBox1.Checked = False then begin
Panel1.Visible := False;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
Button1: TButton;
begin
Button1 := Sender as TButton;
ShowMessage(Button1.Caption + ' Changes');
end;
end.
I hope I'm understanding your question..
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls;
type
TForm1 = class(TForm)
CheckBox1: TCheckBox;
Panel1: TPanel;
Edit1: TEdit;
Edit2: TEdit;
Button1: TButton;
procedure CheckBox1Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure FormShow(Sender: TObject);
private
{ Private declarations }
procedure Showpanel(AShow: boolean);
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Showpanel(AShow: boolean);
begin
Checkbox1.checked := AShow;
Panel1.Visible := AShow;
end;
procedure TForm1.FormShow(Sender: TObject);
begin
ShowPanel(false);
end;
procedure TForm1.CheckBox1Click(Sender: TObject);
begin
Panel1.visible := Checkbox1.Checked;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
Showmessage(Button1.Caption + ' Changes');
end;
end.

Saving record to file Error 'file access denied'

When I run my code an select the save button which i created. The record doesnt save but i get an error 'file access denied'.
my code :
The code i split into 2 units MainUnit and AddTenantUnit.
I think the problem lies within the procedure at the end of the code. If you scroll down I made it clear which procedure (TAddTenantForm.SaveButtonClick).
unit MainUnit;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TMainForm = class(TForm)
AddTenantButton: TButton;
procedure FormCreate(Sender: TObject);
procedure AddTenantButtonClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
TTenantRecord = record
FirstName : string[20];
LastName : string[20];
end;
var
MainForm: TMainForm;
Tenant : TTenantRecord;
TenantFile : file of TTenantRecord;
implementation
uses AddTenantUnit;
{$R *.dfm}
procedure TMainForm.AddTenantButtonClick(Sender: TObject);
begin
AddTenantForm.ShowModal;
end;
procedure TMainForm.FormCreate(Sender: TObject);
begin
assignfile (TenantFile, 'Tenant.dat');
if not fileexists ('Tenant.dat')
then
begin
rewrite (TenantFile);
closefile (TenantFile)
end
{endif};
end;
end.
unit AddTenantUnit;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, MainUnit, StdCtrls;
type
TAddTenantForm = class(TForm)
MainFormButton: TButton;
FirstNameLabel: TLabel;
FirstNameEdit: TEdit;
LastNameLabel: TLabel;
LastNameEdit: TEdit;
SaveButton: TButton;
ClearButton: TButton;
procedure SaveButtonClick(Sender: TObject);
procedure LastNameEditChange(Sender: TObject);
procedure ClearButtonClick(Sender: TObject);
procedure FirstNameEditChange(Sender: TObject);
procedure MainFormButtonClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
AddTenantForm: TAddTenantForm;
implementation
{$R *.dfm}
procedure TAddTenantForm.MainFormButtonClick(Sender: TObject);
begin
AddTenantForm.Close;
end;
procedure TAddTenantForm.FirstNameEditChange(Sender: TObject);
begin
Tenant.FirstName := FirstNameEdit.Text;
end;
procedure TAddTenantForm.ClearButtonClick(Sender: TObject);
begin
FirstNameEdit.Clear;
LastNameEdit.Clear;
end;
procedure TAddTenantForm.LastNameEditChange(Sender: TObject);
begin
Tenant.LastName := LastNameEdit.Text;
end;
// This is where the problem lies when I run this piece of
// code. This represents the Save button being clicked.
procedure TAddTenantForm.SaveButtonClick(Sender: TObject);
begin
assignfile (TenantFile, 'Tenant.dat');
write(TenantFile, Tenant);
closefile (TenantFile);
end;
end.
You are trying to write data into not opened file.
procedure TAddTenantForm.SaveButtonClick(Sender: TObject);
begin
assignfile (TenantFile, 'Tenant.dat');
// Rewrite(TenantFile) or Reset(TenantFile) missed here
write(TenantFile, Tenant);
closefile (TenantFile);
end;

How To Use RxChar ComPort in another form

I have problem with delphi code... I have code:
MAIN FORM
unit MainForm;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, CPort, Menus, ComObj, StdCtrls;
type
TMainForm = class(TForm)
MainMenu1: TMainMenu;
Berkas1: TMenuItem;
Alat1: TMenuItem;
erminal1: TMenuItem;
ComPort1: TComPort;
Button1: TButton;
Memo1: TMemo;
Button2: TButton;
procedure erminal1Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure ComPort1RxChar(Sender: TObject; Count: Integer);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
MainForm: TMainForm;
implementation
uses
ChildForm;
{$R *.dfm}
procedure TMainForm.erminal1Click(Sender: TObject);
var
ChildForm: TChildForm;
begin
ChildForm := TChildForm.Create(Application);
ChildForm.Show;
end;
procedure TMainForm.Button1Click(Sender: TObject);
begin
ComPort1.ShowSetupDialog;
end;
procedure TMainForm.ComPort1RxChar(Sender: TObject; Count: Integer);
var
ComPort: TComPort;
data: string;
begin
inherited;
ComPort := TComPort.Create(Self);
ComPort1.ReadStr(data, 5);
ChildForm.Memo1.Text := ChildForm.Memo1.Text+''+data+'';
end;
end.
CHILD FORM:
unit ChildForm;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ComObj;
type
TChildForm = class(TForm)
Memo1: TMemo;
Button1: TButton;
Button2: TButton;
Button3: TButton;
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
ChildForm: TChildForm;
implementation
uses
MainForm;
{$R *.dfm}
procedure TChildForm.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Action := caFree;
end;
procedure TChildForm.Button1Click(Sender: TObject);
begin
MainForm.ComPort1.Open;
end;
end.
I want to show data input from my device to memo in child form. I put the comport component in main form. But when I run the program, it says:
Project Data.exe raised exception class EAccessViolation with message 'Access violation at address 00466051 in module 'Data.exe'. Read of address 000002F8'. Process stopped. Use Step or Run to continue.
How can i solve the problem?
There are many problems with your code as mentioned in the comments.
To make a better implementation of your parent/child form interaction with the comport component,
do as follows:
Create a TDataModule (ex: DataModule1), put the comport component there.
Now you can access the comport component from the main form and the child form.
Add a private method to your child form:
procedure TChildForm.ComPort1RxChar(Sender: TObject; Count: Integer);
var
data: string;
begin
DataModule1.ComPort1.ReadStr(data, 5);
Self.Memo1.Text := Self.Memo1.Text+''+data+'';
end;
When you open the comport in the child form, set the comport OnRxChar event to your TChildForm.ComPort1RxChar method.
In the TChildForm.OnClose event, set the comport OnRxChar event to nil and close the comport.

Resources