The difference and application of e.Cancel and e.Handled in C#

 

First of all, not every event's e parameter has the above two properties.

e.Cancel: Gets or sets a value indicating whether the event should be canceled; e.Handled: Gets or sets a value indicating whether the event has been handled.

Here are some of the more common scenarios:

1)e.cancel:

① The window is closed. For example, the user clicks the upper right corner of the window to close it, but a confirmation box pops up in the code to let the user confirm whether he really wants to exit. If the user chooses no, then e.cancel = true;

②DataGridview cell editing, if you want to exclude the editing operation of some columns, you only need to judge in the CellBeginEdit event that if the current cell belongs to the column to be excluded, e.Cancel = true, and the CellEndEdit event will not be executed. ;

③ Make the selection of the specified node of the TreeView control invalid. If the selection of the root node is invalid, you only need to judge in the BeforeSelect event of the TreeView control that if it is the root node, e.Cancel = true, and the NodeMouseClick event of the node will not be executed. .

2) e.Handled: mostly used to filter certain events, such as keyboard keys

①If the text box is required to only input numbers, then when the input is a letter, then e.handled=true;

②DataGridView control will delete the selected row by pressing the Delete key by default. If you want to realize whether to delete the confirmation box when the Delete key is pressed, click "Yes" to delete the corresponding data in the database, and correspondingly delete the selected row of the DataGridview, click "Yes" No" does not delete the corresponding data in the database, nor delete the selected row of the DataGridview, just click "No", e.Handled = true. e.Handled = true is very important. Without this sentence, although the corresponding data in the database can be controlled not to be deleted when "No" is clicked, the selected row in the DataGridview is deleted.

First of all, not every event's e parameter has the above two properties.

e.Cancel: Gets or sets a value indicating whether the event should be canceled; e.Handled: Gets or sets a value indicating whether the event has been handled.

Here are some of the more common scenarios:

1)e.cancel:

① The window is closed. For example, the user clicks the upper right corner of the window to close it, but a confirmation box pops up in the code to let the user confirm whether he really wants to exit. If the user chooses no, then e.cancel = true;

②DataGridview cell editing, if you want to exclude the editing operation of some columns, you only need to judge in the CellBeginEdit event that if the current cell belongs to the column to be excluded, e.Cancel = true, and the CellEndEdit event will not be executed. ;

③ Make the selection of the specified node of the TreeView control invalid. If the selection of the root node is invalid, you only need to judge in the BeforeSelect event of the TreeView control that if it is the root node, e.Cancel = true, and the NodeMouseClick event of the node will not be executed. .

2) e.Handled: mostly used to filter certain events, such as keyboard keys

①If the text box is required to only input numbers, then when the input is a letter, then e.handled=true;

②DataGridView control will delete the selected row by pressing the Delete key by default. If you want to realize whether to delete the confirmation box when the Delete key is pressed, click "Yes" to delete the corresponding data in the database, and correspondingly delete the selected row of the DataGridview, click "Yes" No" does not delete the corresponding data in the database, nor delete the selected row of the DataGridview, just click "No", e.Handled = true. e.Handled = true is very important. Without this sentence, although the corresponding data in the database can be controlled not to be deleted when "No" is clicked, the selected row in the DataGridview is deleted.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324844175&siteId=291194637