Delphi中的Sender:TObject对象解析

本文深入探讨了Delphi编程中Sender参数的作用和使用方法,解释了其在事件处理过程中的意义,并展示了如何利用Sender参数实现多控件事件处理的重用性和灵活性。


Delphi中的Sender:TObject对象解析

procedure TForm1.Button1Click(Sender: TObject);

begin

end;

解析:Procedure是过程,TForm是窗体类,加上数字就是某个窗体,像TForm1就是Form1窗体。

Button1是你的按钮控件的名称,Button1Click就是按钮的单击事件,(Sender:Tobject)就是发送消息到对象,这里默认为本窗体。

Sender的类型是Tobject,是Tobject的派生类。


Delphi中Sender对象

1.Sender的定义:

每一个事件处理里面至少都有一个Sender参数。比如:

procedure TForm1.Button1Click(Sender:TObject);

begin

...

end;

Sender的含义就是代表调用TForm1.Button1Click这个过程的控件. 

由于Sender是TObject,所以任何object都可以赋给Sender.当你点击BUTTON1时,会产生一个Button1Click事件,系统会把Button1传递给Button1Click过程作为参数,也就是所说的Sender.


2.Sender的用法:

<1>.由于Sender代表了调用所在过程的控件,那么你就可以直接把它拿来当那个控件用,不过如果要用属性的话,最好写成(Sender as 控件名).控件属性:=... 例如:

procedure TForm1.Edit1Click(Sender: TObject);

begin

with Sender as TEdit do

begin

text:=’hello’;

end;

end;

再比如:

Procedure   TForm1.Button1Click(Sender:TObject); 

begin 

      if   Sender   is   TButton   then 

            showmessage((Sender   as   TButton).Caption) 

      else 

            showmessage( '没有Sender ') 

end; 

如果你这样调用 

Procedure   TForm1.button2.click(sender:Tobject); 

begin 

      Button1Click(nil); 

end; 

则返回 '没有Sender ' 

而如果你这样调用 

Procedure   TForm1.button2.click(sender:Tobject); 

begin 

      Button1Click(sender); 

end;

则显示:button2 

<2>.如果在两个事件中处理同样的事情,那么可以利用Sender来省去重写同样的过程。例如:

Procedure TForm1.Button1Click(Sender:TObject);

begin

do same sth.....;

if Sender=Button1 then

do sth....;

if Sender=Button2 then

do other sth....;

end;

procedure TForm1.Button2Click(Sender:TOBJect);

begin

Button1Click(Button2);

end;


3.Sender参数的两个用途

Sender是一个TObject类型的参数,它告诉Delphi哪个控件接收这个事件并调用相应的处理过程。

你可以编写一个单一的事件处理句柄,通过Sender参数和IF…THEN…语句或者CASE语句配合,来处理多个构件。

发生事件的构件或控件的值已经赋给了Sender参数,该参数的第一个用途用途就在于:可以使用保留字IS来测试Sender,

以便找到调用这个事件处理句柄的构件或控件的类型。

例如,将表单中编辑框和标签的Click事件的处理句柄都指向表单的xxx过程,编辑框和标签对Click事件有不同的反应: 

  procedure TForm1 xxx(Sender:TObject); 

  begin 

  if(sender is Tedit) then 

  showmessage(′this is a editbox′); 

  if(sender is Tlabel) then 

  showmessage(′this is a label′); 

  end; 

Sender参数的第二个用途是结合AS操作符进行类型转换,将若干个派生于某一父类的子类强制转换成该父类。

例如表单中有一个TEdit类控件和一个TMemo控件,它们实际上都派生于TcustomEdit类,

如果你要为二者的某一事件提供同样处理,可以将二者事件句柄都指向自定义的过程yyy: 

  Procedure TForm1.yyy(Sender:TObject); 

  begin 

  (sender as TcustomEdit).text:=′This is some demo text′; 

  end; 

在过程中,AS操作符将TEdit类和TMemo类均强制转换成TcustomEdit类,再对TcustomEdit类的属性赋值。注意这种转换必须符合Delphi中类的层次关系。 

  

总而言之,使用Sender参数可以通过单一过程段处理多类控件,真正体现了Delphi面向对象的重用性。

unit main; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ExtCtrls, DB, PostgreSQLUniProvider, Uni, MemDS, DBAccess, GridsEh, DBGridEh, DBGridEhGrouping, ToolCtrlsEh, DBGridEhToolCtrls, DynVarsEh, EhLibVCL, Vcl.Buttons, Vcl.ComCtrls, Vcl.Menus, Vcl.DBCtrls, Vcl.Mask, IdTCPClient, DBAxisGridsEh, DBGridEhImpExp, DBCtrlsEh, DataSetImpExpEh, System.UITypes, ShellAPI,Vcl.Clipbrd, SynEditHighlighter, SynHighlighterSQL, SynAutoCorrect, SynCompletionProposal, SynEdit, SynMemo,SynEditTypes; type TForm1 = class(TForm) Panel1: TPanel; Label1: TLabel; cbConnections: TComboBox; Label2: TLabel; btnExecute: TButton; Panel2: TPanel; DBGridEh0: TDBGridEh; UniConnection0: TUniConnection; UniQuery1: TUniQuery; btnUpdate: TButton; btnExportExcel: TButton; SaveDialog1: TSaveDialog; StatusBar1: TStatusBar; btnExit1: TButton; UniDataSource0: TUniDataSource; lstTables: TListBox; // 新增列表控件 // DBGridEh1: TDBGridEh; // 新增DBGridEh控件 UniQueryTables: TUniQuery; // 用于获取表信息的查询 UniQueryFields: TUniQuery; // 用于获取字段信息的查询 UniDataSource1: TUniDataSource; DBGridEh1: TDBGridEh; pmTables: TPopupMenu; SynCompletionProposal1: TSynCompletionProposal; SynAutoComplete1: TSynAutoComplete; SynAutoCorrect1: TSynAutoCorrect; SynEdit1: TSynEdit; SynSQLSyn1: TSynSQLSyn; // 新增数据源 procedure FormCreate(Sender: TObject); procedure btnExecuteClick(Sender: TObject); procedure btnUpdateClick(Sender: TObject); procedure btnExportExcelClick(Sender: TObject); procedure cbConnectionsChange(Sender: TObject); procedure btnExit1Click(Sender: TObject); procedure lstTablesClick(Sender: TObject); procedure SynCompletionProposal1Execute(Kind: SynCompletionType; Sender: TObject; var CurrentInput: string; var x, y: Integer; var CanExecute: Boolean); procedure SynEdit1DblClick(Sender: TObject); procedure SynEdit1KeyPress(Sender: TObject; var Key: Char); private { Private declarations } procedure LoadConnections; procedure ConnectToDatabase(Index: Integer); procedure LoadTables; // 加载所有表 procedure LoadFields(const TableFullName: string); // 加载选定表的所有字段 function TestNetworkConnection(const Server: string; Port: Integer): Boolean; procedure lstTablesContextPopup(Sender: TObject; MousePos: TPoint; var Handled: Boolean); procedure DBGridEh1ContextPopup(Sender: TObject; MousePos: TPoint; var Handled: Boolean); procedure CopyTableNameToClipboard(Sender: TObject); procedure CopyFieldNameToClipboard(Sender: TObject); procedure InitHighlighter; procedure LoadDatabaseSchema; public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} uses ExcelXP, ComObj; function TForm1.TestNetworkConnection(const Server: string; Port: Integer): Boolean; var Client: TIdTCPClient; begin Result := False; Client := TIdTCPClient.Create(nil); try Client.Host := Server; Client.Port := Port; try Client.Connect; Result := Client.Connected; Client.Disconnect; except Result := False; end; finally Client.Free; end; end; procedure TForm1.lstTablesContextPopup(Sender: TObject; MousePos: TPoint; var Handled: Boolean); var ItemIndex: Integer; TableName: string; MenuItem: TMenuItem; begin // 获取当前鼠标位置对应的项索引 ItemIndex := lstTables.ItemAtPos(MousePos, True); if ItemIndex >= 0 then begin lstTables.ItemIndex := ItemIndex; TableName := lstTables.Items[ItemIndex]; // 清空现有菜单项 pmTables.Items.Clear; // 创建新的菜单项 MenuItem := TMenuItem.Create(pmTables); MenuItem.Caption := '复制表名: ' + TableName; MenuItem.OnClick := CopyTableNameToClipboard; pmTables.Items.Add(MenuItem); // 显示弹出菜单 pmTables.Popup(MousePos.X + lstTables.Left, MousePos.Y + lstTables.Top); Handled := True; // 阻止默认的弹出菜单 end; end; procedure TForm1.SynCompletionProposal1Execute(Kind: SynCompletionType; Sender: TObject; var CurrentInput: string; var x, y: Integer; var CanExecute: Boolean); var // ... [变量声明保持不变] ... begin CanExecute := UniConnection0.Connected; if not CanExecute then Exit; // 外层更新控制 - 确保整个操作原子性 SynCompletionProposal1.ItemList.BeginUpdate; try SynCompletionProposal1.ItemList.Clear; // 先清空列表 try // ... [获取当前光标位置和单词的逻辑保持不变] ... if LastDotPos = 0 then begin // 没有点号,显示所有表名 LoadDatabaseSchema; end else begin // ... [解析表名和字段名的逻辑保持不变] ... // 关键修复:处理大小写敏感问题 FieldsQuery.SQL.Text := 'SELECT column_name ' + 'FROM information_schema.columns ' + 'WHERE LOWER(table_name) = LOWER(:table_name) ' + // 使用LOWER确保大小写不敏感 'AND LOWER(table_schema) = LOWER(:schema_name) ' + 'ORDER BY ordinal_position'; FieldsQuery.ParamByName('table_name').AsString := TableName; FieldsQuery.ParamByName('schema_name').AsString := SchemaName; FieldsQuery.Open; if FieldsQuery.RecordCount = 0 then begin // 添加占位项防止空列表 SynCompletionProposal1.ItemList.Add('(无字段)'); end else begin while not FieldsQuery.Eof do begin SynCompletionProposal1.ItemList.Add( FieldsQuery.FieldByName('column_name').AsString ); FieldsQuery.Next; end; end; end; except on E: Exception do begin SynCompletionProposal1.ItemList.Add('错误: ' + E.Message); end; end; // 确保列表不为空 if SynCompletionProposal1.ItemList.Count = 0 then SynCompletionProposal1.ItemList.Add('(无建议)'); finally SynCompletionProposal1.ItemList.EndUpdate; // 确保总是结束更新 end; end; procedure TForm1.SynEdit1DblClick(Sender: TObject); begin SynEdit1.SelectAll; end; procedure TForm1.SynEdit1KeyPress(Sender: TObject; var Key: Char); begin // 当输入点号时触发自动完成 if Key = '.' then begin if not UniConnection0.Connected then begin ShowMessage('数据库未连接'); Key := #0; Exit; end; // 先插入点号 SynEdit1.SelText := '.'; // 立即触发自动完成 SynCompletionProposal1.ActivateCompletion; Key := #0; // 阻止默认处理 end // Ctrl+Space 触发自动完成 else if (Key = ' ') and (GetKeyState(VK_CONTROL) < 0) then begin Key := #0; SynCompletionProposal1.ActivateCompletion; end; end; procedure TForm1.InitHighlighter; begin SynSQLSyn1.SQLDialect := sqlPostgres; SynSQLSyn1.KeyAttri.Foreground := clNavy; SynSQLSyn1.CommentAttri.Foreground := clGreen; SynSQLSyn1.StringAttri.Foreground := clMaroon; SynEdit1.Highlighter := SynSQLSyn1; SynEdit1.Options := SynEdit1.Options + [eoAutoIndent, eoScrollPastEol]; end; procedure TForm1.DBGridEh1ContextPopup(Sender: TObject; MousePos: TPoint; var Handled: Boolean); var AHitTest: TGridCoord; // 使用 TGridCoord 替代 TGridHitTestInfoEh FieldName: string; MenuItem: TMenuItem; Cell: TGridCoord; begin // 获取鼠标点击位置的单元格坐标 Cell := DBGridEh1.MouseCoord(MousePos.X, MousePos.Y); if (Cell.X >= 0) and (Cell.Y >= 0) and (UniQueryFields.Active) and (not UniQueryFields.IsEmpty) and (Cell.X = 0) then // column_name 列 begin // 定位到对应行 UniQueryFields.RecNo := Cell.Y + 1; FieldName := UniQueryFields.FieldByName('column_name').AsString; // 清空现有菜单项 pmTables.Items.Clear; // 创建新的菜单项 MenuItem := TMenuItem.Create(pmTables); MenuItem.Caption := '复制字段名: ' + FieldName; MenuItem.OnClick := CopyFieldNameToClipboard; pmTables.Items.Add(MenuItem); // 显示弹出菜单 pmTables.Popup(MousePos.X + DBGridEh1.Left, MousePos.Y + DBGridEh1.Top); Handled := True; // 阻止默认的弹出菜单 end; end; procedure TForm1.LoadDatabaseSchema; var TablesQuery: TUniQuery; begin if not UniConnection0.Connected then Exit; TablesQuery := TUniQuery.Create(nil); try TablesQuery.Connection := UniConnection0; TablesQuery.SQL.Text := 'SELECT LOWER(table_schema) AS schema_name, ' + // 统一小写处理 'LOWER(table_name) AS table_name ' + // 统一小写处理 'FROM information_schema.tables ' + 'WHERE table_catalog = current_database() ' + 'AND table_schema NOT IN (''information_schema'', ''pg_catalog'') ' + 'ORDER BY table_schema, table_name'; TablesQuery.Open; while not TablesQuery.Eof do begin SynCompletionProposal1.ItemList.Add( TablesQuery.FieldByName('schema_name').AsString + '.' + TablesQuery.FieldByName('table_name').AsString ); TablesQuery.Next; end; finally TablesQuery.Free; end; end; procedure TForm1.CopyTableNameToClipboard(Sender: TObject); var TableName: string; begin if lstTables.ItemIndex >= 0 then begin TableName := lstTables.Items[lstTables.ItemIndex]; Clipboard.AsText := TableName; StatusBar1.Panels[0].Text := '已复制表名: ' + TableName; end; end; procedure TForm1.CopyFieldNameToClipboard(Sender: TObject); var FieldName: string; begin if UniQueryFields.Active and not UniQueryFields.IsEmpty then begin FieldName := UniQueryFields.FieldByName('column_name').AsString; Clipboard.AsText := FieldName; StatusBar1.Panels[0].Text := '已复制字段名: ' + FieldName; end; end; procedure TForm1.FormCreate(Sender: TObject); begin LoadConnections; if cbConnections.Items.Count > 0 then begin cbConnections.ItemIndex := 0; // 尝试连接 ConnectToDatabase(0); end; // 修改自动完成设置 SynCompletionProposal1.Options := [scoLimitToMatchedText, scoUseInsertList, scoEndCharCompletion]; SynCompletionProposal1.EndOfTokenChr := ' .()=,;'; SynCompletionProposal1.TriggerChars := '.'; // 点号触发 SynCompletionProposal1.ShortCut := 16416; // Ctrl+Space LoadTables; // 加载所有表 // 创建弹出菜单实例(如果设计时没有创建) if not Assigned(pmTables) then pmTables := TPopupMenu.Create(Self); // 关联右键菜单事件 lstTables.OnContextPopup := lstTablesContextPopup; DBGridEh1.OnContextPopup := DBGridEh1ContextPopup; InitHighlighter; //在FormCreate中添加键盘事件 SynEdit1.OnKeyPress := SynEdit1KeyPress; end; procedure TForm1.LoadConnections; begin cbConnections.Items.Clear; cbConnections.Items.Add('连接1: 21 gy_zh_hy_cp_aq_jf_yw_yy_nl_dt_dz_auth_td_gz'); cbConnections.Items.Add('连接2: 22 sc_fz_wd_sz_bi_jk_hl_ys_tj'); cbConnections.Items.Add('连接3: 23 jy_jc_bl_zj'); cbConnections.Items.Add('连接4: 24 jz_br_yj'); cbConnections.Items.Add('连接5: 25 yz_lj_hz_zz_yx'); cbConnections.Items.Add('连接6: 26 fy_yg_hg_wm'); cbConnections.Items.Add('连接7: 27 yp_zl_sm_jh_wl'); cbConnections.Items.Add('连接8: 28 聚合库'); end; procedure TForm1.LoadTables; begin lstTables.Items.Clear; if not UniConnection0.Connected then Exit; try UniQueryTables.Close; // 修改SQL查询,获取lyradb数据库中除public模式外的所有表 UniQueryTables.SQL.Text := 'SELECT table_schema, table_name ' + 'FROM information_schema.tables ' + 'WHERE table_catalog = ''lyradb'' ' + // 指定数据库名 'AND table_schema NOT IN (''public'', ''information_schema'', ''pg_catalog'',''hint_plan'',''lyra'',''profile'',''repack'',''util'') ' + // 排除系统模式 'AND table_schema NOT LIKE ''ceti%'' ' +// 排除系统模式 'AND table_schema NOT LIKE ''grus%'' ' +// 排除系统模式 'AND table_type = ''BASE TABLE'' ' + // 只获取基本表 'ORDER BY table_schema, table_name'; UniQueryTables.Open; while not UniQueryTables.Eof do begin // 显示格式:模式名.表名 lstTables.Items.Add( UniQueryTables.FieldByName('table_schema').AsString + '.' + UniQueryTables.FieldByName('table_name').AsString ); UniQueryTables.Next; end; UniQueryTables.Close; except on E: Exception do ShowMessage('加载表列表失败: ' + E.Message); end; end; procedure TForm1.lstTablesClick(Sender: TObject); begin if lstTables.ItemIndex >= 0 then begin // 加载选定表的所有字段 LoadFields(lstTables.Items[lstTables.ItemIndex]); end; end; procedure TForm1.LoadFields(const TableFullName: string); var SchemaName, TableName: string; DotPos,i: Integer; begin // 解析模式名和表名 DotPos := Pos('.', TableFullName); if DotPos > 0 then begin SchemaName := AnsiQuotedStr(Copy(TableFullName, 1, DotPos-1), '"'); TableName := AnsiQuotedStr(Copy(TableFullName, DotPos+1, MaxInt), '"'); end else begin SchemaName := 'public'; // 默认模式 TableName := AnsiQuotedStr(TableFullName, '"'); end; // 清空字段信息 DBGridEh1.Columns.Clear; // 获取选定表的所有字段信息(包含注释) UniQueryFields.Close; UniQueryFields.SQL.Text := 'SELECT ' + ' c.column_name, ' + ' c.data_type, ' + ' c.column_default, ' + ' c.is_nullable, ' + ' col_description((c.table_schema||''.''||c.table_name)::regclass::oid, c.ordinal_position) as column_comment ' + 'FROM information_schema.columns c ' + 'WHERE c.table_schema = ' + SchemaName + 'AND c.table_name = ' + TableName + 'ORDER BY c.ordinal_position'; UniQueryFields.ParamByName('schema_name').AsString := SchemaName; UniQueryFields.ParamByName('table_name').AsString := TableName; UniQueryFields.Open; // 设置数据源 UniDataSource1.DataSet := UniQueryFields; // 自动添加所有列到DBGridEh DBGridEh1.Columns.AddAllColumns(True); // 然后再限制最大宽度 for i := 0 to DBGridEh1.Columns.Count - 1 do begin DBGridEh1.Columns[i].OptimizeWidth; if DBGridEh1.Columns[i].Width > 200 then DBGridEh1.Columns[i].Width := 200; end; end; procedure TForm1.ConnectToDatabase(Index: Integer); var ConnStr: string; begin UniConnection0.Disconnect; // 设置 Provider 名称 UniConnection0.ProviderName := 'PostgreSQL'; case Index of 0: begin // 连接1 UniConnection0.Server := '192.168.129.21'; UniConnection0.Database := 'lyradb'; UniConnection0.Username := 'lyra_ops'; UniConnection0.Password := 'i48VF69KXwgP'; UniConnection0.Port := 5432; // 明确指定端口 end; 1: begin // 连接2 UniConnection0.Server := '192.168.129.22'; UniConnection0.Database := 'lyradb'; UniConnection0.Username := 'lyra_ops'; UniConnection0.Password := 'i48VF69KXwgP'; UniConnection0.Port := 5432; // 明确指定端口 end; // 其他连接配置... 2: begin // 连接3 UniConnection0.Server := '192.168.129.23'; UniConnection0.Database := 'lyradb'; UniConnection0.Username := 'lyra_ops'; UniConnection0.Password := 'i48VF69KXwgP'; UniConnection0.Port := 5432; // 明确指定端口 end; 3: begin // 连接4 UniConnection0.Server := '192.168.129.24'; UniConnection0.Database := 'lyradb'; UniConnection0.Username := 'lyra_ops'; UniConnection0.Password := 'i48VF69KXwgP'; UniConnection0.Port := 5432; // 明确指定端口 end; 4: begin // 连接5 UniConnection0.Server := '192.168.129.25'; UniConnection0.Database := 'lyradb'; UniConnection0.Username := 'lyra_ops'; UniConnection0.Password := 'i48VF69KXwgP'; UniConnection0.Port := 5432; // 明确指定端口 end; 5: begin // 连接6 UniConnection0.Server := '192.168.129.26'; UniConnection0.Database := 'lyradb'; UniConnection0.Username := 'lyra_ops'; UniConnection0.Password := 'i48VF69KXwgP'; UniConnection0.Port := 5432; // 明确指定端口 end; 6: begin // 连接7 UniConnection0.Server := '192.168.129.27'; UniConnection0.Database := 'lyradb'; UniConnection0.Username := 'lyra_ops'; UniConnection0.Password := 'i48VF69KXwgP'; UniConnection0.Port := 5432; // 明确指定端口 end; 7: begin // 连接8 UniConnection0.Server := '192.168.129.28'; UniConnection0.Database := 'lyradb'; UniConnection0.Username := 'lyra_ops'; UniConnection0.Password := 'i48VF69KXwgP'; UniConnection0.Port := 5432; // 明确指定端口 end; end; // 显示连接字符串用于调试 ConnStr := Format('Server=%s;Database=%s;User_ID=%s;Port=%d', [UniConnection0.Server, UniConnection0.Database, UniConnection0.Username, UniConnection0.Port]); StatusBar1.Panels[0].Text := '正在连接: ' + ConnStr; try if not TestNetworkConnection(UniConnection0.Server, 5432) then begin ShowMessage('无法连接到数据库服务器,请检查网络连接'); Exit; end; UniConnection0.Connect; // 在状态栏显示连接状态 StatusBar1.Panels[0].Text := '已连接到: ' + cbConnections.Items[Index]; // 连接成功后加载表 // 连接成功后重新加载 LoadTables; if UniConnection0.Connected then begin // 清空并重新加载表名缓存 SynCompletionProposal1.ItemList.Clear; LoadDatabaseSchema; end; except on E: Exception do // ShowMessage('连接失败: ' + E.Message); begin ShowMessage(Format('连接失败:'#13#10'错误: %s'#13#10'连接字符串: %s', [E.Message, ConnStr])); StatusBar1.Panels[0].Text := '连接失败'; end; end; end; procedure TForm1.btnExecuteClick(Sender: TObject); var i: Integer; SQLToExecute: string; begin if not UniConnection0.Connected then begin ShowMessage('请先连接到数据库'); Exit; end; // 判断是否有选中文本 if SynEdit1.SelText <> '' then SQLToExecute := SynEdit1.SelText // 执行选中的SQL else SQLToExecute := SynEdit1.Text; // 执行全部SQL if Trim(SQLToExecute) = '' then begin ShowMessage('请输入SQL语句'); Exit; end; try UniQuery1.Close; UniQuery1.SQL.Text := SQLToExecute; // 判断SQL类型(查询或更新) if (Pos('SELECT', UpperCase(SQLToExecute)) = 1) or (Pos('WITH', UpperCase(SQLToExecute)) = 1) or (Pos('SHOW', UpperCase(SQLToExecute)) = 1) then begin UniQuery1.Open; StatusBar1.Panels[1].Text := '记录数: ' + IntToStr(UniQuery1.RecordCount); // 自动调整列宽 DBGridEh0.Columns.Clear; DBGridEh0.Columns.AddAllColumns(True); for i := 0 to DBGridEh0.Columns.Count - 1 do begin DBGridEh0.Columns[i].OptimizeWidth; if DBGridEh0.Columns[i].Width > 200 then DBGridEh0.Columns[i].Width := 200; end; end else begin // 执行非查询语句 UniQuery1.ExecSQL; StatusBar1.Panels[1].Text := '执行成功,影响行数: ' + IntToStr(UniQuery1.RowsAffected); end; except on E: Exception do ShowMessage('执行SQL失败: ' + E.Message); end; end; procedure TForm1.btnUpdateClick(Sender: TObject); begin if UniQuery1.State in [dsEdit, dsInsert] then UniQuery1.Post; try UniQuery1.ApplyUpdates; ShowMessage('更新成功'); except on E: Exception do ShowMessage('更新失败: ' + E.Message); end; end; procedure TForm1.btnExportExcelClick(Sender: TObject); var // ExpClass: TDBGridEhExportClass; Exp: TDBGridEhExportAsXLSX; Ext: string; i: Integer; SaveDialog1: TSaveDialog; begin if UniQuery1.IsEmpty then begin ShowMessage('没有数据可导出'); Exit; end; SaveDialog1 := TSaveDialog.Create(nil); try SaveDialog1.Filter := 'Excel文件(*.xls)|*.xls|Excel 2007+(*.xlsx)|*.xlsx'; SaveDialog1.DefaultExt := 'xls'; SaveDialog1.InitialDir := '\Desktop\'; SaveDialog1.FileName := '导出数据_' + FormatDateTime('yyyymmddhhnnss', Now); if SaveDialog1.Execute then begin // ExpClass := TDBGridEhExportAsXLS; Exp := TDBGridEhExportAsXLSX.Create; Ext := 'xls'; if Pos('.xlsx', LowerCase(SaveDialog1.FileName)) > 0 then Ext := 'xlsx'; if UpperCase(ExtractFileExt(SaveDialog1.FileName)) <> '.' + UpperCase(Ext) then SaveDialog1.FileName := SaveDialog1.FileName + '.' + Ext; // 执行导出 try // 设置DBGridEh的样式 Exp.DBGridEh := DBGridEh0; Exp.DBGridEh.TitleFont := DBGridEh0.TitleFont; Exp.DBGridEh.TitleFont.Name := '宋体'; Exp.DBGridEh.TitleFont.Style := [fsBold]; Exp.DBGridEh.TitleFont.Size := 12; Exp.DBGridEh.TitleParams.Color := clSkyBlue; for i := 0 to Exp.DBGridEh.Columns.Count - 1 do begin Exp.DBGridEh.Columns[i].OptimizeWidth; if Exp.DBGridEh.Columns[i].Width > 200 then Exp.DBGridEh.Columns[i].Width := 200; end; if SameText(Ext, 'xlsx') then begin Exp.ExportToFile(SaveDialog1.FileName, True); end else begin Exp.ExportToFile(SaveDialog1.FileName, True); end; finally Exp.Free; end; // ShowMessage('导出成功: ' + SaveDialog1.FileName); // 假设导出操作已经完成 if MessageDlg('导出成功: ' + SaveDialog1.FileName + #13#10'是否要打开文件?', mtInformation, [mbYes, mbNo], 0, mbNo) = mrYes then begin // 用户点击了"是"(打开文件) ShellExecute(0, 'open', PChar(SaveDialog1.FileName), nil, nil, SW_SHOWNORMAL); end; end; finally SaveDialog1.Free; end; end; procedure TForm1.btnExit1Click(Sender: TObject); begin Form1.Close; end; procedure TForm1.cbConnectionsChange(Sender: TObject); begin ConnectToDatabase(cbConnections.ItemIndex); end; end. 按上述意见如此修改了代码,编译报错:[dcc32 Error] main.pas(141): E2029 Identifier expected but 'BEGIN' found [dcc32 Error] main.pas(153): E2003 Undeclared identifier: 'LastDotPos' [dcc32 Warning] main.pas(153): W1023 Comparing signed and unsigned types - widened both operands [dcc32 Error] main.pas(163): E2003 Undeclared identifier: 'FieldsQuery' [dcc32 Error] main.pas(163): E2066 Missing operator or semicolon [dcc32 Error] main.pas(169): E2066 Missing operator or semicolon [dcc32 Error] main.pas(169): E2066 Missing operator or semicolon [dcc32 Error] main.pas(169): E2003 Undeclared identifier: 'TableName' [dcc32 Error] main.pas(170): E2066 Missing operator or semicolon [dcc32 Error] main.pas(170): E2066 Missing operator or semicolon [dcc32 Error] main.pas(170): E2003 Undeclared identifier: 'SchemaName' [dcc32 Error] main.pas(171): E2066 Missing operator or semicolon [dcc32 Error] main.pas(173): E2029 'THEN' expected but identifier 'RecordCount' found [dcc32 Error] main.pas(180): E2029 'DO' expected but identifier 'Eof' found [dcc32 Error] main.pas(183): E2029 ')' expected but identifier 'FieldByName' found [dcc32 Error] main.pas(183): E2066 Missing operator or semicolon [dcc32 Error] main.pas(184): E2029 'END' expected but ')' found [dcc32 Error] main.pas(188): E2125 EXCEPT or FINALLY expected [dcc32 Error] main.pas(200): E2029 'END' expected but 'FINALLY' found [dcc32 Error] main.pas(203): E2029 '.' expected but ';' found 可以的话给一个完整的代码,以免给出部分代码自行修改有误。
最新发布
08-28
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值