線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:2067
推到 Plurk!
推到 Facebook!

繼承TDBLookupComboBox物件,編譯無錯誤,表單使用出現Has no Parent window.

缺席
g9221712
高階會員


發表:145
回覆:344
積分:162
註冊:2006-07-06

發送簡訊給我
#1 引用回覆 回覆 發表時間:2007-01-29 05:50:59 IP:220.134.xxx.xxx 訂閱
各位前輩和版主大人:
我欲繼承TDBLookupComboBox,這個物件,之前使用TDBEDIT來開發自訂vcl元件都沒有錯誤,但改用TDBLookupComboBox卻出現
Has no Parent window.錯誤訊息,不知道要怎麼改正! 懇請大家幫忙! 謝謝!
unit TUCombobox_;
interface
uses
DateUtils, Forms, Windows, Messages, SysUtils, Classes, Controls, StdCtrls, DBCtrls, Dialogs, Buttons, DB, Graphics, ADODB, Variants;

type
TUCombobox = class(TWinControl)
private
{ Private declarations }
Fparent: Variant;
UTDBLookupComboBox: TDBLookupComboBox;

protected
{ Protected declarations }
procedure CreateParams(var Params: TCreateParams); override;

//---------------------------------------------------------
function Get_Datasource: TDatasource;
procedure Set_Datasource(aDS: TDatasource);
//---------------------------------------------------------
function Get_DataField: string;
procedure Set_DataField(aField: string);
//---------------------------------------------------------
procedure Loaded; override;


public
{ Public declarations }
procedure Setfocus; override;
constructor Create(Aowner: TComponent); override;
destructor Destroy; override;
published
{ Published declarations }
property Datasource: TDatasource read Get_Datasource write Set_Datasource;
property DataField: string read Get_DataField write Set_DataField;
end;
procedure Register;
implementation
uses TYaoCalendar_PAS, Math;
procedure TUCombobox.CreateParams(var Params: TCreateParams);
begin
inherited;
UTDBLookupComboBox.Parent := self;
end;

constructor TUCombobox.Create(AOwner: TComponent); // 建構子、建立物件初始狀態
begin
inherited Create(AOwner);
UTDBLookupComboBox := TDBLookupComboBox.Create(Self);
UTDBLookupComboBox.parent := self;
UTDBLookupComboBox.width := 100;
UTDBLookupComboBox.Height := 20;
UTDBLookupComboBox.TOP := 0;
UTDBLookupComboBox.Font.Size := 12;
UTDBLookupComboBox.Font.Name := '新細明體';
UTDBLookupComboBox.DataSource := nil;
UTDBLookupComboBox.DataField := '';
self.Height := 25;
self.Width := 240;
end;
destructor TUCombobox.Destroy;
begin
UTDBLookupComboBox.Free;
inherited Destroy;
end;
procedure Register;
begin
RegisterComponents('YAOS', [TUCombobox]);
end;
procedure TUCombobox.Loaded;
begin
inherited Loaded;
end;
procedure TUCombobox.Setfocus();
begin
UTDBLookupComboBox.SetFocus;
end;
function TUCombobox.Get_Datasource: TDatasource;
begin
Result := UTDBLookupComboBox.DataSource;
end;
procedure TUCombobox.Set_Datasource(aDS: TDatasource);
begin
if (aDS <> UTDBLookupComboBox.Datasource) then
begin
UTDBLookupComboBox.DataSource := aDS;
end;
if aDS = nil then
begin
UTDBLookupComboBox.DataField := '';
UTDBLookupComboBox.DataSource := nil;
end;
end;
//==============================================================================
function TUCombobox.Get_DataField: string;
begin
Result := UTDBLookupComboBox.DataField; ;
end;
procedure TUCombobox.Set_DataField(aField: string);
var
i: Integer;
begin
if (UTDBLookupComboBox.DataField <> aField) then
begin
UTDBLookupComboBox.DataField := aField;
end;
end;
end.

------
「人們所以覺得寂寞,是因為他們會築牆,卻不會搭橋。」
程式寫的越久,卻發現自己越來越不會寫程式!
g9221712
高階會員


發表:145
回覆:344
積分:162
註冊:2006-07-06

發送簡訊給我
#2 引用回覆 回覆 發表時間:2007-01-29 06:22:04 IP:220.134.xxx.xxx 訂閱
我看了原始vcl的定義,TDBLookupComboBox元件好像還有很多子元件,不知道是否我繼承時還需要指定子元件的Parent 
constructor TDBLookupComboBox.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
ControlStyle := ControlStyle [csReplicatable];
Width := 145;
Height := 0;
FDataList := TPopupDataList.Create(Self);
FDataList.Visible := False;
FDataList.Parent := Self;
FDataList.OnMouseUp := ListMouseUp;
FButtonWidth := GetSystemMetrics(SM_CXVSCROLL);
FDropDownRows := 7;
end;

昏到中,這個我會用就有鬼啦!救人啊!
------
「人們所以覺得寂寞,是因為他們會築牆,卻不會搭橋。」
程式寫的越久,卻發現自己越來越不會寫程式!
g9221712
高階會員


發表:145
回覆:344
積分:162
註冊:2006-07-06

發送簡訊給我
#3 引用回覆 回覆 發表時間:2007-01-29 08:29:42 IP:220.134.xxx.xxx 訂閱
我在表單上動態建立是OK的啊,不知道少什麼?
procedure TFRM_USER.Button1Click(Sender: TObject);
var
tb: TDBLookupComboBox;
begin
tb := TDBLookupComboBox.Create(self);
tb.Parent := self;
tb.Top := 50;
tb.Width := 200;
tb.DataSource := self.DS1;
tb.DataField := '員工編號';
tb.ListSource := self.DataSource1;
tb.ListField := '姓名';
tb.KeyField := '員工編號';

end;

------
「人們所以覺得寂寞,是因為他們會築牆,卻不會搭橋。」
程式寫的越久,卻發現自己越來越不會寫程式!
g9221712
高階會員


發表:145
回覆:344
積分:162
註冊:2006-07-06

發送簡訊給我
#4 引用回覆 回覆 發表時間:2007-01-29 09:12:21 IP:220.134.xxx.xxx 訂閱
  改成如下也是不行:真是想破頭了!  大家幫幫忙!
constructor TUCombobox.Create(AOwner: TComponent); // 建構子、建立物件初始狀態
begin
inherited Create(AOwner);
if AOwner is TForm then
begin
Set_Parent(TForm(AOwner).Name);
end;

UTDBLookupComboBox := TDBLookupComboBox.Create(Self);
UTDBLookupComboBox.parent := self;
UTDBLookupComboBox.ParentWindow := tform(Application.FindComponent(TForm(AOwner).Name)).Handle;
UTDBLookupComboBox.width := 100;
UTDBLookupComboBox.Height := 20;
UTDBLookupComboBox.TOP := 0;
UTDBLookupComboBox.Font.Size := 12;
UTDBLookupComboBox.Font.Name := '新細明體';
UTDBLookupComboBox.DataSource := nil;
UTDBLookupComboBox.DataField := '';
self.Height := 25;
self.Width := 240;
end;

------
「人們所以覺得寂寞,是因為他們會築牆,卻不會搭橋。」
程式寫的越久,卻發現自己越來越不會寫程式!
hagar
版主


發表:143
回覆:4056
積分:4445
註冊:2002-04-14

發送簡訊給我
#5 引用回覆 回覆 發表時間:2007-01-29 17:58:10 IP:59.124.xxx.xxx 未訂閱
為何不用如下的方式繼承就好了?
Private
// …
published
// …

end;
g9221712
高階會員


發表:145
回覆:344
積分:162
註冊:2006-07-06

發送簡訊給我
#6 引用回覆 回覆 發表時間:2007-01-29 18:31:38 IP:220.134.xxx.xxx 訂閱
因為實際使用上,我是用複合繼承,也就是元件內有多個元件!
我也知道單一繼承沒有問題,真是怪哉!
------
「人們所以覺得寂寞,是因為他們會築牆,卻不會搭橋。」
程式寫的越久,卻發現自己越來越不會寫程式!
g9221712
高階會員


發表:145
回覆:344
積分:162
註冊:2006-07-06

發送簡訊給我
#7 引用回覆 回覆 發表時間:2007-01-29 23:36:22 IP:220.134.xxx.xxx 訂閱
我又試了,在表單上動態建立TDBComboBox,原來我不會,不知道怎麼建立才正確
var
tudbCombobox: TDBComboBox;
begin

tudbCombobox := TDBComboBox.Create(self);
tudbCombobox.DataField := '員工編號';
tudbCombobox.DataSource := DS1;
tudbCombobox.Parent := FRM_USER;
tudbCombobox.CreateParented(FRM_USER.WindowHandle);

end;

拖拉式開發太久,真的不會寫啦! Has no Parent window
------
「人們所以覺得寂寞,是因為他們會築牆,卻不會搭橋。」
程式寫的越久,卻發現自己越來越不會寫程式!
g9221712
高階會員


發表:145
回覆:344
積分:162
註冊:2006-07-06

發送簡訊給我
#8 引用回覆 回覆 發表時間:2007-01-30 02:53:03 IP:220.134.xxx.xxx 訂閱
表單上動態建立TDBComboBox改成如下程式碼就ok!但是若寫成物件還是一樣失敗http://delphi.ktop.com.tw/board.php?cid=31&fid=97&tid=86526

var
tudbCombobox: TDBComboBox;
begin

tudbCombobox := TDBComboBox.Create(self);
tudbCombobox.Parent := FRM_USER;
tudbCombobox.CreateParented(FRM_USER.WindowHandle);

tudbCombobox.DataField := '員工編號';
tudbCombobox.DataSource := DS1;

end;
拖拉式開發太久,真的不會寫啦! Has no Parent window

------
「人們所以覺得寂寞,是因為他們會築牆,卻不會搭橋。」
程式寫的越久,卻發現自己越來越不會寫程式!
hagar
版主


發表:143
回覆:4056
積分:4445
註冊:2002-04-14

發送簡訊給我
#9 引用回覆 回覆 發表時間:2007-01-31 08:50:09 IP:59.124.xxx.xxx 未訂閱
拿掉藍色的部份, 改成紅色的部份試試
把 Create TDBLookupComboBox 的部份放到 CreateWnd
type
TUCombobox = class(TWinControl)
private
{ Private declarations }
UTDBLookupComboBox: TDBLookupComboBox;
Fparent: string;

protected
{ Protected declarations }
//procedure CreateParams(var Params: TCreateParams); override;
//---------------------------------------------------------
function Get_Parent: string; // 傳回值
procedure Set_Parent(Value: string); // 設定值,不傳回值
//---------------------------------------------------------
function Get_Datasource: TDatasource;
procedure Set_Datasource(aDS: TDatasource);
//---------------------------------------------------------
function Get_DataField: string;
procedure Set_DataField(aField: string);
//---------------------------------------------------------
procedure Loaded; override;

procedure CreateWnd; override;
public
{ Public declarations }
procedure Setfocus; override;
constructor Create(Aowner: TComponent); override;
destructor Destroy; override;

published
{ Published declarations }
property Datasource: TDatasource read Get_Datasource write Set_Datasource;
property DataField: string read Get_DataField write Set_DataField;
property RunParent: string read Get_Parent write Set_Parent;

end;
procedure Register;
implementation
{procedure TUCombobox.CreateParams(var Params: TCreateParams);
begin
inherited;
// UTDBLookupComboBox.Parent := self;
// UTDBLookupComboBox.Parent := tform(Get_DataField); // 亂下的
// UTDBLookupComboBox.ParentWindow := tform(Get_DataField).Handle; // 亂下的

end;
}

constructor TUCombobox.Create(AOwner: TComponent); // 建構子、建立物件初始狀態
begin
inherited Create(AOwner);

// 設定上層物件
// ================================
//if AOwner is TForm then
//begin
// Set_Parent(TForm(AOwner).Name);
//end;

self.Height := 25;
self.Width := 240;
end;
destructor TUCombobox.Destroy;
begin
UTDBLookupComboBox.Free;
inherited Destroy;
end;

procedure Register;
begin
RegisterComponents('YAOS', [TUCombobox]);
end;

procedure TUCombobox.Loaded;
begin
inherited Loaded;
end;

procedure TUCombobox.Setfocus();
begin
UTDBLookupComboBox.SetFocus;
end;

function TUCombobox.Get_Datasource: TDatasource;
begin
Result := UTDBLookupComboBox.DataSource;
end;

procedure TUCombobox.Set_Datasource(aDS: TDatasource);
begin
if (aDS <> UTDBLookupComboBox.Datasource) then
begin
UTDBLookupComboBox.DataSource := aDS;
end;
if aDS = nil then
begin
UTDBLookupComboBox.DataField := '';
UTDBLookupComboBox.DataSource := nil;
end;
end;
//==============================================================================

function TUCombobox.Get_DataField: string;
begin
Result := UTDBLookupComboBox.DataField; ;
end;

procedure TUCombobox.Set_DataField(aField: string);
begin
if (UTDBLookupComboBox.DataField <> aField) then
begin
UTDBLookupComboBox.DataField := aField;
end;
end;

//==============================================================================
function TUCombobox.Get_Parent: string;
begin
Result := Fparent;
end;

procedure TUCombobox.Set_Parent(Value: string);
begin
if Fparent <> Value then
begin
Fparent := Value;
end;
end;

procedure TUCombobox.CreateWnd;
begin
inherited;

UTDBLookupComboBox := TDBLookupComboBox.Create(Self);
UTDBLookupComboBox.Parent := Self;
UTDBLookupComboBox.DataSource := nil;
UTDBLookupComboBox.DataField := '';
UTDBLookupComboBox.width := 100;
UTDBLookupComboBox.Height := 20;
UTDBLookupComboBox.TOP := 0;
UTDBLookupComboBox.Font.Size := 12;
UTDBLookupComboBox.Font.Name := '新細明體';
end;

end.

g9221712
高階會員


發表:145
回覆:344
積分:162
註冊:2006-07-06

發送簡訊給我
#10 引用回覆 回覆 發表時間:2007-01-31 13:12:18 IP:220.134.xxx.xxx 訂閱
前輩:
我照您的方式改,編譯可以過,但是實際上在IDE上拖放的時候,會出現執行的錯誤~
CreateWnd跟Create不知道差別在哪裡! 那如果複合的元件,不知道要如何寫,
因為需要計算物件的顯示位置啊~假設UTDBLookupComboBox前面有一個Lable
懇請給予解答,感謝您了~

------
「人們所以覺得寂寞,是因為他們會築牆,卻不會搭橋。」
程式寫的越久,卻發現自己越來越不會寫程式!
danny
版主


發表:100
回覆:522
積分:595
註冊:2002-03-11

發送簡訊給我
#11 引用回覆 回覆 發表時間:2007-01-31 20:35:42 IP:211.76.xxx.xxx 訂閱
可視元件一般是從 TCustomControl 繼承來作, 這樣比較簡單. 除非必要一般不會從 TWinControl 繼承.
另外, 不可視元件一般是TComponent 繼承.
也就是說選擇繼承的父類別, 會影響你元件的處理複雜度.
因此你的元件建議從 TCustomControl 繼承來作, 你試看看 ...
------
將問題盡快結案也是一種禮貌!
danny
版主


發表:100
回覆:522
積分:595
註冊:2002-03-11

發送簡訊給我
#12 引用回覆 回覆 發表時間:2007-01-31 20:45:22 IP:211.76.xxx.xxx 訂閱
CreateWnd跟Create不知道差別在哪裡!? 那如果複合的元件,不知道要如何寫,
CreateWnd 是處理 Delphi 元件與 Windows 對應的關係, Create 是包裝好的 Delphi 元件的處理方式, 這也就是為何用 Delphi 開發元件比較簡單(也許吧)的原因.
其他請參考 #11 的說明.
------
將問題盡快結案也是一種禮貌!
g9221712
高階會員


發表:145
回覆:344
積分:162
註冊:2006-07-06

發送簡訊給我
#13 引用回覆 回覆 發表時間:2007-02-15 02:09:06 IP:220.134.xxx.xxx 訂閱
後來經過朋友的協助!原來是繼承InfoPower 元件,去設定InfoPower 元件的Font Size和Font Name ,就會導致這樣的錯誤!

我將設定InfoPower 元件的Font Size和Font Name 拿掉,就可以了! 真是怪啊!
------
「人們所以覺得寂寞,是因為他們會築牆,卻不會搭橋。」
程式寫的越久,卻發現自己越來越不會寫程式!
syntax
尊榮會員


發表:26
回覆:1139
積分:1258
註冊:2002-04-23

發送簡訊給我
#14 引用回覆 回覆 發表時間:2007-02-16 21:05:54 IP:61.64.xxx.xxx 訂閱

不是 設定InfoPower 元件的Font Size和Font Name 的問題

是必須在 設定InfoPower 元件的Font Size和Font Name 前就已有建立 parent window,也就是 parent 要先指定好,且是有 window ctrl 的 parent
這樣 設定InfoPower 元件的Font Size和Font Name 也不會錯

g9221712
高階會員


發表:145
回覆:344
積分:162
註冊:2006-07-06

發送簡訊給我
#15 引用回覆 回覆 發表時間:2007-02-16 21:39:27 IP:220.132.xxx.xxx 訂閱
前輩:

您說的我了解!但是我卻是將Font Size和Font Name 敘述拿掉後,就解決no Parent window.的錯誤訊息!

難不成Infopower有特殊的用法!

目前我是可以正常使用中,只註解 // Font Size和Font Name 敘述 兩個敘述!

不知道到底是啥問題!
------
「人們所以覺得寂寞,是因為他們會築牆,卻不會搭橋。」
程式寫的越久,卻發現自己越來越不會寫程式!
系統時間:2024-04-28 16:15:31
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!