c# - 当我从列表 : why half time that's select 1st value as well with 2nd event? 中选择一个值时的 ListBox

标签 c# winforms user-controls listbox

正如标题中所说:

  • 有一个列表框
  • 当我在中选择一个值时
  • 第一个 SelectedValueChanged 事件包含我点击的值
  • 不时有第二个 SelectedValueChanged 事件包含列表中的第一个值

我发现它添加了那段代码:

这是我的设计器包含的内容:

    this.listBox1.SelectedValueChanged += 
new System.EventHandler(this.listBox1_SelectedValueChanged);

这是我的类(class)包含的内容:

public class myControl : UserControl
{

  private void listBox1_SelectedValueChanged(object sender, EventArgs e)
  {
     StackTrace st = new StackTrace(true);
  }                                    /// I put breakpoint that line to look at st
}

我看不到第二个事件的触发位置。堆栈中有大量系统代码,但与我自己的代码没有任何关联。但我可以按需将其传递到这里。

我唯一没有真正掌握的是 UserControl 上下文。也许可以为此做点什么。

这样,您是否知道为什么我点击一次后会接到两次电话?

根据King King的问题编辑:

场景是:

  • 影响 listBox1.DataSource 调用 SelectedValueChanged

    1. listBox1_SelectedValueChanged sender: {SelectedItem = "1st value"} object {System.Windows.Forms.ListBox}
  • 点击 listBox1 中的第三个​​值会调用两次 SelectedValueChanged

    1. listBox1_SelectedValueChanged sender: {SelectedItem = "3rd value"} 对象 {System.Windows.Forms.ListBox}
    2. listBox1_SelectedValueChanged sender: {SelectedItem = "1st value"} object {System.Windows.Forms.ListBox}
  • 之后只选择第一个值

最佳答案

听起来你有 2 个或更多其他事件的问题,要么你正在选择第三个元素并重新加载列表框或类似的东西。不好意思我目前还没有评论权限,但是你可以选择第一个然后第三个然后选择第一个然后你第二次选择第一个后会得到双重事件吗?

关于c# - 当我从列表 : why half time that's select 1st value as well with 2nd event? 中选择一个值时的 ListBox,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18649267/

相关文章:

c# - 从表单应用程序调用控制台应用程序的 `Main` 方法(通过按钮单击事件)

c# - 在设计模式下锁定 .NET 自定义控件中的高度调整大小

c# - NativeWindow 等效项

c# - WinForms:在正确的位置绘制路径

c# - 如何使用另一个类中的方法添加到 ListBox?

silverlight - 为什么 System.Windows.Controls.Button 总是有 10px 的填充?

c# - 我关于在循环中动态创建标签的代码不起作用,只出现一个

c# - 模拟 HttpResponse WriteAsync

c# - .NET 的线程安全缓冲区

c# - ASP.Net Core LogLevel 不工作