我有两种类型的文件在程序中处理,打算在打开文件时通过单选按钮指定类型。
于是写个类继承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时,编译通过,运行时出现内存冲突的错误。不指定,运行时又报没有父窗口的错误。
怎么办?困扰我一上午了。

解决方案 »

  1.   

    经过试验,发现TRadioGroup不行,而TRadioButton可以。可能是因为TRadioGroup控件实现的方法必须要某个特定属性。下面的代码可以显示Radio。你可以自己加 Group。TNewOpenDialog = class (TOpenDialog)
    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;
      

  2.   

    TOpenDialog是不可见的元件
    不能当成容器
    它不过是封装了调用文件对话框的接口
    并不是你看到的文件对话框-_-!!!object OpenDialog1: TOpenDialog
      Filter = 'Text files(*.txt)|*.txt|Bitmap files(*.bmp)|*.bmp'
    end建议你采用Filter属性,让用户通过文件类型下拉框来选择
    参考FilterIndex属性
      

  3.   

    keyz的方法试了,但是我就是没法将两个radiobutton Group起来,但是还是解决了一部分问题,zswang说出了原理,赞。