一个苦恼的bug,大家帮忙看看
头文件如下:
#define MAX_INTEGER_VAL 0x7fffffff
#define MIN_INTEGER_VAL (-1 * 0x7fffffff)
/*允许设置的最大小数位数*/
#define MAX_ROUND_BIT 9
//---------------------------------------
class PACKAGE TNumEdit : public TRzEdit
{
private:
/*最大值*/
double FMaxVal;
/*最小值*/
double FMinVal;
/*保留小数位数*/
int FRoundBitNum;
protected:
DYNAMIC void __fastcall KeyPress(char &Key);
DYNAMIC void __fastcall WinProc(TMessage& Msg);
DYNAMIC void __fastcall DoExit();
public:
__fastcall TNumEdit(TComponent* Owner);
bool __fastcall Findstr(AnsiString str);
/*将数值格式化的函数*/
String __fastcall FormatNum(double Val);
__published:
__property double MaxVal = {read = FMaxVal, write = FMaxVal};
__property double MinVal = {read = FMinVal, write = FMinVal};
__property int RoundBitNum = {read = FRoundBitNum, write = FRoundBitNum};
};
cpp文件如下:
//---------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "NumEdit.h"
#pragma link "RzEdit"
#pragma package(smart_init)
//---------------------------------------
// ValidCtrCheck is used to assure that the components created do not have
// any pure virtual functions.
//
static inline void ValidCtrCheck(TNumEdit *)
{
new TNumEdit(NULL);
}
//---------------------------------------
__fastcall TNumEdit::TNumEdit(TComponent* Owner)
: TRzEdit(Owner)
{
FMaxVal = MAX_INTEGER_VAL;
FMinVal = MIN_INTEGER_VAL;
FRoundBitNum = MAX_ROUND_BIT;
this->Text = "0.000";
}
//---------------------------------------
namespace Numedit
{
void __fastcall PACKAGE Register()
{
TComponentClass classes[1] = {__classid(TNumEdit)};
RegisterComponents("Additional", classes, 0);
}
}
//---------------------------------------
//---------------------------------------
void __fastcall TNumEdit::KeyPress(char &Key)
{
if(Key == '.')
{
if(Text.Pos('.') > 0 || SelStart == 0)
Key = 0;
}else if(Key == 0x0d)
{
DoExit();
}else if(Key == '-')
{
if(Text.Pos('-') > 0 || SelStart != 0)
Key = 0;
}else if(Key==VK_BACK)
{
}else if(Key >= '0' && Key <= '9')
{
if(Text.Pos('-') > 0 && SelStart == 0 && SelLength == 0)
Key = 0;
}else
Key=0;
TRzEdit::KeyPress(Key);
}
//---------------------------------------
bool __fastcall TNumEdit::Findstr(AnsiString str)
{
for(int i = 0;i < str.Length();i ++)
{
if(str.c_str()[i] == '.')
return True;
}
return False;
}
//---------------------------------------
String __fastcall TNumEdit::FormatNum(double Val)
{
String s_format;
s_format = Format("%%.%df", ARRAYOFCONST(((int)FRoundBitNum)));
return(Format(s_format, ARRAYOFCONST((Val))));
}
//---------------------------------------
void __fastcall TNumEdit::DoExit(void)
{
double dVal;
if(this->Text.Length() == 1 && this->Text.Pos("-") == 1)
{
dVal = 0;
this->Text = FormatNum(dVal);
return;
}
if(Text.IsEmpty())
dVal = 0.0;
else
{
dVal = this->Text.ToDouble();
if(dVal < FMinVal)
dVal = FMinVal;
if(dVal > FMaxVal)
dVal = FMaxVal;
}
this->Text = FormatNum(dVal);
TRzEdit::DoExit();
}
//---------------------------------------
void __fastcall TNumEdit::WinProc(TMessage& Msg)
{
}
//---------------------------------------
问题描述如下:
我继承了一个第三方控件的类,并且设置了2个属性MinVal和MaxVal(最小值和最大值),现在是我用自己控件的时候,能设置MaxVal属性,
不能设置MinVal属性。不明白,2个属性写的一模一样,怎么会一个能设置,一个不能呢???也不是说MinVal完全不能设置,只能设置0以上的值,0或0一下的都能设置。。??何解。。?
[解决办法]
[解决办法]
double dVal;
if((this->Text.Length() == 1 )&& (this->Text.Pos("-") == 1))//加多两个括号试试看.
{
dVal = 0;
this->Text = FormatNum(dVal);
return;
}