Lets say I have a Spinedit on my Form.
The current Value of the Spinedit is e.g 5
When the user clicks on the SpinButton the next Value could be
4 or 6.
In the onChange event
I can get the new Value
4 or 6
but I need to know also the old Value
5
How can I get the previous value 5 in Delphi?
I need to know the old and the new Value
Store the previous value in a variable at the end of the OnChange event and at startup as below.
unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.Samples.Spin;
type
TForm1 = class(TForm)
SpinEdit1: TSpinEdit;
procedure SpinEdit1Change(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
FSpinPrev : Integer;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
FSpinPrev := SpinEdit1.Value;
end;
procedure TForm1.SpinEdit1Change(Sender: TObject);
begin
if SpinEdit1.Value > FSpinPrev then Caption := 'Increasing'
else Caption := 'Decreasing';
FSpinPrev := SpinEdit1.Value;
end;
end.
Related
Need to access some components right after program start but I have found that doing so from form's onCreate event is bad because at the moment they may still be unavailable (access violation occurs). Can not find onCreate event in any component. Am I missing something?
Here is code. Empty form with ValueListEditor.
unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.Grids, Vcl.ValEdit;
type
TForm1 = class(TForm)
ValueListEditor1: TValueListEditor;
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
procedure: Load;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
Load;
end;
procedure Load;
begin
(Application.MainForm.FindComponent('ValueListEditor1') as TValueListEditor)
.Strings.LoadFromFile('c:\li');
end;
end.
The problem is not because your component hasn't been created yet, because it has been. The real problem is because the Application.MainForm property hasn't been assigned yet when your main Form's OnCreate event is fired, so you are calling FindComponent() on a nil Form pointer.
Since Load() is merely accessing a member of TForm1 then Load() should also be a member of TForm1 as well, and then you can call it, and thus access your component, via the implicit Self pointer, which is valid during the Form's OnCreate event, eg:
unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.Grids, Vcl.ValEdit;
type
TForm1 = class(TForm)
ValueListEditor1: TValueListEditor;
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
procedure LoadValues;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
LoadValues;
end;
procedure TForm1.LoadValues;
begin
ValueListEditor1.Strings.LoadFromFile('c:\li');
end;
end.
If, for whatever reason, Load() must be a standalone procedure, then at least have it use your global Form1 variable, which the call to Application.CreateForm() will assign before the main Form's OnCreate event is fired, eg:
unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.Grids, Vcl.ValEdit;
type
TForm1 = class(TForm)
ValueListEditor1: TValueListEditor;
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
procedure LoadValues;
public
{ Public declarations }
end;
var
Form1: TForm1;
procedure Load;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
LoadValues;
end;
procedure TForm1.LoadValues;
begin
ValueListEditor1.Strings.LoadFromFile('c:\li');
end;
procedure Load;
begin
if Form1 <> nil then
Form1.LoadValues;
end;
end.
Alternatively, you can fallback to looking for the Form1 object in the Screen.Forms[] array, eg:
unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.Grids, Vcl.ValEdit;
type
TForm1 = class(TForm)
ValueListEditor1: TValueListEditor;
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
procedure LoadValues;
public
{ Public declarations }
end;
var
Form1: TForm1;
procedure Load;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
LoadValues;
end;
procedure TForm1.LoadValues;
begin
ValueListEditor1.Strings.LoadFromFile('c:\li');
end;
procedure Load;
var
I: Integer;
Frm: TForm;
begin
for I := 0 to Screen.FormCount-1 do
begin
Frm := Screen.Forms[I];
if Frm is TForm1 then
begin
TForm1(Frm).LoadValues;
Exit;
end;
end;
end;
end.
Use the form's onShow() -event.
But be aware, that the onShow() -event is called every time the form is shown, not only the first time.
In your form, you should override the DoShow method and insert your code there. Since DoShow is called everytime the form changes from invisible to visible, you should also add a boolean variable in your form and check in the DoShow override, if it is false. In that case, it is the first time DoShow is called. Then set the variable to true and do whatever you need to do the first time.
Please note that within DoShow the form is not visible yet. If you need to do something once the form is made visible, you can post a custom message from DoShow and put your code in the corresponding message handler. At the time it is executed, the form just became visible.
const
WM_APP_STARTUP = WM_USER + 1;
type
TForm1 = class(TForm)
protected
FInitialized : Boolean;
procedure DoShow; override;
procedure WMAppStartup(var Msg: TMessage); message WM_APP_STARTUP;
end;
implementation
procedure TForm1.DoShow;
begin
// Form is NOT visible but will soon be
if not FInitialized then begin
FInitialized := TRUE;
// Insert your code here
PostMessage(Handle, WM_APP_STARTUP, 0, 0);
end;
inherited DoShow;
end;
procedure TForm1.WMAppStartup(var Msg: TMessage);
begin
// The form is now visible
// Insert your code here
end;
Delphi MDIChild windowState problem
I have a delphi project with a MDI and childs. When I created a new child form, this occupies perfctly a reserved space on main form (child wsMaximized).
But when a create another child form (without closed first child), the first child "lost a wsMaximized state".
Resume: I need the child forms maximized every time, but when second child is opened, the "windowstate" another childs is changed.
First child:
procedure TfrmPrincipal.PosicionarForm(AForm: TForm);
var
Rect: TRect;
begin
GetWindowRect(Application.MainForm.ClientHandle, Rect);
InflateRect(Rect, -2 * GetSystemMetrics(SM_CXBORDER),
-2 * GetSystemMetrics(SM_CYBORDER));
OffsetRect(Rect, -Rect.Left, -Rect.Top);
AForm.BoundsRect := Rect;
end;
procedure TfrmPrincipal.actCadastroFornecedorExecute(Sender: TObject);
begin
frmCadastroFornecedor := TfrmCadastroFornecedor.Create(Application);
PosicionarForm(frmCadastroFornecedor);
frmCadastroFornecedor.Show;
svSub.Visible := False;
SV.Opened := False;
end;
Secound child:
procedure TfrmPrincipal.actCadastroProdutosExecute(Sender: TObject);
begin
frmCadastroProduto := TfrmCadastroProduto.Create(Application);
PosicionarForm(frmCadastroProduto);
frmCadastroProduto.Show;
svSub.Visible := False;
SV.Opened := False;
end;
EDIT:
I created a new project, have 3 forms. The code is very simple, but this behavior continues.
Main FORM:
unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.Menus;
type
TForm1 = class(TForm)
MainMenu1: TMainMenu;
screen11: TMenuItem;
screen21: TMenuItem;
procedure screen11Click(Sender: TObject);
procedure screen21Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
uses
unit2, unit3;
{$R *.dfm}
procedure TForm1.screen11Click(Sender: TObject);
begin
form2 := tform2.Create(Application);
form2.Show;
end;
procedure TForm1.screen21Click(Sender: TObject);
begin
form3 := tform3.Create(Application);
form3.Show;
end;
end.
Child 1:
unit Unit2;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs;
type
TForm2 = class(TForm)
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form2: TForm2;
implementation
{$R *.dfm}
procedure TForm2.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Action := caFree;
end;
end.
Child 2:
unit Unit3;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs;
type
TForm3 = class(TForm)
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form3: TForm3;
implementation
{$R *.dfm}
procedure TForm3.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Action := caFree;
end;
end.
After some trial and error (once again an example of how important a complete, minimal, reproducible, example is) I was able to reproduce the error you ask about. If you had included the the .dfm files, the actual problem had been clear much sooner.
Now, to the problem that the WindowState of the forms change from wsMaximized to wsNormal after deletion of one of the childforms.
This happens when you have removed biMaximise from BorderIcons for forms Form2 and Form3
Cure: Let BorderIcons.biMaximise be selected.
I also suggest you remove PosicionarForm(AForm: TForm); from the main form. It doesn't appear to do anything useful.
I'm using 2 Units in Delphi 2010: Unit1 has a Form with a TEdit and a Button, this Button will call a procedure from Unit2 (Unit2 doesn't have a form). That procedure will change the 'caption' property of the TEdit of Unit1, I tried to put Unit2 in Unit1 "Uses" to access the procedure, and put Unit1 in the "Uses" of Unit2 to to access the TEdit in Unit1, but this relation is cyclic.
I'm not sure what can I do to solve this, any suggestion?
There are many ways to do this, and which one is most appropriate depends crucially on the precise circumstances, so technically this question is too broad for Stack Overflow, I suspect.
Method 1
Still, let me just show you one very simple approach. This might not be a good approach, but it is close to what it seems you are trying to do.
Create a new VCL application.
Drop a TButton and a TEdit control on the main form (TForm1 in Unit1).
Create a new unit, Unit2:
unit Unit2;
interface
procedure SetCaption;
implementation
uses
Unit1;
procedure SetCaption;
begin
Unit1.Form1.Edit1.Text := 'Hello, World!';
end;
end.
On the main form, use this event handler for the button's OnClick event:
procedure TForm1.Button1Click(Sender: TObject);
begin
Unit2.SetCaption;
end;
after you have added Unit2 to the implementation section's uses clause. In full,
unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
uses
Unit2;
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
Unit2.SetCaption;
end;
end.
Notice how the units may use each other if referenced only in the implementation sections. Also notice we use the global Form1 variable in Unit1 to refer to the automatically created instance of TForm1. In many (most) cases, you don't want to use automatically created forms, but that is a different story (I could write a 100-page Delphi textbook here!).
Method 2
Also, let me reiterate the fact that there are many other ways to accomplish this result. For instance, this is arguably a better approach:
unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
uses
Unit2;
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
Unit2.SetCaption(Edit1);
end;
end.
unit Unit2;
interface
uses
StdCtrls;
procedure SetCaption(AEdit: TCustomEdit);
implementation
procedure SetCaption(AEdit: TCustomEdit);
begin
AEdit.Text := 'Hello, World!';
end;
end.
Method 3
Arguably even better:
unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
uses
Unit2;
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
Edit1.Text := Unit2.GetText;
end;
end.
unit Unit2;
interface
function GetText: string;
implementation
function GetText: string;
begin
Result := 'Hello, World!';
end;
end.
Update:
This question escalated to a new/related question which was fortunately solved by #RemyLebeau here.
So, instead of reading below, you should go directly to Major flaw - Radio buttons are not correctly set while the form is invisible
Thanks Remy
I have two form. When I click a radiobtn, I want to hide one form and show the second one.
Hide Form1 and show Form2:
procedure TForm1.RadioButton2Click(Sender: TObject);
begin
Form2.Visible:= TRUE;
Form1.Visible:= FALSE;
end;
In Form2 I press a button to 'return' to Form1:
procedure TForm2.Button1Click(Sender: TObject);
begin
Form1.RadioButton1.Checked:= TRUE;
Form1.Visible:= TRUE; <--- this will 'magically' put the RadioButton1 back to false
end;
However, I get this error when I try to make Form1 visible:
Project Tester.exe raised exception class EInvalidOperation with
message 'Cannot change Visible in OnShow or OnHide'
Putting a breakpoint in RadioButton2Click I find out that RadioButton1 was magically rechecked during Form1.Visible:= TRUE (during TCustomForm.SetVisible more exactly).
Why is the RadioButton2 'magically' checked during SetVisible?
unit Unit1;
INTERFACE
USES
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.Menus, System.Actions, Vcl.ActnList, Vcl.StdCtrls;
TYPE
TForm1 = class(TForm)
GroupBox1: TGroupBox;
RadioButton1: TRadioButton;
RadioButton2: TRadioButton;
procedure RadioButton2Click(Sender: TObject);
procedure RadioButton1Click(Sender: TObject);
private
public
end;
VAR
Form1: TForm1;
IMPLEMENTATION {$R *.dfm}
USES Unit2;
procedure TForm1.RadioButton1Click(Sender: TObject);
begin
Caption:= '1';
end;
procedure TForm1.RadioButton2Click(Sender: TObject);
begin
Caption:= '2';
Form2.Visible:= TRUE;
Form1.Visible:= FALSE;
end;
end.
-
unit Unit2;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;
type
TForm2 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
public
end;
VAR
Form2: TForm2;
IMPLEMENTATION {$R *.dfm}
USES Unit1;
procedure TForm2.Button1Click(Sender: TObject);
begin
Form1.RadioButton1.Checked:= TRUE;
Form1.Visible:= TRUE;
end;
end.
The workaround (not the final fix) is to do the changes to the GUI (form1) AFTER you make it visible!
Update!
The bug is related to the TabOrder property!
Details
Direct references from one form to fields of another is bad design and I highly advice you to change this. But anyway, I made two forms: first with two radiobutton, second with a button. Handlers:
{ For both radiobuttons }
procedure TForm1.RadioButtonClick(Sender: TObject);
begin
Form1.Visible := RadioButton1.Checked;
Form2.Visible := RadioButton2.Checked;
end;
{ For button }
procedure TForm2.Button1Click(Sender: TObject);
begin
Form1.RadioButton1.Checked := true;
end;
It works fine, without any remark. Delphi 10.1 Berlin.
Anyone know why I am getting an access violation with the following:
unit TestForm;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Generics.Collections, Vcl.Grids,
Vcl.ValEdit;
type
TClientThing = class
private
iCDic: TDictionary<string, string>;
published
property Dic: TDictionary<string, string> read iCDic write iCDic;
end;
TForm1 = class(TForm)
vleHeader: TValueListEditor;
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
ClientThing: TClientThing;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
var
vCustomParamKey: string;
vCustomValueKey: string;
J: integer;
begin
with ClientThing do
begin
// Get the header params from the config and list edit...
for J := 0 to vleHeader.RowCount - 1 do
begin
vCustomParamKey := vleHeader.Cells[0, J];
vCustomValueKey := vleHeader.Cells[1, J];
Dic.Add(vCustomParamKey, vCustomValueKey);
end;
end;
end;
end.
The Access violation is at the Dic.Add line. The exception is:
I have been up all night and so have probably missed something. The TValueListEditor contents are (code editor view):
X-Application=g9V0rB9a3J5UF8
X-Authentication=kQNvuuimr0yMtEYZtXAZntTScPlvjecEAGtvbnNIU=
JSONRpc=2.0
The form never assigns a value to ClientThing, so it's still nil. Assign it a value, and then assign a value to iCDic. You should have been able to detect this problem when you stepped through the code with the debugger.