編輯:這是一個與此提議的副本中涵蓋的問題不同的問題。這個問題解決了一個不同的問題,那里的答案都不適用于我的情況。我的單選按鈕從一開始就被選中,因為它應該是。它(或者更確切地說是它的組的成員)是在 Load 事件處理程式期間使用保存的屬性設定的。此外,如下所述,我的麻煩單選按鈕不是標簽順序中的第一個。
我剛開始在負責重構的 WinForms 應用程式上遇到問題。
在表單的 Load 事件處理程式完成一段時間后,一個特定單選按鈕的 Click 事件會自動觸發。我不知道為什么,它弄亂了程式的預期流程。
我在點擊處理程式的開頭放了一個斷點并檢查了呼叫堆疊,但麻煩的呼叫總是來自外部代碼,其中最后兩行是 RadioButton.OnEnter 后跟 Control.OnClick
谷歌搜索出現了這個 SE 答案,但這與我的情況不太匹配。有問題的單選按鈕在選項卡順序中相當晚,所有控制元件都是在 VS Designer 中添加的,而不是通過代碼添加的,并且加載處理程式在完成之前將 Focus 設定為不同的控制元件。在加載處理程式結束和錯誤單擊之間沒有遇到對單選按鈕的參考(使用太多斷點進行驗證)。
有誰知道還有什么可能導致控制元件在啟動時自動單擊?
編輯 這是單選按鈕 (mVmOff) 的設計器代碼及其所在的嵌套組框:
//
// mVmGroup (6th in tab order)
//
this.mVmGroup.Controls.Add(this.milliVmGroup);
this.mVmGroup.Controls.Add(this.mVRLabel);
this.mVmGroup.Controls.Add(this.mVmLabel);
this.mVmGroup.Controls.Add(this.mVRCombo);
this.mVmGroup.Controls.Add(this.mVm);
this.mVmGroup.Font = new System.Drawing.Font("Verdana", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.mVmGroup.ForeColor = System.Drawing.Color.Black;
this.mVmGroup.Location = new System.Drawing.Point(200, 295);
this.mVmGroup.Name = "mVmGroup";
this.mVmGroup.Size = new System.Drawing.Size(169, 140);
this.mVmGroup.TabIndex = 6;
this.mVmGroup.TabStop = false;
this.mVmGroup.Text = "mV Meter";
this.toolTip.SetToolTip(this.mVmGroup, "This is the Milli Voltmeter\'s matrix.");
//
// milliVmGroup (6.4 in tab order)
//
this.milliVmGroup.Controls.Add(this.mVmOS);
this.milliVmGroup.Controls.Add(this.mVmOff);
this.milliVmGroup.Controls.Add(this.mVmIS);
this.milliVmGroup.Font = new System.Drawing.Font("Verdana", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.milliVmGroup.ForeColor = System.Drawing.Color.RoyalBlue;
this.milliVmGroup.Location = new System.Drawing.Point(8, 63);
this.milliVmGroup.Name = "milliVmGroup";
this.milliVmGroup.Size = new System.Drawing.Size(152, 71);
this.milliVmGroup.TabIndex = 4;
this.milliVmGroup.TabStop = false;
this.milliVmGroup.Text = "Matrix";
//
// mVmOff (6.4.2 in tab order)
//
this.mVmOff.AutoSize = true;
this.mVmOff.Checked = true;
this.mVmOff.Font = new System.Drawing.Font("Verdana", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.mVmOff.Location = new System.Drawing.Point(5, 49);
this.mVmOff.Name = "mVmOff";
this.mVmOff.Size = new System.Drawing.Size(42, 17);
this.mVmOff.TabIndex = 2;
this.mVmOff.TabStop = true;
this.mVmOff.Text = "Off";
this.toolTip.SetToolTip(this.mVmOff, "Click to disconnect the Milli Voltmeter. \r\nDisconnects K29, K8, and K30. \r\nConnec"
"ts K3 and K11. \r\n \r\nNote: Normaly checked.");
this.mVmOff.UseVisualStyleBackColor = true;
this.mVmOff.Click = new System.EventHandler(this.MVmOff_Click);
以及表格該部分的螢屏截圖: mV Meter group box
如果有幫助,在錯誤單擊發生的斷點處,單選按鈕是在表單上繪制的唯一控制元件。
uj5u.com熱心網友回復:
通常,您應該依賴 CheckedChange 并僅在單擊發生時才使用 Click。
但我會回答你的問題:
有誰知道還有什么可能導致控制元件在啟動時自動單擊?
您的 RadioButton 按預期聚焦。那么引發 Click 事件是 RadioButtonOnEnter方法中存在的邏輯錯誤。當您使用箭頭鍵輸入控制元件時,它會嘗試引發 OnClick,但它無法以正確的方式執行此操作,例如,即使焦點已自動移動到控制元件而沒有箭頭鍵,它也會引發 Click 事件。
當您通過覆寫 OnEnter 進入控制元件時,您可以禁用引發 Click 事件:
using System;
using System.Windows.Forms;
public class MyRadioButton : RadioButton
{
protected override void OnEnter(EventArgs e)
{
RaiseOnEnter(e);
}
private void RaiseOnEnter(EventArgs e)
{
var EventEvent = typeof(Control).GetField("EventEnter",
System.Reflection.BindingFlags.NonPublic |
System.Reflection.BindingFlags.Static).GetValue(null);
EventHandler handler = (EventHandler)Events[EventEvent];
handler?.Invoke(this, e);
}
}
關于 .NET 5 和 6 的注意事項
即使在.NET 5 和 6 RadioButton中也存在同樣的問題;如果您想對這些版本使用上述解決方法,請確保獲得該s_enterEvent欄位。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/475065.html
上一篇:VB.NET6,(Win形式)中的哈希密碼并將其存盤在MySQL服務器中
下一篇:OAuth2和MFA是什么關系
