C# 当应用程序失去焦点时,Windows窗体组合框触发SelectionChangeCommitted、SelectedValueChanged和SelectedIndexChanged事件
C# 当应用程序失去焦点时,Windows窗体组合框触发SelectionChangeCommitted、SelectedValueChanged和SelectedIndexChanged事件,c#,winforms,events,combobox,.net-2.0,C#,Winforms,Events,Combobox,.net 2.0,我有.NET2.0Windows窗体,其中包含combobxes。我编写了以下代码来填充combobox,然后将其绑定到Int类型的ProductType属性
// Populate Combo
cmbProduct.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
cmbProduct.DataSource = new DataView(productDataSet.Tables[0]);
cmbProduct.
我有.NET2.0Windows窗体,其中包含combobxes。我编写了以下代码来填充combobox,然后将其绑定到Int类型的ProductType属性
// Populate Combo
cmbProduct.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
cmbProduct.DataSource = new DataView(productDataSet.Tables[0]);
cmbProduct.DisplayMember = displayColumnName_;
cmbProduct.ValueMember = idColumnaName_;
// Add Databindings
cmbProduct.DataBindings.Add("SelectedValue", this, "ProductType").DataSourceUpdateMode = DataSourceUpdateMode.OnPropertyChanged;
问题
运行应用程序李>
单击组合框的下拉箭头,但不要选择任何项目
按任意键(如ALT+TAB、Windows键等),将焦点从当前Windows窗体应用程序转移
这会引发异常和应用程序崩溃。以下是从“输出”窗口获取的详细信息
发生了“System.ArgumentException”类型的第一次意外异常
在System.Windows.Forms.dll中,无法创建类型为“System.DBNull”的对象
已转换为类型“System.Int32”。在
System.ComponentModel.ReflectPropertyDescriptor.SetValue(对象
组件(对象值)
位于System.Windows.Forms.BindToObject.SetValue(对象值)
位于System.Windows.Forms.Binding.PullData(布尔格式,布尔强制)
在System.Windows.Forms.Binding.Target\u PropertyChanged(对象发送方,事件参数e)
在System.EventHandler.Invoke(对象发送方,EventArgs e)
位于System.Windows.Forms.ListControl.OnSelectedValueChanged(EventArgs e)
在System.Windows.Forms.ComboBox.OnSelectedValueChanged(EventArgs e)中
在System.Windows.Forms.ComboBox.OnSelectedIndexChanged(EventArgs e)中
在System.Windows.Forms.ComboBox.WmReflectCommand(Message&m)中
在System.Windows.Forms.ComboBox.WndProc(Message&m)中
在System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message&m)中
在System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&m)中
在System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd、Int32 msg、IntPtr wparam、IntPtr lparam)
在System.Windows.Forms.UnsafentiveMethods.SendMessage(HandleRef hWnd,
Int32 msg、IntPtr wParam、IntPtr lParam)
位于System.Windows.Forms.Control.SendMessage(Int32 msg、IntPtr wparam、IntPtr lparam)
在System.Windows.Forms.Control.ReflectMessageInternal(IntPtr hWnd,Message&m)中
at System.Windows.Forms.Control.WmCommand(Message&m)
位于System.Windows.Forms.Control.WndProc(Message&m)
在System.Windows.Forms.ScrollableControl.WndProc(Message&m)中
在System.Windows.Forms.ContainerControl.WndProc(Message&m)中
在System.Windows.Forms.Form.WndProc(Message&m)中
在System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message&m)中
在System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&m)中
在System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd、Int32 msg、IntPtr wparam、IntPtr lparam)
在System.Windows.Forms.UnsafentiveMethods.CallWindowProc(IntPtr)中
wndProc、IntPtr hWnd、Int32 msg、IntPtr wParam、IntPtr lParam)
在System.Windows.Forms.NativeWindow.DefWndProc(Message&m)中
位于System.Windows.Forms.Control.DefWndProc(Message&m)
at System.Windows.Forms.Control.WmCommand(Message&m)
位于System.Windows.Forms.Control.WndProc(Message&m)
在System.Windows.Forms.ComboBox.WndProc(Message&m)中
在System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message&m)中
在System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&m)中
在System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd、Int32 msg、IntPtr wparam、IntPtr lparam)
在System.Windows.Forms.UnsafentiveMethods.PeekMessage(MSG&MSG,HandleRef hwnd,Int32 msgMin,Int32 msgMax,Int32 remove)
在System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafentiveMethods.IMsoComponentManager.FPushMessageLoop(Int32)中
dwComponentID、Int32原因、Int32数据)
位于System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32
原因(应用程序上下文上下文)
位于System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32
原因(应用程序上下文上下文)
在System.Windows.Forms.Application.Run(Form mainForm)中
我在事件处理程序中添加了一些调试语句来检查事件序列。
输出窗口的详细信息如下所示:
cmbProduct_SelectionChangeCommitted occured - SelectedValue is NULL
cmbProduct_SelectedValueChanged occured - New SelectedValue is NULL
The thread 0x1728 has exited with code 0 (0x0).
cmbProduct_SelectedIndexChanged occured - New SelectedIndex is -1
The thread 0x1250 has exited with code 0 (0x0).
问题
当应用程序在combobox状态为OPEN时失去焦点时,为什么.Net会触发SelectionChangeCommitted
、SelectedValueChanged
和SelectedIndexChanged
事件?代码看起来很完美。这个问题可能是由于.net framework中的错误造成的。有专家能证实这一点吗
避免异常的一个解决方法是,如果SelectedValue为NULL,则存储“ProductType”属性的默认值
例如,如果ProductType的默认值为-1
cmbProduct.DataBindings["SelectedValue"].DataSourceNullValue = -1;
希望,这有帮助
Robin听起来你问了一个错误的问题:“为什么组合会提交焦点丢失?”而不是“为什么会抛出异常?”
罗宾正确地回答了重要的问题
用户不需要执行您提供的复杂的3个步骤来查看异常-仅选择有问题的(null)索引就会导致异常