C# “我的表单上的所有按钮似乎都在触发”;“什么?”;

C# “我的表单上的所有按钮似乎都在触发”;“什么?”;,c#,asp.net,vb.net,C#,Asp.net,Vb.net,我有一个windows窗体,其中所有控件都是动态创建的 然而,所有的按钮似乎都在点击事件之前触发了“某物”,而“某物”导致我的组合框的selectedindexchanged被触发。我已经彻底检查了声明的事件处理程序,并确保没有按钮作为其事件进行了SIC路径 我注意到,用鼠标点击按钮的正常行为是,当点击按钮但按住并释放按钮外的区域时,按钮不会被触发 有趣的是,在我的例子中,点击本身甚至在按下按钮之前就触发了“某物” 谁能告诉我我的钮扣怎么了 以下是我似乎并不真正理解的InteliTrace事件:

我有一个windows窗体,其中所有控件都是动态创建的

然而,所有的按钮似乎都在点击事件之前触发了“某物”,而“某物”导致我的组合框的selectedindexchanged被触发。我已经彻底检查了声明的事件处理程序,并确保没有按钮作为其事件进行了SIC路径

我注意到,用鼠标点击按钮的正常行为是,当点击按钮但按住并释放按钮外的区域时,按钮不会被触发

有趣的是,在我的例子中,点击本身甚至在按下按钮之前就触发了“某物”

谁能告诉我我的钮扣怎么了

以下是我似乎并不真正理解的InteliTrace事件:

Calls for thread <No Name> (6976)
[System.Threading.ThreadHelper.ThreadStart()]
SESAdminForm.My.MyApplication.Main(String() Args = {String(0)})
[Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(string[]      commandLine = {unknown})]
System.Windows.Forms.Application.Run(System.Windows.Forms.ApplicationContext context = {unknown})
ThreadContext.RunMessageLoop(int reason = {unknown}, System.Windows.Forms.ApplicationContext context = {unknown})
ThreadContext.RunMessageLoopInner(int reason = {unknown}, System.Windows.Forms.ApplicationContext context = {unknown})
ThreadContext.RunMessageLoopInner(int reason = {unknown}, System.Windows.Forms.ApplicationContext context = {unknown})
System.Windows.Forms.NativeWindow.DebuggableCallback(System.IntPtr hWnd = {unknown}, int msg = {unknown}, System.IntPtr wparam = {unknown}, System.IntPtr lparam = {unknown})
ControlNativeWindow.WndProc(ref System.Windows.Forms.Message m = {unknown})
ControlNativeWindow.OnMessage(ref System.Windows.Forms.Message m = {unknown})
System.Windows.Forms.Button.WndProc(ref System.Windows.Forms.Message m = {unknown})
System.Windows.Forms.ButtonBase.WndProc(ref System.Windows.Forms.Message m = {unknown})
System.Windows.Forms.Control.WndProc(ref System.Windows.Forms.Message m = {unknown})
System.Windows.Forms.Control.WmMouseDown(ref System.Windows.Forms.Message m = {unknown}, System.Windows.Forms.MouseButtons button = {unknown}, int clicks = {unknown})
System.Windows.Forms.Control.FocusInternal()
System.Windows.Forms.NativeWindow.DebuggableCallback(System.IntPtr hWnd = {unknown}, int msg = {unknown}, System.IntPtr wparam = {unknown}, System.IntPtr lparam = {unknown})
ControlNativeWindow.WndProc(ref System.Windows.Forms.Message m = {unknown})
ControlNativeWindow.OnMessage(ref System.Windows.Forms.Message m = {unknown})
System.Windows.Forms.Button.WndProc(ref System.Windows.Forms.Message m = {unknown})
System.Windows.Forms.ButtonBase.WndProc(ref System.Windows.Forms.Message m = {unknown})
System.Windows.Forms.Control.WndProc(ref System.Windows.Forms.Message m = {unknown})
System.Windows.Forms.Control.WmSetFocus(ref System.Windows.Forms.Message m = {unknown})
System.Windows.Forms.ContainerControl.ActivateControlInternal(System.Windows.Forms.Control control = {unknown})
System.Windows.Forms.ContainerControl.ActivateControlInternal(System.Windows.Forms.Control control = {unknown}, bool originator = {unknown})
System.Windows.Forms.ContainerControl.AssignActiveControlInternal(System.Windows.Forms.Control value = {unknown})
System.Windows.Forms.ContainerControl.UpdateFocusedControl()
System.Windows.Forms.ContainerControl.EnterValidation(System.Windows.Forms.Control enterControl = {unknown})
System.Windows.Forms.ContainerControl.ValidateThroughAncestor(System.Windows.Forms.Control ancestorControl = {unknown}, bool preventFocusChangeOnError = {unknown})
System.Windows.Forms.Control.PerformControlValidation(bool bulkValidation = {unknown})
System.Windows.Forms.Control.NotifyValidating()
System.Windows.Forms.ComboBox.OnValidating(System.ComponentModel.CancelEventArgs e = {unknown})
System.Windows.Forms.ComboBox.NotifyAutoComplete()
System.Windows.Forms.ComboBox.NotifyAutoComplete(bool setSelectedIndex = {unknown})
Set System.Windows.Forms.ComboBox.SelectedIndex(int value = {unknown})
System.Windows.Forms.ComboBox.OnSelectedIndexChanged(System.EventArgs e = {unknown})
SESAdminForm.DataEntry.cBox_SelectionChangeCommittedUseItem2(Object sender = {System.Windows.Forms.ComboBox}, System.EventArgs e = {System.EventArgs})
SESAdminForm.DataEntry.DisplayText()
调用线程(6976)
[System.Threading.ThreadHelper.ThreadStart()]
SESAdminForm.My.MyApplication.Main(String()Args={String(0)})
[Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(字符串[]命令行={unknown}]
System.Windows.Forms.Application.Run(System.Windows.Forms.ApplicationContext上下文={unknown})
ThreadContext.RunMessageLoop(int reason={unknown},System.Windows.Forms.ApplicationContext context={unknown})
ThreadContext.RunMessageLoopInner(int reason={unknown},System.Windows.Forms.ApplicationContext context={unknown})
ThreadContext.RunMessageLoopInner(int reason={unknown},System.Windows.Forms.ApplicationContext context={unknown})
System.Windows.Forms.NativeWindow.DebuggableCallback(System.IntPtr hWnd={unknown},int msg={unknown},System.IntPtr wparam={unknown},System.IntPtr lparam={unknown})
ControlNativeWindow.WndProc(ref System.Windows.Forms.Message m={unknown})
ControlNativeWindow.OnMessage(ref System.Windows.Forms.Message m={unknown})
System.Windows.Forms.Button.WndProc(ref System.Windows.Forms.Message m={unknown})
System.Windows.Forms.ButtonBase.WndProc(ref System.Windows.Forms.Message m={unknown})
System.Windows.Forms.Control.WndProc(ref System.Windows.Forms.Message m={unknown})
System.Windows.Forms.Control.WmMouseDown(ref System.Windows.Forms.Message m={unknown},System.Windows.Forms.MouseButtons button={unknown},int clicks={unknown})
System.Windows.Forms.Control.FocusInternal()
System.Windows.Forms.NativeWindow.DebuggableCallback(System.IntPtr hWnd={unknown},int msg={unknown},System.IntPtr wparam={unknown},System.IntPtr lparam={unknown})
ControlNativeWindow.WndProc(ref System.Windows.Forms.Message m={unknown})
ControlNativeWindow.OnMessage(ref System.Windows.Forms.Message m={unknown})
System.Windows.Forms.Button.WndProc(ref System.Windows.Forms.Message m={unknown})
System.Windows.Forms.ButtonBase.WndProc(ref System.Windows.Forms.Message m={unknown})
System.Windows.Forms.Control.WndProc(ref System.Windows.Forms.Message m={unknown})
System.Windows.Forms.Control.WmSetFocus(ref System.Windows.Forms.Message m={unknown})
System.Windows.Forms.ContainerControl.ActivateControlInternal(System.Windows.Forms.Control={unknown})
System.Windows.Forms.ContainerControl.ActivateControlInternal(System.Windows.Forms.Control={unknown},bool发起人={unknown})
System.Windows.Forms.ContainerControl.AssignActiveControlInternal(System.Windows.Forms.Control值={unknown})
System.Windows.Forms.ContainerControl.UpdateFocusedControl()
System.Windows.Forms.ContainerControl.EnterValidation(System.Windows.Forms.Control enterControl={unknown})
System.Windows.Forms.ContainerControl.ValidateThroughAncestorControl={unknown},bool preventFocusChangeOnError={unknown})
System.Windows.Forms.Control.PerformControlValidation(bool bulkValidation={unknown})
System.Windows.Forms.Control.NotifyValidating()
System.Windows.Forms.ComboBox.OnValidating(System.ComponentModel.CancelEventArgs e={unknown})
System.Windows.Forms.ComboBox.NotifyAutoComplete()
System.Windows.Forms.ComboBox.NotifyAutoComplete(bool setSelectedIndex={unknown})
设置System.Windows.Forms.ComboBox.SelectedIndex(int值={unknown})
System.Windows.Forms.ComboBox.OnSelectedIndexChanged(System.EventArgs e={unknown})
SESAdminForm.DataEntry.cBox_SelectionChangeCommittedUserItem2(对象发送方={System.Windows.Forms.ComboBox},System.EventArgs e={System.EventArgs})
SESAdminForm.DataEntry.DisplayText()
->有两条与按钮相关的消息,但其中一条似乎发生在mousedown事件之前


谢谢您的时间。

我面临一个类似的问题,原因似乎是“重复”组合框条目(文本相同,但值不同)和我在组合框上设置的自动完成。我没有解决方法,但问题似乎是表单的所有子元素都可以一起验证,验证触发NotifyAutoComplete,然后可以更改所选索引

微软在这里用一句话暗示了这个问题:“如果维护的源代码中有重复的条目,那么自动完成的行为是不可预测的。”

这里是来源,这样你就可以了解发生了什么

本质上,对ComboBox.OnValidating()的调用检查ComboBox上是否启用了自动完成。如果是,则假定我们正在验证,因为用户正在键入。OnValidating调用NotifyOnComplete并尝试设置所选索引,这是通过比较文本值来完成的。这很粗糙,不容易规避


我的解决办法是在调用Form.ValidateChildren()之前关闭组合框上的自动完成功能,该功能会触发此错误,然后在验证后恢复自动完成值。

你能发布一个吗?我会的,但因为我的程序是动态的n复杂的n我不知道问题出在哪里,我很可能会把我的全部代码贴在你的邮箱上,大概有5千行。我相信你不会