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

时间:2014-07-07 04:14:39

标签: c# asp.net vb.net

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

然而,所有按钮似乎都在触发" Something"在点击事件之前,以及" Something"导致我的组合框中的selectedindexchanged被触发。我已经完全了解了所声明的事件处理程序,并确保没有任何按钮将SIC作为事件进行修改。

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

有趣的是,在我的情况下,点击本身会触发" Something"甚至在按下释放之前。

有人可以告诉我按钮发生了什么吗?

以下是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()

- &GT;有两个与按钮相关的消息,但有一个消息似乎甚至在mousedown事件之前发生。

感谢您的时间。

1 个答案:

答案 0 :(得分:0)

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

Microsoft在此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/Managed/System/WinForms/ComboBox.cs,21e4308c05a79f0a

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

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