我表单上的所有按钮似乎都在触发“某些东西”

本文关键字:某些东西 表单 按钮 | 更新日期: 2023-09-27 18:11:55

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

然而,在点击事件之前,所有的按钮似乎都在触发"某事",而"某事"正在导致我的组合框的选定交换触发。我已经彻底检查了声明的事件处理程序,并确保没有一个按钮将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()

->有两个按钮相关的消息,但其中一个似乎发生在mousedown事件之前。

感谢您的宝贵时间。

我表单上的所有按钮似乎都在触发“某些东西”

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

微软在这里暗示了这个问题https://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.autocompletemode%28v=vs.110%29.aspx,并评论说"如果在维护的源代码中有重复的条目,自动完成的行为是不可预测的。"

这是源代码,这样你就可以理解发生了什么;

http://referencesource.microsoft.com/System.Windows.Forms winforms/管理/系统/winforms/ComboBox.cs, 21 e4308c05a79f0a

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

我的修复是在我调用Form.ValidateChildren()之前关闭我的组合框上的自动完成,这会触发这种废话,然后在验证后恢复自动完成值