在C#中,e.Cancel和e.Handled是事件参数中的两个属性,用于控制事件的执行和传播。
e.Cancel属性:
private void Button_Click(object sender, EventArgs e) { if (someCondition) { e.Cancel = true; // 取消事件的执行 } }
e.Handled属性:
private void Button_Click(object sender, EventArgs e) { if (someCondition) { e.Handled = true; // 停止事件的传播 } } private void Button_Click2(object sender, EventArgs e) { // 这个事件处理程序将不会被调用,因为前面的事件处理程序将事件的传播停止了。 }
总结: