我有两种类型的文件在程序中处理,打算在打开文件时通过单选按钮指定类型。
于是写个类继承TOpenDialog,以下是我的代码:
type
TNewOpenDialog = class (TOpenDialog)
private
FRadioGroup : TRadioGroup;
public
constructor Create(AOwner: TComponent); override;
function Execute: Boolean; override;
end;
implementationconstructor TNewOpenDialog.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FRadioGroup := TRadioGroup.Create(Self);
with FRadioGroup do
begin
Name := 'TestRadioGroup';
//Parent := TWinControl(Self); // Error
SetBounds(210,10,100,100);
Items.Add('A类数据'); Items.Add('B类数据');
ItemIndex := 0;
end;
end;function TNewOpenDialog.Execute;
begin
if NewStyleControls and not (ofOldStyleDialog in Options) then
Template := 'DLGTEMPLATE' else
Template := nil;
Result := inherited Execute;
end;问题就出在那个parent指定的地方,我想单选框的parent就应该是新类型的文件打开对话框,结果指定为self时,编译通过,运行时出现内存冲突的错误。不指定,运行时又报没有父窗口的错误。
怎么办?困扰我一上午了。
于是写个类继承TOpenDialog,以下是我的代码:
type
TNewOpenDialog = class (TOpenDialog)
private
FRadioGroup : TRadioGroup;
public
constructor Create(AOwner: TComponent); override;
function Execute: Boolean; override;
end;
implementationconstructor TNewOpenDialog.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FRadioGroup := TRadioGroup.Create(Self);
with FRadioGroup do
begin
Name := 'TestRadioGroup';
//Parent := TWinControl(Self); // Error
SetBounds(210,10,100,100);
Items.Add('A类数据'); Items.Add('B类数据');
ItemIndex := 0;
end;
end;function TNewOpenDialog.Execute;
begin
if NewStyleControls and not (ofOldStyleDialog in Options) then
Template := 'DLGTEMPLATE' else
Template := nil;
Result := inherited Execute;
end;问题就出在那个parent指定的地方,我想单选框的parent就应该是新类型的文件打开对话框,结果指定为self时,编译通过,运行时出现内存冲突的错误。不指定,运行时又报没有父窗口的错误。
怎么办?困扰我一上午了。
private
FItem1,FItem2:TRadioButton;
published
procedure DoShow; override;
public
constructor Create(AOwner: TComponent); override;
function Execute: Boolean; override;
end;
constructor TNewOpenDialog.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FItem1:=TRadiobutton.Create(Self);
FItem1.Caption:='A类数据';
FItem2:=TRadiobutton.Create(Self);
FItem2.Caption:='B类数据';
end;procedure TNewOpenDialog.DoShow;
var
Rect,StaticRect:TRect;
begin
GetClientRect(Handle, Rect);
StaticRect := GetStaticRect;
Rect.Left := StaticRect.Left + (StaticRect.Right - StaticRect.Left); Rect.Bottom:=Rect.Top + 20;
FItem1.BoundsRect:=Rect;
Inc(Rect.Top,20);
Inc(Rect.Bottom,20);
FItem2.BoundsRect:=Rect;
FItem1.ParentWindow:=Handle;
FItem2.ParentWindow:=Handle; inherited;end;function TNewOpenDialog.Execute;
begin
if NewStyleControls and not (ofOldStyleDialog in Options) then
Template := 'DLGTEMPLATE' else
Template := nil;
Result := inherited Execute;
end;
不能当成容器
它不过是封装了调用文件对话框的接口
并不是你看到的文件对话框-_-!!!object OpenDialog1: TOpenDialog
Filter = 'Text files(*.txt)|*.txt|Bitmap files(*.bmp)|*.bmp'
end建议你采用Filter属性,让用户通过文件类型下拉框来选择
参考FilterIndex属性