1.给按钮添加快捷键
按钮中Text属性后面添加关键字:(&O)—其中“O”表示快捷得键盘按钮。
即:Alt+O就可以键盘快捷键使用了
2.快捷键打开某个窗口或者事件
首先,给窗口注册键盘事件:keyDown
其次,代码中添加判断,判断某个按钮实现某个事件。代码示例如下:
/// <summary>
/// 键盘按钮事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void TFundBillAddDataForm_KeyDown(object sender, KeyEventArgs e)
{
TextBox textBox = new TextBox();
if (e.KeyCode == Keys.F7)
{
// 收款银行id
if (string.IsNullOrEmpty(textBox7.Text) || SysContent.tBankInfo == null)
{
textBox = textBox7;
}
// 归属单位id
else if (string.IsNullOrEmpty(textBox1.Text) || SysContent.tClientInfo == null)
{
textBox = textBox1;
}
// 资金类别id
else if (string.IsNullOrEmpty(textBox5.Text) || SysContent.tFundTypeInfo == null)
{
textBox = textBox5;
}
// 资金状态id
else if (string.IsNullOrEmpty(textBox6.Text) || SysContent.tFundStatusInfo == null)
{
textBox = textBox6;
}
// 防止重复刷新
if (string.Equals(textboxName, textBox.Name) && panel1.Visible == true)
{
return;
}
switch (textBox.Name)
{
case "textBox1":
List<TClientInfo> listTClientForm = tClientFormSQL.selectConditionData(textBox1.Text);
// 资金归属单位赋值
tClientRefrenceData(listTClientForm);
break;
case "textBox7":
List<TBankInfo> listTBank = receivingBankFormSQL.selectConditionData(textBox7.Text);
// 收款银行赋值
tBankRefrenceData(listTBank);
break;
case "textBox5":
List<TFundTypeInfo> listTFundType = fundTypeFormSQL.selectConditionData(textBox5.Text);
// 资金类别列表赋值
tFundTypeRefrenceData(listTFundType);
break;
case "textBox6":
List<TFundStatusInfo> list = fundStatusFormSQL.selectConditionData(textBox6.Text);
// 资金状态列表赋值
tFundStatusRefrenceData(list);
break;
case "textBox8":
List<TAccountInfo> listTAccountInfo = tAccountFormSQL.selectConditionData(textBox8.Text);
// 预算科目列表赋值
tAccountRefrenceData(listTAccountInfo);
break;
case "textBox9":
List<TItemInfo> listTItemInfo = tItemFormSQL.selectConditionData(textBox9.Text);
// 预算项目列表赋值
tItemRefrenceData(listTItemInfo);
break;
}
textboxName = textBox.Name;
panel1.Visible = true;
}
}