Dodataexchange (cdataexchange * PDX) is an important function of MFC cwnd.
In this function, you can use a series of ddx_xxxx (...) functions to implement data interaction between the UI and data, and use ddv_xxx (...) for data verification. For example:
// Simple text operations
Void
Afxapi
Ddx_text (cdataexchange *
PDX, int NIDC,
Byte & value); // special control types
Void
Afxapi
Ddx_check (cdataexchange *
PDX, int NIDC,
Int & value );
// Number of characters
Void
Afxapi
Ddv_maxchars (cdataexchange *
PDX, cstring
Const & value,
Int nchars );
....
These DDV may not meet our needs. However, you can use the cdataexchange parameter PDX to verify your data in dodataexchange. Use PDX-> m_bsaveandvalidate to determine whether updatedata (true) or updatedata (false ). If the verification fails, PDX-> fail () is triggered.
// Cdataexchange-for data exchange and validation
Class
Cdataexchange
{
// Attributes
Public
: Bool
M_bsaveandvalidate;
// True => Save and validate data
// Operations (for implementors of DDX and DDV procs)
Cwnd *
M_pdlgwnd;
// Container usually a dialog
Hwnd
Preparectrl (int
NIDC );
Hwnd
Prepareeditctrl (int
NIDC );
Void
Fail ();
// Will throw exception
Cdataexchange (cwnd *
Pdlgwnd,
Bool
Bsaveandvalidate );
Colecontrolsite *
Prepareolectrl (int
NIDC );
// For OLE controls in Dialog
// Implementation
Uint
M_idlastcontrol;
// Last control used (for validation)
Bool
M_beditlastcontrol;
// Last control was an edit item
};
It is worth noting that the preparexxx function. When data verification fails, the focus can be restored to the failed control. The custom data verification function can have the following structure:
Template <class T>
Void ddv_myvalidate (cdataexchange *
PDX, uint nctrlid, T & Data ...)
{
PDX-> preparectrl (nctrlid );
If (PDX-> m_bsaveandvalidate)
{
Bool bvalid = .....;
If (! Bvalid)
PDX-> fail ();
Else
Data =...; // get data from UI
}
Else
{
// Set data to contorl
}
}
Dodataexchange (cdataexchange * PDX) is an important function of MFC cwnd.
In this function, you can use a series of ddx_xxxx (...) functions to implement data interaction between the UI and data, and use ddv_xxx (...) for data verification. For example:
// Simple text operations
Void
Afxapi
Ddx_text (cdataexchange *
PDX, int NIDC,
Byte & value); // special control types
Void
Afxapi
Ddx_check (cdataexchange *
PDX, int NIDC,
Int & value );
// Number of characters
Void
Afxapi
Ddv_maxchars (cdataexchange *
PDX, cstring
Const & value,
Int nchars );
....
These DDV may not meet our needs. However, you can use the cdataexchange parameter PDX to verify your data in dodataexchange. Use PDX-> m_bsaveandvalidate to determine whether updatedata (true) or updatedata (false ). If the verification fails, PDX-> fail () is triggered.
// Cdataexchange-for data exchange and validation
Class
Cdataexchange
{
// Attributes
Public
: Bool
M_bsaveandvalidate;
// True => Save and validate data
// Operations (for implementors of DDX and DDV procs)
Cwnd *
M_pdlgwnd;
// Container usually a dialog
Hwnd
Preparectrl (int
NIDC );
Hwnd
Prepareeditctrl (int
NIDC );
Void
Fail ();
// Will throw exception
Cdataexchange (cwnd *
Pdlgwnd,
Bool
Bsaveandvalidate );
Colecontrolsite *
Prepareolectrl (int
NIDC );
// For OLE controls in Dialog
// Implementation
Uint
M_idlastcontrol;
// Last control used (for validation)
Bool
M_beditlastcontrol;
// Last control was an edit item
};
It is worth noting that the preparexxx function. When data verification fails, the focus can be restored to the failed control. The custom data verification function can have the following structure:
Template <class T>
Void ddv_myvalidate (cdataexchange *
PDX, uint nctrlid, T & Data ...)
{
PDX-> preparectrl (nctrlid );
If (PDX-> m_bsaveandvalidate)
{
Bool bvalid = .....;
If (! Bvalid)
PDX-> fail ();
Else
Data =...; // get data from UI
}
Else
{
// Set data to contorl
}
}