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.
Related
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?
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;
I have a project using TSynEdit. I found that when I call SynEdit1.Lines.LoadFromFile(), event OnChange doesn't be fired.
For instance:
unit Unit1;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, FileUtil, SynEdit, Forms, Controls, Graphics, Dialogs,
StdCtrls;
type
{ TForm1 }
TForm1 = class(TForm)
Button1: TButton;
SynEdit1: TSynEdit;
procedure Button1Click(Sender: TObject);
procedure SynEdit1Change(Sender: TObject);
end;
var
Form1: TForm1;
implementation
{$R *.lfm}
{ TForm1 }
procedure TForm1.Button1Click(Sender: TObject);
begin
SynEdit1.Lines.LoadFromFile('unit1.pas');
end;
procedure TForm1.SynEdit1Change(Sender: TObject);
begin
Caption:=Caption + '!';
end;
end.
In above example, I created a form which have a button and a SynEdit.
How can I make event OnChange be fired when SynEdit1.Lines.LoadFromFile() was called?
If you try to assign all the events handler of a synedit, the best you can get is a notification when the editor is cleared. For example if you test the following code the form caption will be set to 'cleared' after the button click event:
uses
Classes, SysUtils, FileUtil, SynEdit, Forms, Controls, Graphics, Dialogs,
StdCtrls, LazSynEditText;
type
TSynEditEx = class helper for TSynEdit
function getTextBuffer: TSynEditStrings;
end;
TForm1 = class(TForm)
Button1: TButton;
SynEdit1: TSynEdit;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
procedure textCleared(sender: TObject);
procedure textHistoryModified(sender: TObject);
procedure textBuffChanged(sender: TObject);
procedure textLineChanged(sender: TObject);
procedure textEditAction(sender: TObject);
procedure textModdChanged(sender: TObject);
end;
var
Form1: TForm1;
implementation
{$R *.lfm}
function TSynEditEx.getTextBuffer: TSynEditStrings;
begin
exit(TextBuffer);
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
SynEdit1.getTextBuffer.AddNotifyHandler(senrCleared, #textCleared);
SynEdit1.getTextBuffer.AddNotifyHandler(senrUndoRedoAdded, #textHistoryModified);
SynEdit1.getTextBuffer.AddNotifyHandler(senrTextBufferChanged, #textBuffChanged);
SynEdit1.getTextBuffer.AddNotifyHandler(senrLineChange, #textLineChanged);
SynEdit1.getTextBuffer.AddNotifyHandler(senrModifiedChanged, #textModdChanged);
end;
procedure TForm1.textModdChanged(sender: TObject);
begin
Caption := Caption + ' ModdChanged';
end;
procedure TForm1.textEditAction(sender: TObject);
begin
Caption := Caption + ' EditAction';
end;
procedure TForm1.textLineChanged(sender: TObject);
begin
Caption := Caption + ' LineChanged';
end;
procedure TForm1.textBuffChanged(sender: TObject);
begin
Caption := Caption + ' BuffChanged';
end;
procedure TForm1.textCleared(sender: TObject);
begin
Caption := Caption + ' Cleared';
end;
procedure TForm1.textHistoryModified(sender: TObject);
begin
Caption := Caption + ' HistoryModified';
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
SynEdit1.Lines.LoadFromFile('unit1.pas');
end;
So the only solution is to subclass TSynEdit and introduce a custom LoadFromFile method and then trigger the onChange event inside. AFAIK, based on a short investigation, this is the only way, a bit like this:
uses
Classes, SysUtils, FileUtil, SynEdit, Forms, Controls, Graphics, Dialogs,
StdCtrls, LazSynEditText;
type
TSynEditEx = class helper for TSynEdit
procedure LoadFromFile(const aFilename: string);
end;
TForm1 = class(TForm)
Button1: TButton;
SynEdit1: TSynEdit;
procedure Button1Click(Sender: TObject);
procedure SynEdit1Change(Sender: TObject);
private
end;
var
Form1: TForm1;
implementation
{$R *.lfm}
procedure TSynEditEx.LoadFromFile(const aFilename: string);
begin
Lines.LoadFromFile('unit1.pas');
if assigned(onChange) then
onChange(self);
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
SynEdit1.LoadFromFile('unit1.pas');
end;
procedure TForm1.SynEdit1Change(Sender: TObject);
begin
caption := caption + '!';
end;
In the real world you wouldn't use a class helper but you'd rather subclass, but I think you should get the idea.
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.
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.