gpt4 book ai didi

c# - Windows Form ComboBox 在应用程序失去焦点时触发 SelectionChangeCommitted、SelectedValueChanged 和 SelectedIndexChanged 事件

转载 作者:太空宇宙 更新时间:2023-11-03 11:45:31 27 4
gpt4 key购买 nike

我有包含组合框的 .Net 2.0 Windows 窗体。我编写了以下代码来填充组合框,然后将其绑定(bind)到 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;

问题

  1. 运行应用程序。
  2. 单击 ComboBox 的下拉箭头但不要选择任何项目。
  3. 按任意键(例如 ALT+TAB、Windows 键等),将焦点从当前 Windows 窗体应用程序转移。

这会引发异常和应用程序崩溃。以下是从“输出”窗口获取的详细信息。

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& m) at System.Windows.Forms.ComboBox.WndProc(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& 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& m) at System.Windows.Forms.Control.WmCommand(Message& m) at System.Windows.Forms.Control.WndProc(Message& m) at System.Windows.Forms.ScrollableControl.WndProc(Message& m) at System.Windows.Forms.ContainerControl.WndProc(Message& m) at System.Windows.Forms.Form.WndProc(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& 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& m) at System.Windows.Forms.Control.DefWndProc(Message& m) at System.Windows.Forms.Control.WmCommand(Message& m) at System.Windows.Forms.Control.WndProc(Message& m) at System.Windows.Forms.ComboBox.WndProc(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) at System.Windows.Forms.UnsafeNativeMethods.PeekMessage(MSG& 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)

我在事件处理程序中添加了一些调试语句来检查事件序列。输出窗口细节如下图:

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).

问题

当应用程序失去焦点且组合框状态为 OPEN 时,为什么 .Net 会触发 SelectionChangeCommittedSelectedValueChangedSelectedIndexChanged 事件?

最佳答案

代码看起来很完美。这个问题可能是因为 .net 框架中的错误。有专家能证实这一点吗?

避免异常的一种解决方法是 - 如果 SelectedValue 为 NULL,则存储“ProductType”属性的默认值。

例如,如果 ProductType 的默认值为 -1,则

cmbProduct.DataBindings["SelectedValue"].DataSourceNullValue = -1; 

希望这对您有所帮助!

罗宾

关于c# - Windows Form ComboBox 在应用程序失去焦点时触发 SelectionChangeCommitted、SelectedValueChanged 和 SelectedIndexChanged 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3529615/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com