⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tvalidat.tex

📁 很牛的GUI源码wxWidgets-2.8.0.zip 可在多种平台下运行.
💻 TEX
字号:
\section{wxValidator overview}\label{validatoroverview}Classes: \helpref{wxValidator}{wxvalidator}, \helpref{wxTextValidator}{wxtextvalidator}, \helpref{wxGenericValidator}{wxgenericvalidator}The aim of the validator concept is to make dialogs very much easier to write.A validator is an object that can be plugged into a control (such as a wxTextCtrl), andmediates between C++ data and the control, transferring the data in either directionand validating it. It also is able to intercept events generatedby the control, providing filtering behaviour without the need to derive a new control class.You can use a stock validator, such as \helpref{wxTextValidator}{wxtextvalidator} (which does textcontrol data transfer, validation and filtering) and \helpref{wxGenericValidator}{wxgenericvalidator} (which does data transfer for a range of controls);or you can write your own.\wxheading{Example}Here is an example of wxTextValidator usage.\begin{verbatim}  wxTextCtrl *txt1 = new wxTextCtrl(this, -1, wxT(""),    wxPoint(10, 10), wxSize(100, 80), 0,    wxTextValidator(wxFILTER_ALPHA, &g_data.m_string));\end{verbatim}In this example, the text validator object provides the following functionality:\begin{enumerate}\itemsep=0pt\item It transfers the value of g\_data.m\_string (a wxString variable) to the wxTextCtrl whenthe dialog is initialised.\item It transfers the wxTextCtrl data back to this variable when the dialog is dismissed.\item It filters input characters so that only alphabetic characters are allowed.\end{enumerate}The validation and filtering of input is accomplished in two ways. When a character is input,wxTextValidator checks the character against the allowed filter flag (wxFILTER\_ALPHA in this case). Ifthe character is inappropriate, it is vetoed (does not appear) and a warning beep sounds.The second type of validation is performed when the dialog is about to be dismissed, so ifthe default string contained invalid characters already, a dialog box is shown giving theerror, and the dialog is not dismissed.\wxheading{Anatomy of a validator}A programmer creating a new validator class should provide the following functionality.A validator constructor is responsible for allowing the programmer to specify the kindof validation required, and perhaps a pointer to a C++ variable that is used for storing thedata for the control. If such a variable address is not supplied by the user, thenthe validator should store the data internally.The \helpref{wxValidator::Validate}{wxvalidatorvalidate} member function should returntrue if the data in the control (not the C++ variable) is valid. It should also showan appropriate message if data was not valid.The \helpref{wxValidator::TransferToWindow}{wxvalidatortransfertowindow} member function shouldtransfer the data from the validator or associated C++ variable to the control.The \helpref{wxValidator::TransferFromWindow}{wxvalidatortransferfromwindow} member function shouldtransfer the data from the control to the validator or associated C++ variable.There should be a copy constructor, and a \helpref{wxValidator::Clone}{wxvalidatorclone} functionwhich returns a copy of the validator object. This is important because validatorsare passed by reference to window constructors, and must therefore be cloned internally.You can optionally define event handlers for the validator, to implement filtering. These handlerswill capture events before the control itself does.For an example implementation, see the valtext.h and valtext.cpp files in the wxWidgets library.\wxheading{How validators interact with dialogs}For validators to work correctly, validator functions must be called at the right times duringdialog initialisation and dismissal.When a \helpref{wxDialog::Show}{wxdialogshow} is called (for a modeless dialog)or \helpref{wxDialog::ShowModal}{wxdialogshowmodal} is called (for a modal dialog),the function \helpref{wxWindow::InitDialog}{wxwindowinitdialog} is automatically called.This in turn sends an initialisation event to the dialog. The default handler forthe wxEVT\_INIT\_DIALOG event is defined in the wxWindow class to simply callthe function \helpref{wxWindow::TransferDataToWindow}{wxwindowtransferdatatowindow}. Thisfunction finds all the validators in the window's children and calls the TransferToWindowfunction for each. Thus, data is transferred from C++ variables to the dialogjust as the dialog is being shown.\normalbox{If you are using a window or panel instead of a dialog, you will need tocall \helpref{wxWindow::InitDialog}{wxwindowinitdialog} explicitly before showing thewindow.}When the user clicks on a button, for example the OK button, the application shouldfirst call \helpref{wxWindow::Validate}{wxwindowvalidate}, which returns false ifany of the child window validators failed to validate the window data. The button handlershould return immediately if validation failed. Secondly, the application shouldcall \helpref{wxWindow::TransferDataFromWindow}{wxwindowtransferdatafromwindow} andreturn if this failed. It is then safe to end the dialog by calling EndModal (if modal)or Show (if modeless).In fact, wxDialog contains a default command event handler for the wxID\_OK button. It goes likethis:\begin{verbatim}void wxDialog::OnOK(wxCommandEvent& event){    if ( Validate() && TransferDataFromWindow() )    {        if ( IsModal() )            EndModal(wxID_OK);        else        {            SetReturnCode(wxID_OK);            this->Show(false);        }    }}\end{verbatim}So if using validators and a normal OK button, you may not even need to write anycode for handling dialog dismissal.If you load your dialog from a resource file, you will need to iterate through the controlssetting validators, since validators can't be specified in a dialog resource.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -