Today I want to share all of you one basic control of MFC which is inherit from CEdit ctrl,
this control can help you to take care of data type verification, for example, once you define the data type to be "int" (the naming inside control is "EN_DATATYPE_INTEGER"), no matter you have input any character in, it will just recognize the allowed char by data type, the others will be reject, if over range, it's will warning you automatically.
Is it useful?
let's take a look about this control. :)
Definition:
enum enDataType {
EN_DATATYPE_ALPHABET = 0, // Standard C++ naming rule
EN_DATATYPE_INTEGER = 1, // int
EN_DATATYPE_UNSIGNED_INTEGER = 2, // unsigned int
EN_DATATYPE_DOUBLE = 3, // double
EN_DATATYPE_UNSIGNED_DOUBLE = 4, // unsigned double
EN_DATATYPE_NONE = 5 // original CEdit behavior
};
From now, You just need to call SetEditDataType(EN_DATATYPE_INTEGER), then the data type of CEditCtrl will become "int" and no longer support the other data types anymore.
Till now, I believe as smart as you should understand why I can said this control can support many data types.
One place you have to read it more detail is OnChar(),
This function block is defined and qualified the input rule of CEditCtrl class, so you should be read it very carefully to understand the know how of it.
How to use it?
Here I support 2 way for you,
1. Using DDX_Control to manage it
2. Use SubclassDlgItem()
below is the source code:
EditCtrl.h
=======================================================
#pragma once
// CEditCtrl
namespace nsAuxLib {
class CEditCtrl : public CEdit
{
DECLARE_DYNAMIC(CEditCtrl)
// This is define how many data types can be judge by this control
enum enDataType {
EN_DATATYPE_ALPHABET = 0,
EN_DATATYPE_INTEGER = 1,
EN_DATATYPE_UNSIGNED_INTEGER = 2,
EN_DATATYPE_DOUBLE = 3,
EN_DATATYPE_UNSIGNED_DOUBLE = 4,
EN_DATATYPE_NONE = 5
};
public:
// Constructor and Destructor
CEditCtrl();
virtual ~CEditCtrl();
private:
enDataType edatatype_;
CString maxvalue_;
CString minvalue_;
CString defaultvalue_;
public:
// Define the current data type for the current object
inline void SetEditDataType(enDataType eType)
{ edatatype_ = eType; }
// Define the initial values of default value,
// minimun value and maximun value
inline void SetMinMaxValue(CString strDefault,
CString strMin,
CString strMax)
{
defaultvalue_ = strDefault;
maxvalue_ = strMax;
minvalue_ = strMin;
}
// Check Range function can return the result status back
bool CheckRange(CString strMin, CString strMax);
bool CheckRange();
double GetValDouble(bool bool_unsighed = true);
int GetValInt(bool bool_unsighed = true);
void SetValDouble(double dblvalue, bool bool_unsighed = true);
void SetValInt(int intvalue, bool bool_unsighed = true);
CString GetValText();
enDataType GetEditDatatype()
{ return edatatype_; }
protected:
DECLARE_MESSAGE_MAP()
public:
// Event handlers table
afx_msg void OnChar(UINT nChar, UINT nRepCnt, UINT nFlags);
afx_msg void OnEnChange();
afx_msg void OnEnKillfocus();
};
}
=======================================================
If you are interesting about this control, you can email to me,
I will send it to you a.s.a.p.
Cheers,
Andrew
沒有留言:
張貼留言