在按键事件中将处理设置为 true 后返回多余的

本文关键字:true 返回 多余 设置 处理 事件 | 更新日期: 2023-09-27 18:33:18

下面的代码中是否需要"return":

if (((e.KeyChar == '1') && (String.IsNullOrWhiteSpace(textBoxPterodactylNum1.Text))) ||
    ((e.KeyChar == '2') && (String.IsNullOrWhiteSpace(textBoxPterodactylNum2.Text))) ||
    ((e.KeyChar == '3') && (String.IsNullOrWhiteSpace(textBoxPterodactylNum3.Text)))) {
    e.Handled = true;
    MessageBox.Show(String.Format(
        "There is no corresponding value for {0} in the pterodactyl TextBox above", e.KeyChar));
    return;
}
. . .
// other code follows if gauntlet above is passed through safely; don't want to continue if it didn't, though (if handled was set to true)

在按键事件中将处理设置为 true 后返回多余的

Handled 属性不会停止事件在其轨道中的执行,它只是告诉冒泡事件你已经处理了它,并且不再希望处理任何其他内容。因此,e.Handled = true下面的代码可能会将其逆转为错误状态,从而允许冒泡事件继续处理事件。

它本质上并不是多余的。您尚未显示下面的代码,但该代码可能会设置e.Handled = false