Delphi - 带有复选框单击问题的 OpenDialog 和 DBGrid
Delphi - OpenDialog and DBGrid with Checkbox Click Problem
如果我打开 TDBGrid
上方的 TOpenDialog
并双击 select 一个项目,单击将重定向到 TDBGrid
,同时标记复选框。
要进行测试,请单击按钮并双击以在 OpenDialog 上打开一个文件。第一个复选框将被选中。
Pas 文件
unit Unit2;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Data.DB, FireDAC.Stan.Intf,
FireDAC.Stan.Option, FireDAC.Stan.Param, FireDAC.Stan.Error,
FireDAC.DatS, FireDAC.Phys.Intf, FireDAC.DApt.Intf,
FireDAC.Stan.StorageBin, FireDAC.Comp.DataSet, FireDAC.Comp.Client,
Vcl.StdCtrls, Vcl.ExtCtrls, Vcl.Grids, Vcl.DBGrids;
type
TForm2 = class(TForm)
DBGrid1: TDBGrid;
Panel1: TPanel;
Button1: TButton;
OpenDialog1: TOpenDialog;
MemData: TFDMemTable;
MemDataCHECKBOX: TStringField;
MemDataNAME: TStringField;
DataSource1: TDataSource;
procedure DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
procedure DBGrid1CellClick(Column: TColumn);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form2: TForm2;
implementation
{$R *.dfm}
procedure TForm2.Button1Click(Sender: TObject);
begin
if OpenDialog1.Execute then
Begin
//
End;
end;
procedure TForm2.DBGrid1CellClick(Column: TColumn);
begin
if Column.FieldName = 'CHECKBOX' then
begin
if Column.Grid.DataSource.DataSet.RecordCount > 0 then
Begin
Column.Grid.DataSource.DataSet.Edit;
if Column.Grid.DataSource.DataSet.FieldByName('CHECKBOX').AsString = 'T' then
begin
Column.Grid.DataSource.DataSet.FieldByName('CHECKBOX').AsString := 'F';
end
else
begin
Column.Grid.DataSource.DataSet.FieldByName('CHECKBOX').AsString := 'T';
end;
Column.Grid.DataSource.DataSet.Post;
End;
end;
end;
procedure TForm2.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
var
Check: Integer;
R: TRect;
begin
if not (odd(DBGrid1.DataSource.DataSet.RecNo)) then
begin
DBGrid1.Canvas.Brush.Color := $EBEBEB;
DBGrid1.Canvas.Font.Color := clBlack;
DBGrid1.Canvas.FillRect(Rect);
end;
if (gdSelected in State) or (DBGrid1.SelectedRows.CurrentRowSelected) then
begin
DBGrid1.Canvas.Brush.Color := clHighlight;
DBGrid1.Canvas.Font.Color := clWhite;
DBGrid1.Canvas.FillRect(Rect);
end;
DBGrid1.DefaultDrawColumnCell(Rect, DataCol, Column, State);
if Column.FieldName = 'CHECKBOX' then
begin
if MemData.FieldByName('CHECKBOX').AsString = 'T' then
begin
Check := DFCS_CHECKED;
end
else
Check := 0;
DBGrid1.Canvas.FillRect(Rect);
R := Rect;
InflateRect(R, -2, -2);
DrawFrameControl(DBGrid1.Canvas.handle, R, DFC_BUTTON, DFCS_BUTTONCHECK or Check);
end;
end;
end.
Dfm 文件
object Form2: TForm2
Left = 0
Top = 0
Caption = 'Form2'
ClientHeight = 509
ClientWidth = 756
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'Tahoma'
Font.Style = []
OldCreateOrder = False
Position = poScreenCenter
PixelsPerInch = 96
TextHeight = 13
object DBGrid1: TDBGrid
Left = 0
Top = 41
Width = 756
Height = 468
Align = alClient
DataSource = DataSource1
DefaultDrawing = False
Options = [dgTitles, dgIndicator, dgColumnResize, dgColLines, dgRowLines, dgTabs, dgConfirmDelete, dgCancelOnExit, dgTitleClick, dgTitleHotTrack]
TabOrder = 0
TitleFont.Charset = DEFAULT_CHARSET
TitleFont.Color = clWindowText
TitleFont.Height = -11
TitleFont.Name = 'Tahoma'
TitleFont.Style = []
OnCellClick = DBGrid1CellClick
OnDrawColumnCell = DBGrid1DrawColumnCell
Columns = <
item
Expanded = False
FieldName = 'CHECKBOX'
Visible = True
end
item
Expanded = False
FieldName = 'NAME'
Width = 635
Visible = True
end>
end
object Panel1: TPanel
Left = 0
Top = 0
Width = 756
Height = 41
Align = alTop
BevelInner = bvLowered
TabOrder = 1
ExplicitLeft = 120
ExplicitTop = 16
ExplicitWidth = 185
object Button1: TButton
Left = 14
Top = 8
Width = 75
Height = 25
Caption = 'Open Dialog'
TabOrder = 0
OnClick = Button1Click
end
end
object OpenDialog1: TOpenDialog
Left = 153
Top = 137
end
object MemData: TFDMemTable
Active = True
FieldDefs = <
item
Name = 'CHECKBOX'
DataType = ftString
Size = 1
end
item
Name = 'NAME'
DataType = ftString
Size = 50
end>
IndexDefs = <>
FetchOptions.AssignedValues = [evMode]
FetchOptions.Mode = fmAll
ResourceOptions.AssignedValues = [rvPersistent, rvSilentMode]
ResourceOptions.Persistent = True
ResourceOptions.SilentMode = True
UpdateOptions.AssignedValues = [uvCheckRequired, uvAutoCommitUpdates]
UpdateOptions.CheckRequired = False
UpdateOptions.AutoCommitUpdates = True
StoreDefs = True
Left = 153
Top = 184
Content = {
414442530F00C31DA2040000FF00010001FF02FF0304000E0000004D0065006D
00440061007400610005000A0000005400610062006C00650006000000000007
0000080032000000090000FF0AFF0B04001000000043004800450043004B0042
004F00580005001000000043004800450043004B0042004F0058000C00010000
000E000D000F0001000000100001110001120001130001140001150001160010
00000043004800450043004B0042004F005800170001000000FEFF0B04000800
00004E0041004D0045000500080000004E0041004D0045000C00020000000E00
0D000F0032000000100001110001120001130001140001150001160008000000
4E0041004D004500170032000000FEFEFF18FEFF19FEFF1AFF1B1C0000000000
FF1D000001000000460100040000004A484F4EFEFEFF1B1C0001000000FF1D00
0001000000460100050000004C55434153FEFEFF1B1C0002000000FF1D000001
000000460100050000004B454C4C59FEFEFF1B1C0003000000FF1D0000010000
004601000500000053494C4153FEFEFF1B1C0004000000FF1D00000100000046
0100060000004D4152435553FEFEFF1B1C0005000000FF1D0000010000004601
00050000004A454E4E59FEFEFF1B1C0006000000FF1D00000100000046010004
00000052495441FEFEFF1B1C0007000000FF1D00000100000046010005000000
5045544552FEFEFF1B1C0008000000FF1D000001000000460100080000004245
4E4A414D494EFEFEFF1B1C0009000000FF1D000001000000460100060000004D
4152434941FEFEFF1B1C000A000000FF1D000001000000460100080000005041
545249434941FEFEFF1B1C000B000000FF1D0000010000004601000500000046
4142494FFEFEFF1B1C000C000000FF1D000001000000460100070000004D4152
43454C4FFEFEFF1B1C000D000000FF1D000001000000460100080000004C454F
4E4152444FFEFEFF1B1C000E000000FF1D00000100000046010005000000414C
494E45FEFEFF1B1C000F000000FF1D000001000000460100070000004144494C
534F4EFEFEFF1B1C00100000001F001E00FF1D00000100000046010001000000
31FEFEFF1B1C00110000001F001E00FF1D0000010000004601000100000032FE
FEFF1B1C00120000001F001E00FF1D0000010000004601000100000033FEFEFF
1B1C00130000001F001E00FF1D0000010000004601000100000034FEFEFF1B1C
00140000001F001E00FF1D0000010000004601000100000035FEFEFF1B1C0015
0000001F001E00FF1D0000010000004601000100000036FEFEFF1B1C00160000
001F001E00FF1D0000010000004601000100000037FEFEFF1B1C00170000001F
001E00FF1D0000010000004601000100000038FEFEFF1B1C00180000001F001E
00FF1D0000010000004601000100000039FEFEFF1B1C00190000001F001E00FF
1D000001000000460100020000003130FEFEFF1B1C001A0000001F001E00FF1D
000001000000460100020000003131FEFEFF1B1C001B0000001F001E00FF1D00
0001000000460100020000003132FEFEFF1B1C001C0000001F001E00FF1D0000
01000000460100020000003133FEFEFEFEFEFF20FEFF2122002A000000FF23FE
FEFE0E004D0061006E0061006700650072001E00550070006400610074006500
73005200650067006900730074007200790012005400610062006C0065004C00
6900730074000A005400610062006C00650008004E0061006D00650014005300
6F0075007200630065004E0061006D0065000A00540061006200490044002400
45006E0066006F0072006300650043006F006E00730074007200610069006E00
740073001E004D0069006E0069006D0075006D00430061007000610063006900
74007900180043006800650063006B004E006F0074004E0075006C006C001400
43006F006C0075006D006E004C006900730074000C0043006F006C0075006D00
6E00100053006F0075007200630065004900440018006400740041006E007300
690053007400720069006E006700100044006100740061005400790070006500
0800530069007A0065001400530065006100720063006800610062006C006500
120041006C006C006F0077004E0075006C006C00080042006100730065001400
4F0041006C006C006F0077004E0075006C006C0012004F0049006E0055007000
640061007400650010004F0049006E00570068006500720065001A004F007200
6900670069006E0043006F006C004E0061006D006500140053006F0075007200
63006500530069007A0065001C0043006F006E00730074007200610069006E00
74004C00690073007400100056006900650077004C006900730074000E005200
6F0077004C00690073007400060052006F0077000A0052006F00770049004400
10004F0072006900670069006E0061006C0016007200730055006E0063006800
61006E006700650064001A0052006F0077005000720069006F00720053007400
6100740065001800520065006C006100740069006F006E004C00690073007400
1C0055007000640061007400650073004A006F00750072006E0061006C001200
530061007600650050006F0069006E0074000E004300680061006E0067006500
7300}
object MemDataCHECKBOX: TStringField
FieldName = 'CHECKBOX'
Size = 1
end
object MemDataNAME: TStringField
FieldName = 'NAME'
Size = 50
end
end
object DataSource1: TDataSource
DataSet = MemData
Left = 153
Top = 232
end
end
这可能不是最好的解决方案,但它有效:
procedure TForm2.Button1Click(Sender: TObject);
begin
DBGrid1.OnCellClick := nil;
try
if OpenDialog1.Execute then
begin
//
end;
Sleep(100);
Application.ProcessMessages;
finally
DBGrid1.OnCellClick := DBGrid1CellClick;
end;
end;
如果我打开 TDBGrid
上方的 TOpenDialog
并双击 select 一个项目,单击将重定向到 TDBGrid
,同时标记复选框。
要进行测试,请单击按钮并双击以在 OpenDialog 上打开一个文件。第一个复选框将被选中。
Pas 文件
unit Unit2;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Data.DB, FireDAC.Stan.Intf,
FireDAC.Stan.Option, FireDAC.Stan.Param, FireDAC.Stan.Error,
FireDAC.DatS, FireDAC.Phys.Intf, FireDAC.DApt.Intf,
FireDAC.Stan.StorageBin, FireDAC.Comp.DataSet, FireDAC.Comp.Client,
Vcl.StdCtrls, Vcl.ExtCtrls, Vcl.Grids, Vcl.DBGrids;
type
TForm2 = class(TForm)
DBGrid1: TDBGrid;
Panel1: TPanel;
Button1: TButton;
OpenDialog1: TOpenDialog;
MemData: TFDMemTable;
MemDataCHECKBOX: TStringField;
MemDataNAME: TStringField;
DataSource1: TDataSource;
procedure DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
procedure DBGrid1CellClick(Column: TColumn);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form2: TForm2;
implementation
{$R *.dfm}
procedure TForm2.Button1Click(Sender: TObject);
begin
if OpenDialog1.Execute then
Begin
//
End;
end;
procedure TForm2.DBGrid1CellClick(Column: TColumn);
begin
if Column.FieldName = 'CHECKBOX' then
begin
if Column.Grid.DataSource.DataSet.RecordCount > 0 then
Begin
Column.Grid.DataSource.DataSet.Edit;
if Column.Grid.DataSource.DataSet.FieldByName('CHECKBOX').AsString = 'T' then
begin
Column.Grid.DataSource.DataSet.FieldByName('CHECKBOX').AsString := 'F';
end
else
begin
Column.Grid.DataSource.DataSet.FieldByName('CHECKBOX').AsString := 'T';
end;
Column.Grid.DataSource.DataSet.Post;
End;
end;
end;
procedure TForm2.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
var
Check: Integer;
R: TRect;
begin
if not (odd(DBGrid1.DataSource.DataSet.RecNo)) then
begin
DBGrid1.Canvas.Brush.Color := $EBEBEB;
DBGrid1.Canvas.Font.Color := clBlack;
DBGrid1.Canvas.FillRect(Rect);
end;
if (gdSelected in State) or (DBGrid1.SelectedRows.CurrentRowSelected) then
begin
DBGrid1.Canvas.Brush.Color := clHighlight;
DBGrid1.Canvas.Font.Color := clWhite;
DBGrid1.Canvas.FillRect(Rect);
end;
DBGrid1.DefaultDrawColumnCell(Rect, DataCol, Column, State);
if Column.FieldName = 'CHECKBOX' then
begin
if MemData.FieldByName('CHECKBOX').AsString = 'T' then
begin
Check := DFCS_CHECKED;
end
else
Check := 0;
DBGrid1.Canvas.FillRect(Rect);
R := Rect;
InflateRect(R, -2, -2);
DrawFrameControl(DBGrid1.Canvas.handle, R, DFC_BUTTON, DFCS_BUTTONCHECK or Check);
end;
end;
end.
Dfm 文件
object Form2: TForm2
Left = 0
Top = 0
Caption = 'Form2'
ClientHeight = 509
ClientWidth = 756
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'Tahoma'
Font.Style = []
OldCreateOrder = False
Position = poScreenCenter
PixelsPerInch = 96
TextHeight = 13
object DBGrid1: TDBGrid
Left = 0
Top = 41
Width = 756
Height = 468
Align = alClient
DataSource = DataSource1
DefaultDrawing = False
Options = [dgTitles, dgIndicator, dgColumnResize, dgColLines, dgRowLines, dgTabs, dgConfirmDelete, dgCancelOnExit, dgTitleClick, dgTitleHotTrack]
TabOrder = 0
TitleFont.Charset = DEFAULT_CHARSET
TitleFont.Color = clWindowText
TitleFont.Height = -11
TitleFont.Name = 'Tahoma'
TitleFont.Style = []
OnCellClick = DBGrid1CellClick
OnDrawColumnCell = DBGrid1DrawColumnCell
Columns = <
item
Expanded = False
FieldName = 'CHECKBOX'
Visible = True
end
item
Expanded = False
FieldName = 'NAME'
Width = 635
Visible = True
end>
end
object Panel1: TPanel
Left = 0
Top = 0
Width = 756
Height = 41
Align = alTop
BevelInner = bvLowered
TabOrder = 1
ExplicitLeft = 120
ExplicitTop = 16
ExplicitWidth = 185
object Button1: TButton
Left = 14
Top = 8
Width = 75
Height = 25
Caption = 'Open Dialog'
TabOrder = 0
OnClick = Button1Click
end
end
object OpenDialog1: TOpenDialog
Left = 153
Top = 137
end
object MemData: TFDMemTable
Active = True
FieldDefs = <
item
Name = 'CHECKBOX'
DataType = ftString
Size = 1
end
item
Name = 'NAME'
DataType = ftString
Size = 50
end>
IndexDefs = <>
FetchOptions.AssignedValues = [evMode]
FetchOptions.Mode = fmAll
ResourceOptions.AssignedValues = [rvPersistent, rvSilentMode]
ResourceOptions.Persistent = True
ResourceOptions.SilentMode = True
UpdateOptions.AssignedValues = [uvCheckRequired, uvAutoCommitUpdates]
UpdateOptions.CheckRequired = False
UpdateOptions.AutoCommitUpdates = True
StoreDefs = True
Left = 153
Top = 184
Content = {
414442530F00C31DA2040000FF00010001FF02FF0304000E0000004D0065006D
00440061007400610005000A0000005400610062006C00650006000000000007
0000080032000000090000FF0AFF0B04001000000043004800450043004B0042
004F00580005001000000043004800450043004B0042004F0058000C00010000
000E000D000F0001000000100001110001120001130001140001150001160010
00000043004800450043004B0042004F005800170001000000FEFF0B04000800
00004E0041004D0045000500080000004E0041004D0045000C00020000000E00
0D000F0032000000100001110001120001130001140001150001160008000000
4E0041004D004500170032000000FEFEFF18FEFF19FEFF1AFF1B1C0000000000
FF1D000001000000460100040000004A484F4EFEFEFF1B1C0001000000FF1D00
0001000000460100050000004C55434153FEFEFF1B1C0002000000FF1D000001
000000460100050000004B454C4C59FEFEFF1B1C0003000000FF1D0000010000
004601000500000053494C4153FEFEFF1B1C0004000000FF1D00000100000046
0100060000004D4152435553FEFEFF1B1C0005000000FF1D0000010000004601
00050000004A454E4E59FEFEFF1B1C0006000000FF1D00000100000046010004
00000052495441FEFEFF1B1C0007000000FF1D00000100000046010005000000
5045544552FEFEFF1B1C0008000000FF1D000001000000460100080000004245
4E4A414D494EFEFEFF1B1C0009000000FF1D000001000000460100060000004D
4152434941FEFEFF1B1C000A000000FF1D000001000000460100080000005041
545249434941FEFEFF1B1C000B000000FF1D0000010000004601000500000046
4142494FFEFEFF1B1C000C000000FF1D000001000000460100070000004D4152
43454C4FFEFEFF1B1C000D000000FF1D000001000000460100080000004C454F
4E4152444FFEFEFF1B1C000E000000FF1D00000100000046010005000000414C
494E45FEFEFF1B1C000F000000FF1D000001000000460100070000004144494C
534F4EFEFEFF1B1C00100000001F001E00FF1D00000100000046010001000000
31FEFEFF1B1C00110000001F001E00FF1D0000010000004601000100000032FE
FEFF1B1C00120000001F001E00FF1D0000010000004601000100000033FEFEFF
1B1C00130000001F001E00FF1D0000010000004601000100000034FEFEFF1B1C
00140000001F001E00FF1D0000010000004601000100000035FEFEFF1B1C0015
0000001F001E00FF1D0000010000004601000100000036FEFEFF1B1C00160000
001F001E00FF1D0000010000004601000100000037FEFEFF1B1C00170000001F
001E00FF1D0000010000004601000100000038FEFEFF1B1C00180000001F001E
00FF1D0000010000004601000100000039FEFEFF1B1C00190000001F001E00FF
1D000001000000460100020000003130FEFEFF1B1C001A0000001F001E00FF1D
000001000000460100020000003131FEFEFF1B1C001B0000001F001E00FF1D00
0001000000460100020000003132FEFEFF1B1C001C0000001F001E00FF1D0000
01000000460100020000003133FEFEFEFEFEFF20FEFF2122002A000000FF23FE
FEFE0E004D0061006E0061006700650072001E00550070006400610074006500
73005200650067006900730074007200790012005400610062006C0065004C00
6900730074000A005400610062006C00650008004E0061006D00650014005300
6F0075007200630065004E0061006D0065000A00540061006200490044002400
45006E0066006F0072006300650043006F006E00730074007200610069006E00
740073001E004D0069006E0069006D0075006D00430061007000610063006900
74007900180043006800650063006B004E006F0074004E0075006C006C001400
43006F006C0075006D006E004C006900730074000C0043006F006C0075006D00
6E00100053006F0075007200630065004900440018006400740041006E007300
690053007400720069006E006700100044006100740061005400790070006500
0800530069007A0065001400530065006100720063006800610062006C006500
120041006C006C006F0077004E0075006C006C00080042006100730065001400
4F0041006C006C006F0077004E0075006C006C0012004F0049006E0055007000
640061007400650010004F0049006E00570068006500720065001A004F007200
6900670069006E0043006F006C004E0061006D006500140053006F0075007200
63006500530069007A0065001C0043006F006E00730074007200610069006E00
74004C00690073007400100056006900650077004C006900730074000E005200
6F0077004C00690073007400060052006F0077000A0052006F00770049004400
10004F0072006900670069006E0061006C0016007200730055006E0063006800
61006E006700650064001A0052006F0077005000720069006F00720053007400
6100740065001800520065006C006100740069006F006E004C00690073007400
1C0055007000640061007400650073004A006F00750072006E0061006C001200
530061007600650050006F0069006E0074000E004300680061006E0067006500
7300}
object MemDataCHECKBOX: TStringField
FieldName = 'CHECKBOX'
Size = 1
end
object MemDataNAME: TStringField
FieldName = 'NAME'
Size = 50
end
end
object DataSource1: TDataSource
DataSet = MemData
Left = 153
Top = 232
end
end
这可能不是最好的解决方案,但它有效:
procedure TForm2.Button1Click(Sender: TObject);
begin
DBGrid1.OnCellClick := nil;
try
if OpenDialog1.Execute then
begin
//
end;
Sleep(100);
Application.ProcessMessages;
finally
DBGrid1.OnCellClick := DBGrid1CellClick;
end;
end;