Difference between validating and validated

Source: Internet
Author: User

When the validating event occurs, the value has not been actually saved, so it is empty. When the validated event occurs, the value has been saved, so it can be retrieved; A value is equivalent to the prefix, the consequence of a value. That is to say, you can use the validating event to test a prerequisite before the value is saved. If not, cancel the previous operation and use E. cancel; to check the validity of the value itself, use the validated event to obtain the value of this field after the value is saved. This is also why the E parameter of the validated event does not have cancel, because the value has been saved and the action has been completed and cannot be canceled.

In addition, whether it is validating or validated, the control does not lose the focus when an event occurs, so you can operate the control. The Event Sequence of the control is enter, gotfocus, leave, validating, validated, and losefocus. That is to say, the program focus first "enters" the control, and then the control "gets Focus ", then, the focus is "Off" control. At this time, the tasks that focus on the control have not been saved, so the "Verification" is saved and the "verification completed" event is saved, after the aftermath, the control will "lose focus ".

You may often need to check whether the information entered into the windows form is valid. For example, if you have a Textbox Control for a phone number, you can check whether the control contains only the appropriate characters (numbers, Parentheses, and hyphens ). Generally, you can use a regular expression to verify user input data. Before learning about validating, we also need to know the sequence of focus events: Focus events occur in the following order: enter // gotfocus occurred when entering the control // leave occurred when the control received the focus // validating occurred when the input focus left the control // lostfocus occurred when the control data was validated when the validated // lostfocus occurred after the data validation was completed // if the causesvalidation attribute is set to false when the focus is lost, the validating and validated events are canceled. Note: The gotfocus and lostfocus events are low-level focus events associated with wm_killfocus and wm_setfocus Windows messages. You must use the enter and leave events for all controls. If the canceleventargs object's cancel attribute is set to true in the validating event Delegate, all events that occur after the validating event are normally canceled. Verify the content of the control in the operation. You can write code to handle the validating event. In the event handler, test specific conditions (such as the phone number above ). Verification is one of the series of events that occur during processing. If the test fails, the cancel attribute of the canceleventargs of the validating event is set to true. This will cancel the validating event and cause the focus to return to the control (juky_huang note: this will lead to an endless loop, unless the data validation is passed, you can use the following mandatory method to close it ). The actual result is that the user cannot exit the control unless the data is valid. When the data is invalid, the side effect of the control that maintains the focus is that the parent form cannot be closed using any common method to close the form: in the close box, right-click the System Menu displayed in the title bar and call the close method programmatically. However, in some cases, no matter whether the value in the control is valid, you want users to close the form. You can override the verification and close the form that still contains invalid data by creating the closing event handler of the form. In this event, set the cancel attribute to false. This will force close the form. Note: If you use this method to forcibly close the form, any information not saved in the control will be lost. Note that when the mode form is disabled, the control content is not verified. You can still use control verification to lock the focus to the control, but you do not have to consider closing the form. The validating event is triggered when a user changes a certain value of a control, for example, changing the text of A textbox. In general, to activate validating, you need to set the causesvalidation attribute to true. You can check whether the changed value is valid in the validating event. For example, if you specify that textbox text can only enter one or several of the three letters ABC, the validating event will be triggered when the user inputs or changes, you can check textbox in the delegate of the validating event. is text only composed of one or more of the three letters ABC. If yes, you can exit the process without doing anything. If not, you only need to execute this statement: E. Cancel = true to cancel the operation.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.