我有包含组合框的.Net 2.0 Windows窗体 . 我编写了以下代码来填充组合框,然后将其绑定到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;
ISSUE
运行应用程序 .
单击ComboBox的下拉箭头但不要选择任何项目 .
按任意键(例如ALT TAB,Windows Key等),这将使焦点从当前的Windows表单应用程序转移 .
这会抛出异常和应用程序崩溃 . 以下是从“输出”窗口获取的详细信息 .
System.Windows.Forms.dll中出现“System.ArgumentException”类型的第一次机会异常“System.DBNull”类型的对象无法转换为“System.Int32”类型 . System.Windows上System.Windows.Forms.Binding.PullData(Boolean reformat,Boolean force)的System.Windows.Forms.BindToObject.SetValue(Object value)中的System.ComponentModel.ReflectPropertyDescriptor.SetValue(Object component,Object value) System.Windows.Forms.ComboBox.OnSelectedValueChanged中System.Windows.Forms.ListControl.OnSelectedValueChanged(EventArgs e)的System.EventHandler.Invoke(Object sender,EventArgs e)中的.Forms.Binding.Target_PropertyChanged(Object sender,EventArgs e) (EventArgs e)位于System.Windows上System.Windows.Forms.ComboBox.WndProc(Message&m)的System.Windows.Forms.ComboBox.WmReflectCommand(Message&m)的System.Windows.Forms.ComboBox.OnSelectedIndexChanged(EventArgs e)在System.Windows.Forms.NativeWindow.DebuggableCallback的System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&m)中的.Forms.Control.ControlNativeWindow.OnMessage(Message&m)(IntPtr hWnd,Int32 msg,IntPtr wparam,IntPtr lparam )在System.Windows.Forms.UnsafeNativeMethods.SendMess System.Windows.Forms.Control.ReflectMessageInternal(IntPtr hWnd,Message&m)中的System.Windows.Forms.Control.SendMessage(Int32 msg,IntPtr wparam,IntPtr lparam)的age(HandleRef hWnd,Int32 msg,IntPtr wParam,IntPtr lParam) )System.Windows.Forms.Forms.Conm.PndProc(Message&m)的System.Windows.Forms.Control.WmCommand(Message&m)位于System.Windows.Forms的System.Windows.Forms.ScrollableControl.WndProc(Message&m)处 . System.Windows.Fornd.VandProc上的System.Windows.Forms.Form.WndProc(Message&m)中的SystemControl.WndProc(Message&m)位于System.Windows.Forms.Control.ControlNativeWindow.WndProc的System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message&m)消息&m)System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd,Int32 msg,IntPtr wparam,IntPtr lparam)在System.Windows.Forms.UnsafeNativeMethods.CallWindowProc(IntPtr wndProc,IntPtr hWnd,Int32 msg,IntPtr wParam,IntPtr)在System.Windows.Forms.Control.DefWndProc的System.Windows.Forms.NativeWindow.DefWndProc(Message&m)处的lParam) m)System.Windows.Fornd.And处于System.Windows.Fornd.CandProc(Message&m)的System.Windows.Forms.Control.WndProc(Message&m)处的System.Windows.Forms.Control.WmCommand(Message&m)位于System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd,Int32 msg,IntPtr wparam,IntPtr lparam)的System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&m)的.Control.ControlNativeWindow.OnMessage(Message&m)at at在System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID)中的System.Windows.Forms.UnsafeNativeMethods.PeekMessage(MSG&msg,HandleRef hwnd,Int32 msgMin,Int32 msgMax,Int32 remove) System32上的System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason,ApplicationContext context),System.Windows上的System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason,ApplicationContext context)中的,Int32 reason,Int32 pvLoopData . Forms.Application.Run(Form mainForm )
我在事件处理程序中添加了一些调试语句来检查事件序列 . 输出窗口deatails如下所示:
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).
Question
当应用程序在组合框状态下失去焦点时,为什么.Net会触发 SelectionChangeCommitted , SelectedValueChanged 和 SelectedIndexChanged 事件?