Although I've made sure that one of these text boxes are indeed in focus, the Focused property is false for each one. Why?
private TextBox GetTextboxInFocus()
{
if (txtFeet.Focused)
{
return txtFeet;
}
if (txtInches.Focused)
{
return txtInches;
}
if (txtSixteenths.Focused)
{
return txtSixteenths;
}
return null;
}
答案 0 :(得分:0)
问题在于你调用/调用......
我已经为你理解了一个示例代码......
private void CheckFocusAndShowResults(string throughControl)
{
var result = GetTextboxInFocus();
if (result == null)
{
label1.Text = "No textbox is focused.";
}
else
{
label1.Text = string.Format("textbox {0} is focused and {1} is called.", result.Name, throughControl);
}
}
尝试通过按钮调用时......没有文本框聚焦。
然后我为文本框添加了两个事件以验证焦点......
private void textBox1_Click(object sender, EventArgs e)
{
CheckFocusAndShowResults("Texbox Click Event");
}
private void textBox1_Leave(object sender, EventArgs e)
{
CheckFocusAndShowResults("Texbox Leave Event");
}
单击textbox1时,会触发第一个事件,并且textbox1会聚焦(请参阅标签)
当离开textbox1时,第二个事件被触发,textbox1被聚焦,同时评估条件......虽然目前焦点是textbox2(用蓝色轮廓突出显示)。
所以你可以按照我所示的点击或离开事件来调用。