I did:
procedure TForm1.SpeedButton1Click(Sender: TObject);
begin
DataTable.qOrders.Next;
end;
It works, but the problem is when I click the button to reach the last record, the button is not disabled, like in a TDBNavigator.
How did I make the TSpeedButton disable and enable automatically like the TDBNavigator does?
Drop a TActionList onto your form and add the standard dataset actions to it. Connect these actions to your dataset and your speedbuttons to the appropriate actions. These standard actions will handle the enable state according to the current dataset state.
Here is a simple solution, that works perfectly for me.
I have a form (frmMain), dataset (dsWork), datasource (srcWork), grid and two speedbuttons (btnNext and btnPrior). The important part is in "OnDataChange" event of TDataSource. Here is the source code:
unit MainForm;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Grids, DBGrids, DB, DBTables, StdCtrls, ExtCtrls;
type
TfrmMain = class(TForm)
btnNext: TButton;
srcWork: TDataSource;
dsWork: TTable;
btnPrior: TButton;
grdWork: TDBGrid;
procedure btnNextClick(Sender: TObject);
procedure btnPriorClick(Sender: TObject);
procedure srcWorkDataChange(Sender: TObject; Field: TField);
private
{ Private declarations }
public
{ Public declarations }
end;
var
frmMain: TfrmMain;
implementation
{$R *.dfm}
procedure TfrmMain.btnNextClick(Sender: TObject);
begin
if not dsWork.Eof then dsWork.Next;
end;
procedure TfrmMain.btnPriorClick(Sender: TObject);
begin
if not dsWork.Bof then dsWork.Prior;
end;
procedure TfrmMain.srcWorkDataChange(Sender: TObject; Field: TField);
begin
btnNext.Enabled := not dsWork.Eof;
btnPrior.Enabled := not dsWork.Bof;
end;
end.
Related
Look at the image below:
As you can see I cannot send Buttons to back. This only works for labels.
So how can I send TImage to front with its transparency.
By the way I've read This related question but didn't help me. Because you cannot even click on a button after running Andreas Rejbrand's code. Not only buttons, everything (like the scrollbar in this image)
Edit:
I don't want to make the button reachable after I send that back to the image. Just want to bring TImage to front of everything.
Thanks.
One way which could you near the goal would be to use interposer classes for the TWincontrols and paint the image moved on them, after they have been painted already, using a TControlCanvas and "hooking" WM_PAINT.
The code is showing a raw draft using a semitransparent PNG image and may be enhanced.
unit Unit2;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Buttons, dxGDIPlusClasses, ExtCtrls;
type
TButton=Class (StdCtrls.TButton)
Procedure WMPaint(var MSG:TMessage);Message WM_Paint;
End;
TEdit=Class (StdCtrls.TEdit)
Procedure WMPaint(var MSG:TMessage);Message WM_Paint;
End;
TForm2 = class(TForm)
Image1: TImage;
SpeedButton1: TSpeedButton;
Button1: TButton;
Edit1: TEdit;
Edit2: TEdit;
private
{ Private-Deklarationen }
public
{ Public-Deklarationen }
end;
var
Form2: TForm2;
implementation
{$R *.dfm}
{ TButton }
procedure TButton.WMPaint(var MSG: TMessage);
var
cc:TControlCanvas;
begin
inherited;
CC:=TControlCanvas.Create;
CC.Control := TControl(Self);
CC.Draw(-Left,-Top,Form2.Image1.Picture.Graphic);
CC.Free;
end;
procedure TEdit.WMPaint(var MSG: TMessage);
var
cc:TControlCanvas;
begin
inherited;
CC:=TControlCanvas.Create;
CC.Control := TControl(Self);
CC.Draw(-Left,-Top,Form2.Image1.Picture.Graphic);
CC.Free;
end;
end.
Another (better) place to "hook" would be overriding PaintWindow
unit Unit2;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Buttons, dxGDIPlusClasses, ExtCtrls;
type
TButton=Class (StdCtrls.TButton)
procedure PaintWindow(DC: HDC);override;
End;
TEdit=Class (StdCtrls.TEdit)
procedure PaintWindow(DC: HDC);override;
End;
TForm2 = class(TForm)
Image1: TImage;
SpeedButton1: TSpeedButton;
Button1: TButton;
Edit1: TEdit;
Edit2: TEdit;
private
{ Private-Deklarationen }
public
{ Public-Deklarationen }
end;
var
Form2: TForm2;
implementation
{$R *.dfm}
{ TButton }
procedure TButton.PaintWindow(DC: HDC);
var
cc:TCanvas;
begin
inherited;
CC:=TCanvas.Create;
CC.Handle := DC;
CC.Draw(-Left,-Top,Form2.Image1.Picture.Graphic);
CC.Free;
end;
procedure TEdit.PaintWindow(DC: HDC);
var
cc:TCanvas;
begin
inherited;
CC:=TCanvas.Create;
CC.Handle := DC;
CC.Draw(-Left,-Top,Form2.Image1.Picture.Graphic);
CC.Free;
end;
end.
You do not want the Image brought to front (which by the way is impossible over a windowed control), because you want the button also reachable.
Although your question is contradicting itself, and it is not at all clear what exactly you want to achieve, I think you mean to have a transparent button over an image.
If so, then use a TSpeedButton, and set its Transparent and Flat property to True.
Here an example with the three button states: normal, hovered, pressed:
You can use the solution you linked in your question. For the controls that you want the clicks go through - disable them. Since you're putting the image on a panel, disabling both the panel and the image will let the button to be clicked.
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.
Here I have reproduced situation I have encounter couple of times. I have two forms. Form1 and Form2. Form1 has one edit field and OnKeyup event hooked up. Form2 has only one button and OnClick hooked up. When in Form1 user press VK_Retrun in field of type TEdit, Form2.Show is executed. Form2 shows up with focus on the button. The event OnClick is hooked up with the code "Close" inside. If user hit VK_RETURN key on the keyboard, Form2 closes as expected.. but here come the problem, Form2 got fired straight up again. It seems as when Form1 get focus the key is still in "a queue" and the edit field will proceed with VK_RETURN.
Here is a full listings of this situation:
unit UTestButton;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Edit1: TEdit;
procedure Edit1KeyUp(Sender: TObject; var Key: Word;
Shift: TShiftState);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
uses UTestButton2;
{$R *.dfm}
procedure TForm1.Edit1KeyUp(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
if (Key = VK_Return) THEN
Form2.Show;
end;
end.
This is the second unit.
unit UTestButton2;
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
{$R *.dfm}
procedure TForm2.Button1Click(Sender: TObject);
begin
Close;
end;
end.
In praxes I use sometimes simple form with labels or informations but one or two buttons and focus on button. User can hit key on keyboard as expected. If he use the vk_return and the underlying control use the key, I have to do some workaround to clear keys of buffer as with
PeekMessage(Mgs, 0, WM_CHAR, WM_CHAR, PM_REMOVE);
That´s not totally satisfying for me. Has anyone solution for this situation?
Simply handle the OnKeyDown event instead of OnKeyUp for your initial edit box:
procedure TForm1.Edit1KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
if (Key = VK_RETURN) then
Form2.Show;
end;
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.
When a user selects a value in my TDateTimePicker I want to override the to-be-set value to the start of the week that goes with the selected value.
I tried setting it in the OnChange event, but then the originally selected value will be set right after I finished the event.
How would I go about this?
use the "ONCloseUp" event - this sample works for me (Delphi 7, WinXP)
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ComCtrls, DateUtils, StdCtrls;
type
TForm1 = class(TForm)
dtp1: TDateTimePicker;
btn1: TButton;
edt1: TEdit;
procedure btn1Click(Sender: TObject);
procedure dtp1CloseUp(Sender: TObject);
private
{ Private declarations }
procedure SetDayToMonday();
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.SetDayToMonday;
begin
dtp1.DateTime := dtp1.DateTime - DayOfTheWeek(dtp1.DateTime) + 1;
end;
procedure TForm1.dtp1CloseUp(Sender: TObject);
begin
SetDayToMonday;
end;
end.
--reinhard :-)
Use the onUserInput event!
I would post a message to the form, define a message (WM_USER+1000+X), post it, and handle it. If you don't "pend" it like this, you could also do a PendingDateTimeTimer:TTimer that does validation slightly later (say 10msec) after the OnChange event sets PendingDateTimeTimer.Enabled := true.