当我按下Enter键时,启用buttonEditClient_PressEnter函数, 应该调用buttonEditClient_ButtonClick函数。
private void buttonEditClient_PressEnter(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
//fire buttonEditClient_ButtonClick function
}
}
private async void buttonEditClient_ButtonClick(object sender, ButtonPressedEventArgs e)
{
//buttonEditClient_ButtonClick activated
}
在设计器中:
this.buttonEditClient.ButtonClick += new DevExpress.XtraEditors.Controls.ButtonPressedEventHandler(this.buttonEditClient_ButtonClick);
this.buttonEditClient.KeyDown += new System.Windows.Forms.KeyEventHandler(this.buttonEditClient_PressEnter);
如果我尝试这样做:
private void buttonEditClient_PressEnter(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
buttonEditClient_ButtonClick(sender, e)
}
}
我收到此错误:
cannot convert from 'System.Windows.Forms.KeyEventArgs' to 'DevExpress.XtraEditors.Controls.ButtonPressedEventArgs'
如何激活buttonEditClient_ButtonClick函数?
答案 0 :(得分:2)
点击事件与键盘事件本质上是不同的(例如,一个事件包含有关按下的鼠标按钮和光标位置的信息,另一个包含有关按下的键的信息),因此您无法将KeyEventArgs
传递给键盘事件。点击处理程序,它需要一个ButtonPressedEventArgs
。
您在这里有一些简单的选择:
ButtonPressedEventArgs
实例的方法,然后传递该实例而不是KeyEventArgs
。这将是一个非常棘手的解决方案,因为您实际上是在编造东西(您要赋予它什么光标位置?)。第一个解决方案可能如下所示:
private void buttonEditClient_PressEnter(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
buttonEditClientSubmit();
}
}
private async void buttonEditClient_ButtonClick(object sender, ButtonPressedEventArgs e)
{
buttonEditClientSubmit();
}
private void buttonEditClientSubmit()
{
// your code...
}
答案 1 :(得分:0)
这取决于您是否确实需要ButtonPressedEventArgs
中的任何内容。如果您不需要ButtonPressedEventArgs
中的任何内容,则可以让两个事件都调用一个函数。
private void Handle_buttonEditClient()
{
// Do what you want to do when the button is pressed or has the "Enter"
// key pressed on it.
}
private void buttonEditClient_PressEnter(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
Handle_buttonEditClient();
}
}
private void buttonEditClient_ButtonClick(object sender, ButtonPressedEventArgs e)
{
Handle_buttonEditClient();
}
注意
我使buttonEditClick_ButtonClick
同步,但是如果使它保持异步,则同样适用。只需让两个事件调用相同的函数即可。
如果您需要ButtonPressedEventArgs
,则就像Anas Alweish所说的那样。您必须创建ButtonPressedEventArgs
的实例。我对DevExpress不熟悉,所以我不知道你会怎么做。也许像new ButtonPressedEventArgs(buttonEditClient)
一样?;