C# - 按钮点击和函数调用的 if else 语句
C# - if else statement for button click and function call
我正在开发一个系统,该系统涉及将一组值输入到一系列文本框中,然后单击一个按钮,将每个文本框中的值添加到它各自的 List<>
中。
单击按钮后,我使用 Focus()
函数将焦点放在文本框组 (txtHR
) 顶部的文本框上。当使用光标单击按钮时,这工作正常。
唯一的问题是:
因为有很多文本框要写,我做了一个函数,点击 Enter 键将焦点移到文本框列表中(使数据输入更快) .这会导致焦点位于按钮 btnSaveData
上,并再次按下 Enter 键有效地执行按钮单击。
这会将 return 焦点移至 txtHR
,但系统随后也会接收 Enter 键并将焦点向下移动到下一个文本框。
有办法解决这个问题吗?我猜它会涉及一个 if/else
语句,它基于是按钮单击还是按键调用 txtHR.Focus()
.
btnSaveData_Click
和 Control_KeyUp
的代码如下所示:
private void btnSaveData_Click(object sender, EventArgs e) //To be clicked while clock is running
{ //turn inputted data into outputted data
//take the data in the input boxes and...
updateLists(); //add to respective list
saveReadings(); //append each variable to file
//return cursor to top box in list ready for next data set
txtHR.Focus();
}
private void Control_KeyUP(object sender, KeyEventArgs e) //for selected textboxes and buttons only
{
if ((e.KeyCode == Keys.Enter) || (e.KeyCode == Keys.Return))
{
this.SelectNextControl((Control)sender, true, true, true, true);
}
}
在进行焦点更改之前,您可以测试以确保按下 Enter 键的控件是 TextBox
,或者如果还有其他类型的控件您也需要这种焦点转发行为,而不是测试它是否是保存按钮。像这样:
private void Control_KeyUP(object sender, KeyEventArgs e) //for selected textboxes and buttons only
{
// Bail if not on a TextBox.
if ((sender as TextBox) == null) // **or instead** if ((sender as Button) == this.btnSaveData)
return;
if ((e.KeyCode == Keys.Enter) || (e.KeyCode == Keys.Return))
{
this.SelectNextControl((Control)sender, true, true, true, true);
}
}
我正在开发一个系统,该系统涉及将一组值输入到一系列文本框中,然后单击一个按钮,将每个文本框中的值添加到它各自的 List<>
中。
单击按钮后,我使用 Focus()
函数将焦点放在文本框组 (txtHR
) 顶部的文本框上。当使用光标单击按钮时,这工作正常。
唯一的问题是:
因为有很多文本框要写,我做了一个函数,点击 Enter 键将焦点移到文本框列表中(使数据输入更快) .这会导致焦点位于按钮 btnSaveData
上,并再次按下 Enter 键有效地执行按钮单击。
这会将 return 焦点移至 txtHR
,但系统随后也会接收 Enter 键并将焦点向下移动到下一个文本框。
有办法解决这个问题吗?我猜它会涉及一个 if/else
语句,它基于是按钮单击还是按键调用 txtHR.Focus()
.
btnSaveData_Click
和 Control_KeyUp
的代码如下所示:
private void btnSaveData_Click(object sender, EventArgs e) //To be clicked while clock is running
{ //turn inputted data into outputted data
//take the data in the input boxes and...
updateLists(); //add to respective list
saveReadings(); //append each variable to file
//return cursor to top box in list ready for next data set
txtHR.Focus();
}
private void Control_KeyUP(object sender, KeyEventArgs e) //for selected textboxes and buttons only
{
if ((e.KeyCode == Keys.Enter) || (e.KeyCode == Keys.Return))
{
this.SelectNextControl((Control)sender, true, true, true, true);
}
}
在进行焦点更改之前,您可以测试以确保按下 Enter 键的控件是 TextBox
,或者如果还有其他类型的控件您也需要这种焦点转发行为,而不是测试它是否是保存按钮。像这样:
private void Control_KeyUP(object sender, KeyEventArgs e) //for selected textboxes and buttons only
{
// Bail if not on a TextBox.
if ((sender as TextBox) == null) // **or instead** if ((sender as Button) == this.btnSaveData)
return;
if ((e.KeyCode == Keys.Enter) || (e.KeyCode == Keys.Return))
{
this.SelectNextControl((Control)sender, true, true, true, true);
}
}