代碼示例:你是真的對(duì)Delphi很了解么?
PRocedure StepEditor( strgrid: TStringGrid; Step: TStep );
var
sValue, sField: string;
EditorClass: TStepEditorClass;
Editor: TStepEditor;
begin
sField := strgrid.Cells[0, strgrid.Selection.Top];
sValue := strgrid.Cells[1, strgrid.Selection.Top];
EditorClass := EditorClassList.Editors[ sField ];
Editor := EditorClass.Create;
Editor.Field := sField;
Editor.Step := Step;
Editor.Edit( sValue );
Editor.Free;
strgrid.Cells[ 1, strgrid.Selection.Top ] := sValue;
end;
EditorClass 是一個(gè)Class of Class, 也就是類的類
比如
TFormClass = Class of TForm;
但是不同于:TFormClass = Class( TForm ); 這是兩個(gè)概念!
而 EditorClassList 里面存放的就是 類的類的列表;
Editor := EditorClass.Create;
Create是類方法,而不是對(duì)象方法,所以可以由 EditorClass來創(chuàng)建EditorClass的一個(gè)實(shí)例
補(bǔ)充:
TStepEditor = Class( TObject )
...
End;
TStepEditorClass = Class of TStepEditor;
Object Inspector 為什么能夠提供一個(gè)方便的編輯環(huán)境?
為什么不同的字段,供選擇的值不一樣,校驗(yàn)的方式不一樣,彈出的編輯框
不一樣?因?yàn)楦鶕?jù)不同的字段類型,注冊(cè)了不同的屬性編輯器 Propety Editor;
簡(jiǎn)化Delphi提供的注冊(cè)屬性編輯器的函數(shù),可以描述為以下:
RegisteryPropertyEditor( PropertyFieldType, EditorClass );
^此處為類型名,如 Bool , Integer, ...等等
^此處為對(duì)應(yīng)的編輯器的類名,注意,不是類名的字符描述
實(shí)際運(yùn)行的時(shí)候,用戶點(diǎn)擊Object Inspector 的一個(gè)字段的時(shí)候,
Delphi內(nèi)部就搜索該字段類型對(duì)應(yīng)的編輯器類;然后由找到的類的類,創(chuàng)建該類的一個(gè)實(shí)例;
進(jìn)行相關(guān)的操作(決定是否有下拉框,是否有一個(gè)按鈕等等)
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注