C++ Builder __property 默认值问题

kingeboy 2007-12-29 07:18:19
大哥现身来帮忙一下吧,问题是这样的

我自写的组件里面定义了一个整行的属性如:

private:
int m_nValue;
void __fastcall SetPro(int nValue)
{
m_nValue = nValue;
}
__published:
__property int Value={read=m_nValue,write=SetPro};

在构造函数里面赋了个值比如100,然后我在设计模式的时候在窗体上放了这个控件.
然后在设计模式的Object Inspector面板中把值改为0(成功),设计模式时一切正常可以在面板上看到Value=0,然后编译程序,这时在SetPro函数处下断点,再调试看这个属性的时候却还是100,就是BC根本没有调用SetPro()函数来设置变量的值,但如果我在设计模式中把属性的值设为非0的数值的时候,Value的值就会在运行时被改变.
如果程序运行的时候显式调用Value=0;是可以改变属性值的
我试了几种数据类型,都是一样,好像是BC在程序运行时状态的时候,判断如果属性被设置的值为0时,就不动态改变此属性的值,自动屏蔽掉了设置操作一样,请问这是BC的问题还是我哪里设置不对啊?
...全文
409 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
kingeboy 2008-01-03
  • 打赏
  • 举报
回复
我不是在构造函数中给赋了100了嘛,我设这个属性主要是当改变其值后要在某个地方使用的,如果每次在设计模式中给了0,就恢复成初始值,那能起什么用.
加Nodefault也不行的,是不是不支持浮点数的属性啊
stherix 2008-01-03
  • 打赏
  • 举报
回复
加个 nodefault 看看
ydlchina 2008-01-03
  • 打赏
  • 举报
回复
你那个defaut只是要在ObjectInspector中显示,然后它没有任何作用,你需要在构造函数中手工赋值给m_dValue = 10;
kingeboy 2008-01-03
  • 打赏
  • 举报
回复
我把代码改成使用浮点数的时候出现问题的样子吧
先在自定义组件的类里面定义一个浮点数变量
double m_dValue;

//定义设置变量值的函数
void __fastcall SetValue(double Value)
{
m_dValue = Value;
}
然后定义一个类的属性,就是可以在ObjectInspector面板看到并设置值的变量,并按照"stherix "同学的方法加上default默认值.
__property double Value={read=m_dValue,write=SetValue,default=10};
如果不使用函数设置变量的值用下面的方法定义也可以
__property double Value={read=m_dValue,write=m_dValue,default=10};
然后在类的构造函数中把m_dValue的值初始化为100;
m_dValue = 100;

现在我们编译这个自定义组件,然后把它放入到测试程序的窗体上,在ObjectInspector面板可以看到Value属性的默认值为100,把值修改为0回车,值成功改变为0,然后编译运行测试程序,调试查看此自定义组件对象Value属性的值,发现还是100.
如果在上一步把Value的值改为一个非0的值,再编译运行程序,会发现Value属性的值正确改变成我们实际上修改后的值,就是不会变回构造函数中初始化的值.

如果把数据类型改为整形就不会出现这种问题,哪位DX能说一下该怎么办啊?
laowang2 2008-01-03
  • 打赏
  • 举报
回复
顶7楼
kingeboy 2008-01-02
  • 打赏
  • 举报
回复
顶啊,期待有人能解答浮点数属性怎么用默认值
CACACACACA 2008-01-02
  • 打赏
  • 举报
回复
来点代码, DELPHI下没有这个问题. 机器上没有BCB.
我不懂电脑 2008-01-02
  • 打赏
  • 举报
回复
他的默认值只是让Object Inspector看到是这个值
实际的值需要在构造函数里初始化为这个值。
e_board 2008-01-01
  • 打赏
  • 举报
回复
写错了,于这个无关,sorry
e_board 2008-01-01
  • 打赏
  • 举报
回复
把__published:应该写到public底下吧,这样写应该没有问题的。
我来看看CB 2007-12-31
  • 打赏
  • 举报
回复
支持
kingeboy 2007-12-29
  • 打赏
  • 举报
回复
default我设过了对double类型的属性值没有用
stherix 2007-12-29
  • 打赏
  • 举报
回复
请参考property 的default指引符
Ehlib 是著名的数据库连接控制,版本为5.0.13,DBgrid增强VCL控件;支持多表头,多固定列,按表头排序,支持合计列,并支持直接打印。可以和PB的ataWindow媲美。 本版本含完整源代码,支持以下 IDE: Delphi 5,6,7,2005 C++Builder 5,6 BDS 2006 (Delphi 2006, C++Builder 2006) Delphi 2007 RAD Studio 2009 (Delphi 2009, C++Builder 2009) Embarcadero RAD Studio 2010 (Delphi 2010, C++Builder 2010) 本人已在Delphi 2010中运行通过,在本人编制的进销存软件中应用完全正常。 注:Delphi2010里面安装不要修改bpl文件的输出路径,采用默认值,否则安装难以成功。 Version 5.0的改进: + At design-time grid can show such components as DataSource, DataSet, DataDriver in its the bottom left part. These components can be selected in object inspector by mouse clicking. It allows to find quickly and select components which is connected to the grid. + New mode - RowPanel. In this mode grid allows to show out cells of record one under another and change height of every cell separately. + Grid can have special panel of detail information for record. It is allowed to put any control from component palette on this panel. At run time, panel is hidden and user can open it by pressing on a '+' sign which is shown at the begining of every row. + Grid can scroll data smoothly and by small steps of one pixel size. Grid can scroll data smoothly vertically and horizontally. + Hot track. Grid can highlight a cell or a row under mouse cursor. + Grid can show vertical line in gradient mode between data rows and bottom part of the grid. + In mode Column.AlwaysShowEditButton grid shows EditButtons in shadow mode. It doesn't irritate eyes by big amount of edit buttons in the grid. + New property EditButtonsShowOptions is added in the Grid of type TEditButtonsShowOptionsEh. TEditButtonsShowOptionsEh = set of (sebShowOnlyForCurCellEh, sebShowOnlyForCurRowEh, sebShowOnlyWhenGridActiveEh, sebShowOnlyWhenDataEditingEh); When Column.AlwaysShowEditButton is True, this property allows to customize, in which cases it is need to show EditButtons. : See detail information
Ehlib 是著名的数据库连接控制,版本为5.2.84,DBgrid增强VCL控件;支持多表头,多固定列,按表头排序,支持合计列,并支持直接打印。可以和PB的ataWindow媲美。 本版本含完整源代码,支持以下 IDE: Delphi 5,6,7,2005 C++Builder 5,6 BDS 2006 (Delphi 2006, C++Builder 2006) Delphi 2007 RAD Studio 2009 (Delphi 2009, C++Builder 2009) Embarcadero RAD Studio 2010 (Delphi 2010, C++Builder 2010) 本人已在Delphi XE中运行通过,在本人编制的进销存软件中应用完全正常。 注:Delphi 2010、XE里面安装不要修改bpl文件的输出路径,采用默认值,否则安装难以成功。 Version 5.2 + Added feature to group data in the DBGridEh. It is allowed to make grouping at run-time and design-time. Grouping works only when the grid is connected to TMemTableEh dataset. Use next subproperties of Grid.DataGrouping property to adjust grouping Active: Boolean - Set this propery true to active grouping. DefaultStateExpanded: Boolean - defines initial expapnding/collapsing state of new created elements of the grouping tree. GroupLevels: TGridDataGroupLevelsEh - Collection of group levels. Use this property to create template of the grouping. GroupPanelVisible: Boolean - Set this property to True to show special panel at the top part of the grid. Then the panel is thisible it is allowed to form group levels by mouse. Drag title of the requaried column to the group panel and drop it onto panel. Font: TFont - Controls the attributes of the default font of group records in the grid. Color: Tcolor - Default background color of the group records. See demonstration project of grouping in Demos\DataGrouping directory. Version 5.1 * The type of RowPanel property was changed. Instead of Boolean type, now it is an object property with subproperies: Active: Boolean - Defines if it is possible to place cells under each over and if every propetry can have personal hight. + NavKeysNavigationType: TGridRowPanelNavKeysNavigationTypeEh - defines the order of navigation over cells in the grid when keys Up, Down, Left, Right are pressed. rpntRowT

604

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder VCL组件使用和开发
社区管理员
  • VCL组件使用和开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧