📄 unittrain.pas
字号:
unit Unittrain;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Buttons, ExtCtrls, Grids;
type
Ttrainfr = class(TForm)
Panel2: TPanel;
panel1: TPanel;
BitBtn1: TBitBtn;
BitBtn2: TBitBtn;
BitBtn3: TBitBtn;
BitBtn4: TBitBtn;
BitBtn5: TBitBtn;
BitBtn6: TBitBtn;
BitBtn7: TBitBtn;
BitBtn8: TBitBtn;
Label1: TLabel;
Label2: TLabel;
Edit1: TEdit;
Edit2: TEdit;
StringGrid1: TStringGrid;
procedure BitBtn1MouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
procedure Panel2MouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
procedure FormCreate(Sender: TObject);
procedure StringGrid1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure BitBtn1Click(Sender: TObject);
procedure BitBtn2Click(Sender: TObject);
procedure BitBtn3Click(Sender: TObject);
procedure BitBtn4Click(Sender: TObject);
procedure BitBtn5Click(Sender: TObject);
procedure BitBtn6Click(Sender: TObject);
procedure BitBtn7Click(Sender: TObject);
procedure BitBtn8Click(Sender: TObject);
procedure EditClear;
procedure EditValue;
procedure FindNext;//使下一个编辑框获得焦点
Procedure FindPrior;//使上一个编辑框获得焦点
procedure Edit1KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);//为编辑框赋值
procedure Edit1Change(Sender: TObject);
Function IsNullEdit: Boolean;
private
{ Private declarations }
public
{ Public declarations }
end;
var
trainfr: Ttrainfr;
aa: Boolean = False ;//设置保存按钮是否可用
implementation
uses unitmodule;
{$R *.dfm}
procedure Ttrainfr.BitBtn1MouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
begin
Screen.Cursor := -21;
TBitBtn(Sender).Font.Color := clRed;
end;
procedure Ttrainfr.Panel2MouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
begin
Screen.Cursor := -2;
BitBtn1.Font.Color := clBlue;
BitBtn2.Font.Color := clBlue;
BitBtn3.Font.Color := clBlue;
BitBtn4.Font.Color := clBlue;
BitBtn5.Font.Color := clBlue;
BitBtn6.Font.Color := clBlue;
BitBtn7.Font.Color := clBlue;
BitBtn8.Font.Color := clBlue;
end;
procedure Ttrainfr.BitBtn3Click(Sender: TObject);
var
s,m: String;
i: integer;
begin
EditClear;
aa := true;
BitBtn5.Enabled := False;
BitBtn6.Enabled := False;
Edit1.setfocus;
if edit2.text<>'' then
aa:=false;
end;
procedure Ttrainfr.Edit1Change(Sender: TObject);
begin
if (Trim(Edit1.Text)<>'')and (Trim(Edit2.Text)<>'')and
(aa = True)then
BitBtn4.Enabled := True
else
BitBtn4.Enabled := False;
end;
procedure Ttrainfr.BitBtn1Click(Sender: TObject);
begin
if Not DataModule2.ADOQuery8.Bof then
begin
DataModule2.ADOQuery8.Prior;
EditValue;
end
else
BitBtn1.Enabled := False;
BitBtn2.Enabled := True;
BitBtn5.Enabled := True;
BitBtn6.Enabled := True;
BitBtn4.Enabled := False;
end;
procedure Ttrainfr.BitBtn2Click(Sender: TObject);
begin
if Not DataModule2.ADOQuery8.Eof then
begin
DataModule2.ADOQuery8.Next;
EditValue;
end
else
BitBtn2.Enabled := False;
BitBtn1.Enabled := True;
BitBtn5.Enabled := True;
BitBtn6.Enabled := True;
BitBtn4.Enabled := False;
end;
procedure Ttrainfr.BitBtn5Click(Sender: TObject);
begin
Try
if Application.MessageBox('确实要修改该条记录吗?','提示',MB_YESNO )= ID_Yes then
begin
if IsNullEdit = False then
begin
with DataModule2.ADOQuery8 do
begin
Edit;
BitBtn4.Enabled := False;
FieldByName('机车编号').Value := Edit1.Text;
FieldByName('机车信息').Value := Edit2.Text ;
Post;
Application.MessageBox('修改成功。','提示',0+64);
end;
end
else
begin
Application.MessageBox('数据项不能为空.','提示',64);
Exit;
end;
end
else
EditValue;
Except
Application.MessageBox('系统出错。','提示',0+64);
Close;
end;
end;
procedure Ttrainfr.BitBtn6Click(Sender: TObject);
begin
BitBtn4.Enabled := False;
Try
if Application.MessageBox('确实要删除该条记录吗?','提示',MB_YESNO )= ID_Yes then
begin
with DataModule2.ADOQuery8 do
begin
Close;
SQL.Clear;
SQL.Add('delete * from 机车信息表 where 机车编号 = :a');
Parameters.ParamByName('a').Value := Trim(Edit1.Text);
ExecSQL;
end;
Application.MessageBox('该条记录已经删除。','提示',0+64);
OnShow(Sender);
end;
Except
Application.MessageBox('系统出错。','提示',0+64);
Close;
end;
end;
procedure Ttrainfr.BitBtn7Click(Sender: TObject);
begin
Self.OnShow(Sender);
BitBtn4.Enabled := False;
BitBtn6.Enabled := True;
BitBtn5.Enabled := True;
BitBtn2.Enabled := True;
edit1.Text:='';
edit2.Text:='';
end;
procedure Ttrainfr.BitBtn4Click(Sender: TObject);
var
a:string;
j:integer;
begin
With DataModule2.ADOQuery8 do
begin
Close;
SQL.Clear;
SQL.Add('select * from 机车信息表 where 机车编号 = :a');
Parameters.ParamByName('a').Value := Trim(Edit1.Text);
Open;
end;
if DataModule2.ADOQuery8.RecordCount > 0 then
begin
Application.MessageBox('该信息已经存在。','提示',0+64);
Exit;
end;
Try
With DataModule2.ADOQuery8 do
begin
Close;
SQL.Clear;
SQL.Add('Insert into 登录表 values ( :a,:b)');
Parameters.ParamByName('a').Value := Trim(Edit1.Text);
Parameters.ParamByName('b').Value := Trim(Edit2.Text);
ExecSQL;
end;
Application.MessageBox('保存成功。','提示',0+64);
BitBtn1.Enabled := True;
BitBtn2.Enabled := True;
self.OnShow(Sender);
Except
Application.MessageBox('系统出错,请查询该车次是否存在。','提示',0+64);
Close;
end;
end;
procedure Ttrainfr.EditClear;
begin
Edit1.Clear;
Edit2.Clear;
end;
procedure Ttrainfr.EditValue;
var
a:string;
begin
if DataModule2.ADOQuery8.FieldByName('机车编号').Value <> null then
begin
Edit1.Text := DataModule2.ADOQuery8.FieldByName('机车编号').Value;
Edit2.Text := DataModule2.ADOQuery8.FieldByName('机车信息').Value;
// a:= DataModule2.ADOQuery8.FieldByName('').Value;
end
else
begin
Edit1.Clear;
Edit2.Clear;
end;
end;
procedure Ttrainfr.Edit1KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
if key = vk_return then
FindNext
else if key = vk_Up then
FindPrior;
end;
procedure Ttrainfr.FindNext;
begin
if FindNextControl(ActiveControl,True,False,False)is TEdit then
if TEdit(FindNextControl(ActiveControl,True,False,False)).Enabled = True then
TEdit(FindNextControl(ActiveControl,True,False,False)).SetFocus
else
begin
ActiveControl := FindNextControl(ActiveControl,True,False,False);
FindNext;
end;
end;
procedure Ttrainfr.FindPrior;
begin
if FindNextControl(ActiveControl,False,False,False)is TEdit then
if TEdit(FindNextControl(ActiveControl,False,False,False)).Enabled = True then
TEdit(FindNextControl(ActiveControl,False,False,False)).SetFocus
else
begin
ActiveControl := FindNextControl(ActiveControl,False,False,False);
FindPrior;
end;
end;
function Ttrainfr.IsNullEdit: Boolean;
var
Count: Integer;
begin
IsNullEdit := False;
For Count := 0 to Panel1.ControlCount-1 do
if Panel1.Controls[Count]is TEdit then
if Trim(TEdit(Panel1.Controls[Count]).Text)='' then
begin
IsNullEdit := True;
Break;
end;
end;
procedure Ttrainfr.bitbtn8click(Sender:TObject);
begin
inherited;
close();
end;
procedure Ttrainfr.FormCreate(Sender: TObject);
var
i:integer;
begin
with StringGrid1 do
begin
FixedCols:=0;//左边没有固定列
Cells[0,0]:='机车编号';//列标题
Cells[1,0]:='机车信息';
end;
with datamodule2.ADOQuery10 do
begin
close;
sql.Clear;
sql.Add('select * from 机车信息表' );
open;
end;
if datamodule2.ADOQuery10.FieldByName('机车编号').Value<>null then
begin
with datamodule2.ADOQuery10 do begin
Open;
First;
StringGrid1.ColCount:= FieldCount; //StringGrid1.ColCount:= FieldCount-1; 是按照数据库中的表的列数来创建的,程序中不需要那么多
StringGrid1.RowCount:= RecordCount+1;
for i:=0 to RecordCount-1 do
begin
StringGrid1.Cells[0,i+1]:= Fields[0].AsString;
StringGrid1.Cells[1,i+1]:= Fields[1].AsString;
Next;
end;
end;
end
else
begin
Application.MessageBox('该表数据为空,请向该表中插入数据。','提示',0+64);
BitBtn1.Enabled := False;
BitBtn2.Enabled := False;
BitBtn5.Enabled := False;
BitBtn6.Enabled := False;
BitBtn7.Enabled := False;
end;
end;
procedure Ttrainfr.StringGrid1MouseDown(Sender: TObject;
Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var
i,ii:integer;
condition:string;
begin
i:=stringgrid1.Row; //行号
ii:=stringgrid1.Col; //列号
condition:=stringgrid1.Cells[ii,i];
// if ii=0 then
begin
edit1.Text:=stringgrid1.Cells[0,i];
edit2.Text:=stringgrid1.Cells[1,i];
end
{if ii=0 then
begin
with datamodule2.ADOQuery10 do
begin
close;
sql.Clear;
sql.Add('select * from 表1 where 机车编号=:a' );
Parameters.ParamByName('a').Value :=condition;
open;
end;
if datamodule2.ADOQuery10.FieldByName('机车编号').Value<>null then
begin
Edit1.Text := DataModule2.ADOQuery10.FieldByName('机车编号').AsString;
Edit2.Text := DataModule2.ADOQuery10.FieldByName('机车信息').AsString;
end;
end
else
begin
with datamodule2.ADOQuery10 do
begin
close;
sql.Clear;
sql.Add('select * from 表1 where 机车信息=:a' );
Parameters.ParamByName('a').Value :=condition;
open;
end;
if datamodule2.ADOQuery10.FieldByName('机车信息').Value<>null then
begin
Edit1.Text := DataModule2.ADOQuery10.FieldByName('机车编号').AsString;
Edit2.Text := DataModule2.ADOQuery10.FieldByName('机车信息').AsString;
end;
end; }
end;
end.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -