August 4th Saturday (八月 四日 土曜日)

本文介绍了一种名为验证器的概念,它通过简化数据验证流程来帮助开发者更轻松地创建对话框。验证器可以实现数据在C++变量与控件之间的双向传输,并能拦截控件产生的事件以提供过滤行为。
  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), and mediates between C++
data and the control, transferring the data in either direction
and validating it. It also is able to intercept events generated
by the control, providing filtering behaviour without the need to
derive a new control class.

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 kind of validation required, and perhaps a pointer to
a C++ variable that is used for storing the data for the control. If
such a variable address is not supplied by the user, then the validator
should store the data internally.

  The wxValidator::Validate member function should return true if the
data in the control (not the C++ variable) is valid. It should also show
an appropriate message if data was not valid.

  The wxValidator::TransferToWindow member function should transfer the
data from the validator or associated C++ variable to the control.

  The wxValidator::TransferFromWindow member function should transfer the
data from the control to the validator or associated C++ variable.

  There should be a copy constructor, and a wxValidator::Clone function which
returns a copy of the validator object. This is important because validators
are 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 handlers will capture events before the control itself does.

How validators interact with dialogs

  For validators to work correctly, validator functions must be called at the
right times during dialog initialisation and dismissal.

  When a wxDialog::Show is called (for a modeless dialog) or wxDialog::ShowModal
is called (for a modal dialog), the function wxWindow::InitDialog is automatically
called. This in turn sends an initialisation event to the dialog.  The default handler
for the wxEVT_INIT_DIALOG event is defined in the wxWindow class to simply call
the function wxWindow::TransferDataToWindow. This function finds all the validators
in the window's children and calls the TransferToWindow function for each.  Thus,
data is transferred from C++ variables to the dialog just as the dialog is being shown.

--------------------------------------------------------------------------------
  If you are using a window or panel instead of a dialog, you will need to call
  wxWindow::InitDialog explicitly before showing the window.
--------------------------------------------------------------------------------

  When the user clicks on a button, for example the OK button, the application should
first call wxWindow::Validate, which returns false if any of the child window validators
failed to validate the window data. The button handler should return immediately if validation
failed.  Secondly, the application should call wxWindow::TransferDataFromWindow and return
if this failed. It is then safe to end the dialog by calling EndModal (if modal) or Show
(if modeless).

void wxDialog::OnOK(wxCommandEvent& event)
{
    if ( Validate() && TransferDataFromWindow() )
    {
        if ( IsModal() )
            EndModal(wxID_OK);
        else
        {
            SetReturnCode(wxID_OK);
            this->Show(false);
        }
    }
}

  So if using validators and a normal OK button, you may not even need to write any code
for handling dialog dismissal.

  If you load your dialog from a resource file, you will need to iterate through the controls
setting validators, since validators can't be specified in a dialog resource.

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值