Access violation in delphi-7 - delphi

OK i'm writing an educational program that uses different forms. This is the first time i'm coding with multiple form as i'm still a novice programmer.
when my "sign in" button is clicked it opens the new form but then displays an access violation code.
unit SignInNew_u;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls, xpman;
type
TSignInNew = class(TForm)
Panel1: TPanel;
Label2: TLabel;
Label3: TLabel;
Label1: TLabel;
Label4: TLabel;
edtName: TEdit;
edtSurname: TEdit;
btnSignIn: TButton;
help: TButton;
procedure btnSignInClick(Sender: TObject);
procedure helpClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
SignInNew: TSignInNew;
implementation
uses HelpNew_u, ElementsNew;
{$R *.dfm}
procedure TSignInNew.btnSignInClick(Sender: TObject);
var
sName,sSurname,text:string;
User:TextFile;
begin
ElementsNew.TMain.Create(self);
ElementsNew.Main.Show;
Main.WindowState:= wsMaximized;
end;
procedure TSignInNew.helpClick(Sender: TObject);
begin
HelpNew := THelpNew.Create(self);
HelpNew.Show;
HelpNew.Width:=281;
HelpNew.Height:=481;
end;
end.
This is how it looks
any help would be appreciated.

Looking your code, you are instantiating a class (TMain)
ElementsNew.TMain.Create(self);
but never assigning it to a variable. You are using a nil var (Main)
ElementsNew.Main.Show;
Main.WindowState:= wsMaximized;
To solve this:
Main := ElementsNew.TMain.Create(self);
Main.Show;

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?

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.

Passing the variable to another Form

I got Form1 with some variables and I want to pass it to another Form3 where I'll use it. So I have two questions.
How can I get access to the variable in another form? I suppose it will
be similar to
var newIdList:= Form1.idList
When var idList get value in
procedure TForm1.Button1Click(Sender: TObject);begin
idList:=strtoint(edit1.text);
end
and I show new form in another can I still get value in idList?
procedure TForm1.Button2Click(Sender: TObject);
begin
form1.hide;
form3.show;
end
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ComCtrls;
type
TForm1 = class(TForm)
PageControl1: TPageControl;
TabSheet1: TTabSheet;
TabSheet2: TTabSheet;
TabSheet3: TTabSheet;
Label5: TLabel;
Edit3: TEdit;
Edit2: TEdit;
Button3: TButton;
Edit4: TEdit;
Button2: TButton;
Button1: TButton;
Edit1: TEdit;
Label1: TLabel;
Label3: TLabel;
Label2: TLabel;
Edit5: TEdit;
Label7: TLabel;
Label6: TLabel;
Button4: TButton;
ListBox1: TListBox;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Edit4Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Edit1Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
idList,imieList,nazwiskoList,adresList: TStringList;
end;
var
Form1: TForm1;
plik:TStringList;
tempPlik:TextFile;
st:string;
linia_klient,linia_video:array[0..20] of string;
id:integer;
implementation
uses Unit3;
{$R *.dfm}
.
.
.
procedure TForm1.FormCreate(Sender: TObject);
var i:integer;
begin
Edit1.Text:='Witaj, Podaj ID klienta';
Label1.Caption:='ID Klienta';
idList:=TStringList.Create;
imieList:=TStringList.Create;
nazwiskoList:=TStringList.Create;
adresList:=TStringList.Create;
if (FileExists('idList.txt')=true) then idList.LoadFromFile('idList.txt') else idList.SaveToFile('idList.txt');
if (FileExists('imieList.txt')=true) then imieList.LoadFromFile('imieList.txt') else imieList.SaveToFile('imieList.txt');
if (FileExists('nazwiskoList.txt')=true) then nazwiskoList.LoadFromFile('nazwiskoList.txt') else nazwiskoList.SaveToFile('nazwiskoList.txt');
if (FileExists('adresList.txt')=true) then adresList.LoadFromFile('adresList.txt') else adresList.SaveToFile('adresList.txt');
AssignFile(tempPlik,'video.txt');
Reset(tempPlik);
i:=0;
While Not Eof(tempPlik) do
begin
Readln(tempPlik,linia_video[i]);
inc(i);
end;
CloseFile(tempPlik);
end;
procedure TForm1.Button4Click(Sender: TObject);
begin
//Form1.Hide;
Form3.Show;
end;
end.
unit Unit3;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm3 = class(TForm)
Button1: TButton;
Edit1: TEdit;
Edit2: TEdit;
Edit3: TEdit;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
procedure Button1Click(Sender: TObject);
procedure FormShow(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form3: TForm3;
implementation
uses Unit1;
{$R *.dfm}
procedure TForm3.Button1Click(Sender: TObject);
begin
Form3.Hide;
//Form1.Show;
end;
procedure TForm3.FormShow(Sender: TObject);
begin
Label4.Caption:= intToStr(idList.Count);
end;
end.
(I will assume that each form resides in its own unit.) First, you have to make sure that idList is accessible to other units. For example,
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs;
type
TForm1 = class(TForm)
private
{ Private declarations }
idList: integer;
public
{ Public declarations }
end;
will not do, but
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs;
type
TForm1 = class(TForm)
private
{ Private declarations }
public
{ Public declarations }
idList: integer;
end;
is OK. In such case, all you need to do in Unit2 is to add Unit1 to its 'uses list' (press Alt+F11, or use File/'Use Unit...', while in Unit2 or while editing Form2). Then you can use Form1.idList to access the variable anywhere inside Unit2. (Form1 is the global instance variable of TForm1 in Unit1).
For example,
unit Unit2;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm2 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form2: TForm2;
implementation
uses Unit1; // <-- Add manually, or press Alt+F11 (or use File/'Use Unit...')
{$R *.dfm}
procedure TForm2.Button1Click(Sender: TObject);
begin
ShowMessage(IntToStr(Form1.idList));
end;
end.
'How can I get access to variable in another form?' - yes, as long as the variable is a public or published member and you have acess to the instance variable, you can access it in the same way as any other class instance variable. Usually, this means adding the unit containing the 'Form1' class to the uses clause of the unit where access is desired.
'and I show new form in another can I still get value in idList?' - sure, as long as the form exists, you have access to the form instance variable and 'idList' is public or published.

How to Send to back for a control at runtime?

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.

Resources