[英]How to prevent user control from stealing focus?
我創建了一個充當鍵盤的用戶控件。 我以與鍵盤完全相同的方式在控件中排列按鈕,然后設置(ControlStyles.Selectable, false)
。 這使焦點可以停留在用戶在父窗體上選擇的文本框中。 我還將用戶控件的樣式設置為不可選擇。 當用戶按下按鈕時,一切工作都很好,但是當用戶控件被單擊時,它會將焦點從文本框移開。 這是我的代碼:
public partial class TouchKeyboard : UserControl
{
public TouchKeyboard()
{
InitializeComponent();
SetStyle(ControlStyles.Selectable, false);
}
private void TouchKeyboard_Load(object sender, EventArgs e)
{
foreach (var button in this.Controls.OfType<CustomControls.CustomButton>())
{
button.Click += new EventHandler(this.ButtonClick);
}
}
private void ButtonClick(object sender, EventArgs e)
{
var button = sender as CustomControls.CustomButton;
if (button.Name == "btnSpace")
{
SendKeys.Send(" ");
}
else
{
SendKeys.Send("{" + button.Text + "}");
}
}
}
當ControlStyles.Selectable
設置為false時,為什么我的用戶控件會搶占焦點?
經過幾天的努力,我終於弄清楚了。 由於面板無法吸引焦點,因此我將面板固定在用戶控件中,並將按鈕放在其中。 這解決了問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.