制作组件中碰到的属性编辑器问题:
以下问题均附代码:
问题1:
以实现整型的下拉列表,但如何实现字符串值的下拉列表。
2.如何实现当用户选中DATASOURCE,然后DATAFIELD中马上显示出字段呢?就像DATA CONTROL页面上的控件一样,选中一个数据源,接着DATAFIELD中就显示出该数据源的字段呢?
3.如果在自已制作的控件中有一字符串属性(如:FileName),我想通过对话框来设置,但我写的代码不能实现。
我写的代码如下:
DataCBX.h File
//---------------------------------------------------------------------------
#ifndef DataComBoxH
#define DataComBoxH
//---------------------------------------------------------------------------
#include <SysUtils.hpp>
#include <Controls.hpp>
#include <Classes.hpp>
#include <Forms.hpp>
#include <StdCtrls.hpp>
#include <dsgnintf.hpp>
#include <db.hpp>
#include <forms.hpp>
#include <dialogs.hpp>
//---------------------------------------------------------------------------
class PACKAGE TDataComBox : public TComboBox
{
private:
TDataSource* FDataSource;
TField *FDataField;
int FBaudRate;
AnsiString FFileName;
protected:
public:
void __fastcall SetDataSource(TDataSource* DataSource);
void __fastcall SetDataField(TField* Field);
void __fastcall SetBaudRate(int BaudRate);
void __fastcall SetFileName(AnsiString FileName);
__fastcall TDataComBox(TComponent* Owner);
__published:
__property TDataSource* DataSource={read=FDataSource,write=SetDataSource};
__property TField* DataField={read=FDataField,write=SetDataField};
__property int BaudRate={read=FBaudRate,write=SetBaudRate,default=9600};
__property AnsiString FileName={read=FFileName,write=SetFileName};
};
//The following PropertyEditor is used by control to generate a listbox
class TBaudRateProperty:public TIntegerProperty
{
public:
TPropertyAttributes __fastcall GetAttributes()
{
return TPropertyAttributes()<<paValueList;
}
void __fastcall GetValues(TGetStrProc Proc)
{
Proc("9600");
Proc("14400");
Proc("28800");
Proc("32400");
Proc("57600");
}
};
//The following PropertyEditor is used by control to generate a listbox
class TFileNameProperty:public TStringProperty
{
public:
TPropertyAttributes __fastcall GetAttributes()
{
return TPropertyAttributes()<<paDialog;
}
void __fastcall Edit()
{
TOpenDialog *O=new TOpenDialog(Application);
O->Title="Choose File";
O->InitialDir="c:\\";
O->FileName=GetValue();
if(O->Execute())
{
Value=O->FileName;
}
delete O;
}
};
//---------------------------------------------------------------------------
#endif
.cpp File
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "DataComBox.h"
#pragma package(smart_init)
//---------------------------------------------------------------------------
// ValidCtrCheck is used to assure that the components created do not have
// any pure virtual functions.
//
static inline void ValidCtrCheck(TDataComBox *)
{
new TDataComBox(NULL);
}
//---------------------------------------------------------------------------
__fastcall TDataComBox::TDataComBox(TComponent* Owner)
: TComboBox(Owner)
{
}
//---------------------------------------------------------------------------
TTypeInfo *AnsiStringInfo() //You know,the macro __typeinfo is only used to obtain typeinfo of vcl not common datatype,so we should use a function to achieve
{
TTypeInfo *pTypeInfo=new TTypeInfo;
pTypeInfo->Name="AnsiString";
pTypeInfo->Kind=tkString;
return pTypeInfo;
}
//---------------------------------------------------------------------------
TTypeInfo *IntInfo()
{
TTypeInfo *pTypeInfo=new TTypeInfo;
pTypeInfo->Kind=tkInteger;
pTypeInfo->Name="int";
return pTypeInfo;
}
//----------------------------The namespace is used by compiler to recoganize control with their name---------------
namespace Datacombox
{
void __fastcall PACKAGE Register()
{
TComponentClass classes[1] = {__classid(TDataComBox)};
RegisterComponents("AKing", classes, 0);
RegisterPropertyEditor(IntInfo(),__classid(TDataComBox),"BaudRate",__classid(TBaudRateProperty));
RegisterPropertyEditor(AnsiStringInfo(),__classid(TDataComBox),"FileName",__classid(TFileNameProperty));
}
}
//---------------------------------------------------------------------------
void __fastcall TDataComBox::SetDataSource(TDataSource* DataSource)
{
FDataSource=DataSource;
}
//-------------------------------------------------------
void __fastcall TDataComBox::SetDataField(TField* Field)
{
FDataField=Field;
}
//-----------------------------------------------------
void __fastcall TDataComBox::SetBaudRate(int BaudRate)
{
FBaudRate=BaudRate;
}
//----------------------------------------------------
void __fastcall TDataComBox::SetFileName(AnsiString FileName)
{
FFileName=FileName;
}