Note that there are some explanatory texts on larger screens.

plurals
  1. POWindows Form ComboBox fires SelectionChangeCommitted, SelectedValueChanged and SelectedIndexChanged event when application loses focus
    text
    copied!<p>I have .Net 2.0 Windows form containing combobxes. I have written following code to populate combobox and then binding it to ProductType property of type Int.</p> <pre><code>// 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; </code></pre> <p><strong>ISSUE</strong></p> <ol> <li>Run application. </li> <li>Click on ComboBox's drop-down arrow but do not select any item.</li> <li>Press any keys (such as ALT+TAB, Windows Key etc) which will shift focus from current Windows form application.</li> </ol> <p>This throws Exception and application crashes. Following are the details taken from Output window.</p> <blockquote> <p>A first chance exception of type 'System.ArgumentException' occurred in System.Windows.Forms.dll Object of type 'System.DBNull' cannot be converted to type 'System.Int32'. at System.ComponentModel.ReflectPropertyDescriptor.SetValue(Object component, Object value) at System.Windows.Forms.BindToObject.SetValue(Object value) at System.Windows.Forms.Binding.PullData(Boolean reformat, Boolean force) at System.Windows.Forms.Binding.Target_PropertyChanged(Object sender, EventArgs e) at System.EventHandler.Invoke(Object sender, EventArgs e) at System.Windows.Forms.ListControl.OnSelectedValueChanged(EventArgs e) at System.Windows.Forms.ComboBox.OnSelectedValueChanged(EventArgs e) at System.Windows.Forms.ComboBox.OnSelectedIndexChanged(EventArgs e) at System.Windows.Forms.ComboBox.WmReflectCommand(Message&amp; m) at System.Windows.Forms.ComboBox.WndProc(Message&amp; m) at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message&amp; m) at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&amp; m) at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) at System.Windows.Forms.UnsafeNativeMethods.SendMessage(HandleRef hWnd, Int32 msg, IntPtr wParam, IntPtr lParam) at System.Windows.Forms.Control.SendMessage(Int32 msg, IntPtr wparam, IntPtr lparam) at System.Windows.Forms.Control.ReflectMessageInternal(IntPtr hWnd, Message&amp; m) at System.Windows.Forms.Control.WmCommand(Message&amp; m) at System.Windows.Forms.Control.WndProc(Message&amp; m) at System.Windows.Forms.ScrollableControl.WndProc(Message&amp; m) at System.Windows.Forms.ContainerControl.WndProc(Message&amp; m) at System.Windows.Forms.Form.WndProc(Message&amp; m) at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message&amp; m) at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&amp; m) at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) at System.Windows.Forms.UnsafeNativeMethods.CallWindowProc(IntPtr wndProc, IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam) at System.Windows.Forms.NativeWindow.DefWndProc(Message&amp; m) at System.Windows.Forms.Control.DefWndProc(Message&amp; m) at System.Windows.Forms.Control.WmCommand(Message&amp; m) at System.Windows.Forms.Control.WndProc(Message&amp; m) at System.Windows.Forms.ComboBox.WndProc(Message&amp; m) at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message&amp; m) at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&amp; m) at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) at System.Windows.Forms.UnsafeNativeMethods.PeekMessage(MSG&amp; msg, HandleRef hwnd, Int32 msgMin, Int32 msgMax, Int32 remove) at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData) at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context) at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context) at System.Windows.Forms.Application.Run(Form mainForm)</p> </blockquote> <p>I added some debug statment in event handlers to check the event sequence. The output window deatails are as shown below :</p> <pre><code>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). </code></pre> <p><strong>Question</strong> </p> <p>Why .Net fires <code>SelectionChangeCommitted</code>, <code>SelectedValueChanged</code> and <code>SelectedIndexChanged</code> events when the application loses its focus with combobox state is OPEN ?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload