【delphi开源代码栏目提醒】:网学会员在delphi开源代码频道为大家收集整理了FrmSetCommodity.pas提供大家参考,希望对大家有所帮助!
unit FrmSetCommodity;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ADODB, DB, Grids, DBGrids, StdCtrls, ExtCtrls, jpeg, DBCtrls;
type
TFormSetCommodity = class(TForm)
Label2: TLabel;
Label3: TLabel;
EditID: TEdit;
EditName: TEdit;
BtnAdd: TButton;
BtnModify: TButton;
BtnRemove: TButton;
BtnOK: TButton;
DataSourceCommodity: TDataSource;
DataSetCommodity: TADODataSet;
CmdSetCommodity: TADOCommand;
Label11: TLabel;
Label5: TLabel;
CBSort: TComboBox;
CBTrade: TComboBox;
Label4: TLabel;
EditImagePath: TEdit;
BtnBrowse: TButton;
DataGrid: TDBGrid;
Label1: TLabel;
Label6: TLabel;
Label7: TLabel;
Shape1: TShape;
Shape2: TShape;
ImageCommodity: TImage;
DataSetSortTrade: TADODataSet;
procedure BtnBrowseClick(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure BtnAddClick(Sender: TObject);
procedure BtnModifyClick(Sender: TObject);
procedure BtnRemoveClick(Sender: TObject);
procedure DataGridDblClick(Sender: TObject);
function GetIDByName(name: string; table: string) : string;
function GetNameByID(ID: string; table: string) : string;
private
{ Private declarations }
public
{ Public declarations }
end;
var
FormSetCommodity: TFormSetCommodity;
implementation
{$R *.dfm}
function TFormSetCommodity.GetIDByName(name: string; table: string) : string;
begin
DataSetSortTrade.Active := false;
DataSetSortTrade.CommandText :=
'select id from '
+ table
+ ' where name = '''
+ name
+ '''';
DataSetSortTrade.Active := true;
if not(DataSetSortTrade.IsEmpty) then
begin
GetIDByName := DataSetSortTrade.FieldByName('id').AsString;
end
else
begin
Application.MessageBox('无效的商品名称!', '错误', MB_OK);
end;
end;
function TFormSetCommodity.GetNameByID(ID: string; table: string) : string;
begin
DataSetSortTrade.Active := false;
DataSetSortTrade.CommandText :=
'select name from '
+ table
+ ' where id = '''
+ ID
+ '''';
DataSetSortTrade.Active := true;
if not(DataSetSortTrade.IsEmpty) then
begin
GetNameByID := DataSetSortTrade.FieldByName('name').AsString;
end
else
begin
Application.MessageBox('无效的商品名称!', '错误', MB_OK);
end;
end;
procedure TFormSetCommodity.BtnBrowseClick(Sender: TObject);
var
OpenDlg: TOpenDialog;
begin
OpenDlg := TOpenDialog.Create(Application);
try
OpenDlg.FileName := EditImagePath.Text;
OpenDlg.Filter := 'Image Files (*.jpg;*.jpeg;*.bmp;*.ico;*.emf;*.wmf)|*.jpg;*.jpeg;*.bmp;*.ico;*.emf;*.wmf|All Files(*.*)|*.*';
OpenDlg.Options := OpenDlg.Options + [ofPathMustExist];
if OpenDlg.Execute then
begin
EditImagePath.Text := OpenDlg.Filename;
ImageCommodity.Picture.LoadFromFile(EditImagePath.Text);
end;
finally
OpenDlg.Free;
end;
end;
procedure TFormSetCommodity.FormShow(Sender: TObject);
var
i, Min, Max: integer;
item: string;
begin
CBSort.Clear;
DataSetSortTrade.Active := False;
DataSetSortTrade.CommandText := 'select name from Sort';
DataSetSortTrade.Active := True;
Min := 0;
Max := DataSetSortTrade.RecordCount;
DataSetSortTrade.First;
for i := Min to (Max-1) do
begin
item := DataSetSortTrade.FieldByName('name').AsString;
CBSort.Items.Add(item);
DataSetSortTrade.Next;
end;
CBTrade.Clear;
DataSetSortTrade.Active := False;
DataSetSortTrade.CommandText := 'select name from Trade';
DataSetSortTrade.Active := True;
Min := 0;
Max := DataSetSortTrade.RecordCount;
DataSetSortTrade.First;
for i := Min to (Max-1) do
begin
item := DataSetSortTrade.FieldByName('name').AsString;
CBTrade.Items.Add(item);
DataSetSortTrade.Next;
end;
end;
procedure TFormSetCommodity.BtnAddClick(Sender: TObject);
var
SortID, TradeID: string;
begin
if (EditID.Text = '') or (EditName.Text = '') or (CBSort.Text = '') or (CBTrade.Text = '') then
begin
Application.MessageBox('输入不能为空!', '错误', MB_OK);
exit;
end;
SortID := GetIDByName(CBSort.Text, 'Sort');
TradeID := GetIDByName(CBTrade.Text, 'Trade');
CmdSetCommodity.CommandText :=
'INSERT INTO Commodity Values('''
+ EditID.Text
+ ''','''
+ EditName.Text
+ ''','''
+ SortID
+ ''','''
+ TradeID
+ ''','''
+ EditImagePath.Text
+ ''')';
CmdSetCommodity.Execute;
DataSetCommodity.Requery;
end;
procedure TFormSetCommodity.BtnModifyClick(Sender: TObject);
var
SortID, TradeID: string;
begin
if (EditID.Text = '') or (EditName.Text = '') or (CBSor