日期:2014-05-18 浏览次数:20734 次
this.listBox1.SelectedIndexChanged -= new EventHandler(listBox1_SelectedIndexChanged); this.listBox1.SetSelected(..., ...); // 这样 SetSelected 就不触发事件,在这里执行些别的代码 this.listBox1.SelectedIndexChanged += new EventHandler(listBox1_SelectedIndexChanged);
------解决方案--------------------
// 调用 SetSelected 的代码 this.listBox1.SelectedIndexChanged -= new EventHandler(listBox1_SelectedIndexChanged); this.listBox1.SetSelected(..., ...);// 在这执行 SetSelected this.listBox1_SelectedIndexChanged(null, null); // 直接调用事件方法,等同于触发 this.listBox1.SelectedIndexChanged += new EventHandler(listBox1_SelectedIndexChanged); // 事件方法 private void listBox1_SelectedIndexChanged(object sender, EventArgs e) { if (sender == null) { // 是 SetSelected 触发的 } else { // 是别的触发的 } }
------解决方案--------------------
listBox1_SelectedIndexChanged(object sender, EventArgs e)
时间里面判断键盘鼠标事件,是否是键盘和鼠标触发的,,第三种是代码触发的很好区分