ProcessCmdKey 不通过回车键
ProcessCmdKey does not pass Enter Key
发生了一些事情,导致我的代码 总是 捕获 Enter 键但从不处理它。意思是 all 我的数据输入框、网格控件等都坏了。就像Enter键被关闭了。
我已经尝试完全删除 ProcessCmdKey
例程,但它仍在执行。
我有一段相当冗长的代码处理选项卡式表单上的 enter 键,但它弄乱了其他选项卡,这意味着每当 enter 被按下它会捕获它。
所以,我只是查看它在哪个选项卡上,如果不是正确的选项卡,就忽略它,简单吗?没有。
无论出于何种原因,无论什么,我的表单都不会处理回车键。
我已经回到了最基本的代码:
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
return base.ProcessCmdKey(ref msg, keyData);
}
根据我的理解,上面应该什么都不做,只需将按下的键传递回去然后处理它。
这适用于除 enter 键之外的所有内容。如果我有一个文本框或任何控件,那么它将接受所有输入,但它完全忽略 enter 键。我也试过 return false;
但还是不行。
我什至尝试完全删除 例程,但它仍然 坏了。重启VS以防万一。
编辑:
OK,下面是我开始 "fix" 事情之前的完整原始代码。这可以正常工作,除了它会阻止 Enter 键在程序的其他地方 工作。正如您所想象的那样,最终用户并不十分欣赏。
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (ActiveControl.Name.ToString() != "order_creation_grid") //ensures that we don't caputre keypresses in the datagrid
{
//if (keyData == (Keys.Tab | Keys.Shift)) //tabbing backwards
//{
// if (ActiveControl.Parent.Name.ToString() == "order_con_name_DD")
// {
// order_con_name_DD.Select();
// order_con_name_DD.ShowDropDown();
// }
//}
if (keyData == Keys.Tab || keyData == Keys.Enter)
{
if (ActiveControl.Parent.Name.ToString() == "order_shipper_dd")
{
var test = keyData;
//getAllConsignees(); remove
get_all_addresses(); //load up the address dd
order_address_dd.SelectedIndex = 0;
order_address_dd.Select();
order_address_dd.Focus();
return true;//lets the system know that the control key was handled and not to fire it again
}
else if (ActiveControl.Parent.Name.ToString() == "order_address_dd")
{
if (order_address_dd.SelectedIndex != -1)//only do this if there is not a new address in the address chooser ie: found one in database
{
order_match_dd.Select();
order_match_dd.Focus();
order_match_dd.ShowDropDown();
return true;
}
else
{
order_address_eb.Value = order_address_dd.Text;
order_consignee_eb.Select();
order_consignee_eb.Focus();
return true; ;
}
}
else if (ActiveControl.Name.ToString() == "order_match_dd")
{
if (order_match_dd.SelectedIndex != -1)//fire off when a valid selection is made
{
//check if it is a new item
if (order_match_dd.Text == "1 New")
{
//blank out the fields and position cursor to Consignee
clear_order_fields();
order_consignee_eb.Select();
order_consignee_eb.Focus();
}
else //parse the value in the match box useing the seperator | into strings and load up the correct fields
{
char[] delimiterChars = { '|' };
string[] fields = order_match_dd.Text.Split(delimiterChars);
clear_order_fields();
order_consignee_eb.Value = fields[0].ToString().Trim(); ;
order_address_eb.Value = fields[1].ToString().Trim();
order_city_eb.Value = fields[2].ToString().Trim();
order_state_eb.Value = fields[3].ToString().Trim();
order_zip_eb.Value = fields[4].ToString().Trim();
//go try and match the driver and facility with this zipcode
get_driver_facility();
order_BOL_eb.Select();
order_BOL_eb.Focus();
}
return true;
}
}
else if (ActiveControl.Parent.Name.ToString() == "order_BOL_eb")
{
int custID = Convert.ToInt16(order_shipper_dd.SelectedValue);
string testFor = order_BOL_eb.Value.ToString();
var lookFor = (from l in dbContext.stop_details
where l.cust_unique_id == testFor && l.customer_id == custID
select l).ToArray();
int count = lookFor.Count();
if (count > 0)
{
MessageBox.Show("WARNING..this BOL has been used before for this customer, make sure you really want to use it again.");
}
order_loose_qty.Focus();
return true;
}
else if (ActiveControl.Parent.Name.ToString() == "order_consignee_eb")
{
order_address_eb.Select();
order_address_eb.Focus();
return true;
}
else if (ActiveControl.Parent.Name.ToString() == "order_address_eb")
{
order_city_eb.Select();
order_city_eb.Focus();
return true;
}
else if (ActiveControl.Parent.Name.ToString() == "order_city_eb")
{
order_state_eb.Select();
order_state_eb.Focus();
return true;
}
else if (ActiveControl.Parent.Name.ToString() == "order_state_eb")
{
order_zip_eb.Select();
order_zip_eb.Focus();
return true;
}
else if (ActiveControl.Parent.Name.ToString() == "order_zip_eb")
{
// get_driver_facility();
order_BOL_eb.Select();
order_BOL_eb.Focus();
return true;
}
else if (ActiveControl.Parent.Name.ToString() == "order_note")
{
//auto send to the grid if checkboxed
if (autoSend_cb.Checked)
{
send_to_orderGrid();
}
//otherwise just sit there...
return true;
}
}
if (keyData == Keys.Enter) //On enter key for these controls fire the tab key so that it will move to the next control
{
if (ActiveControl.Parent.Name.ToString() == "order_loose_weight")
{
SendKeys.Send("{TAB}");
}
else if (ActiveControl.Parent.Name.ToString() == "order_loose_qty")
{
SendKeys.Send("{TAB}");
}
else if (ActiveControl.Parent.Name.ToString() == "order_pallets_qty")
{
SendKeys.Send("{TAB}");
}
else if (ActiveControl.Parent.Name.ToString() == "order_pallets_weight")
{
SendKeys.Send("{TAB}");
}
else if (ActiveControl.Parent.Name.ToString() == "order_hazmat_weight")
{
SendKeys.Send("{TAB}");
}
else if (ActiveControl.Parent.Name.ToString() == "order_COD")
{
SendKeys.Send("{TAB}");
}
}
if (keyData == (Keys.Control | Keys.OemCloseBrackets))
{
this.order_shipper_dd.Select();
this.order_shipper_dd.ShowDropDown();
return true;
}
if (keyData == (Keys.Control | Keys.Enter)) //when ctrl-enter is pressed in the Order Entry screen
{
send_to_orderGrid();
return true;
}
}
return base.ProcessCmdKey(ref msg, keyData);
}
我什至可以注释掉 整个 例程,它仍然可以。
在您发现回车键的真正问题之前考虑这样做:
private void Form1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
if (e.KeyCode == Keys.Return)
e.IsInputKey = true;
}
它的作用很明显,但它现在应该可以解决您的问题。有帮助吗?
我不确定你需要什么。您尚未定义 "process" 回车键的含义。我的最佳猜测:
1- 是否要使用回车键在文本框中换行?将 AcceptsReturn 属性 从 False 更改为 True,您的文本框应该允许您使用回车键来创建新行。
2 - 使用回车键跳转到下一个控件?将 keyDown 事件添加到文本框并执行如下操作:
switch (e.KeyCode)
{
case Keys.Enter:
SendKeys.Send("{TAB}");
break;
}
发生了一些事情,导致我的代码 总是 捕获 Enter 键但从不处理它。意思是 all 我的数据输入框、网格控件等都坏了。就像Enter键被关闭了。
我已经尝试完全删除 ProcessCmdKey
例程,但它仍在执行。
我有一段相当冗长的代码处理选项卡式表单上的 enter 键,但它弄乱了其他选项卡,这意味着每当 enter 被按下它会捕获它。
所以,我只是查看它在哪个选项卡上,如果不是正确的选项卡,就忽略它,简单吗?没有。
无论出于何种原因,无论什么,我的表单都不会处理回车键。
我已经回到了最基本的代码:
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
return base.ProcessCmdKey(ref msg, keyData);
}
根据我的理解,上面应该什么都不做,只需将按下的键传递回去然后处理它。
这适用于除 enter 键之外的所有内容。如果我有一个文本框或任何控件,那么它将接受所有输入,但它完全忽略 enter 键。我也试过 return false;
但还是不行。
我什至尝试完全删除 例程,但它仍然 坏了。重启VS以防万一。
编辑: OK,下面是我开始 "fix" 事情之前的完整原始代码。这可以正常工作,除了它会阻止 Enter 键在程序的其他地方 工作。正如您所想象的那样,最终用户并不十分欣赏。
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (ActiveControl.Name.ToString() != "order_creation_grid") //ensures that we don't caputre keypresses in the datagrid
{
//if (keyData == (Keys.Tab | Keys.Shift)) //tabbing backwards
//{
// if (ActiveControl.Parent.Name.ToString() == "order_con_name_DD")
// {
// order_con_name_DD.Select();
// order_con_name_DD.ShowDropDown();
// }
//}
if (keyData == Keys.Tab || keyData == Keys.Enter)
{
if (ActiveControl.Parent.Name.ToString() == "order_shipper_dd")
{
var test = keyData;
//getAllConsignees(); remove
get_all_addresses(); //load up the address dd
order_address_dd.SelectedIndex = 0;
order_address_dd.Select();
order_address_dd.Focus();
return true;//lets the system know that the control key was handled and not to fire it again
}
else if (ActiveControl.Parent.Name.ToString() == "order_address_dd")
{
if (order_address_dd.SelectedIndex != -1)//only do this if there is not a new address in the address chooser ie: found one in database
{
order_match_dd.Select();
order_match_dd.Focus();
order_match_dd.ShowDropDown();
return true;
}
else
{
order_address_eb.Value = order_address_dd.Text;
order_consignee_eb.Select();
order_consignee_eb.Focus();
return true; ;
}
}
else if (ActiveControl.Name.ToString() == "order_match_dd")
{
if (order_match_dd.SelectedIndex != -1)//fire off when a valid selection is made
{
//check if it is a new item
if (order_match_dd.Text == "1 New")
{
//blank out the fields and position cursor to Consignee
clear_order_fields();
order_consignee_eb.Select();
order_consignee_eb.Focus();
}
else //parse the value in the match box useing the seperator | into strings and load up the correct fields
{
char[] delimiterChars = { '|' };
string[] fields = order_match_dd.Text.Split(delimiterChars);
clear_order_fields();
order_consignee_eb.Value = fields[0].ToString().Trim(); ;
order_address_eb.Value = fields[1].ToString().Trim();
order_city_eb.Value = fields[2].ToString().Trim();
order_state_eb.Value = fields[3].ToString().Trim();
order_zip_eb.Value = fields[4].ToString().Trim();
//go try and match the driver and facility with this zipcode
get_driver_facility();
order_BOL_eb.Select();
order_BOL_eb.Focus();
}
return true;
}
}
else if (ActiveControl.Parent.Name.ToString() == "order_BOL_eb")
{
int custID = Convert.ToInt16(order_shipper_dd.SelectedValue);
string testFor = order_BOL_eb.Value.ToString();
var lookFor = (from l in dbContext.stop_details
where l.cust_unique_id == testFor && l.customer_id == custID
select l).ToArray();
int count = lookFor.Count();
if (count > 0)
{
MessageBox.Show("WARNING..this BOL has been used before for this customer, make sure you really want to use it again.");
}
order_loose_qty.Focus();
return true;
}
else if (ActiveControl.Parent.Name.ToString() == "order_consignee_eb")
{
order_address_eb.Select();
order_address_eb.Focus();
return true;
}
else if (ActiveControl.Parent.Name.ToString() == "order_address_eb")
{
order_city_eb.Select();
order_city_eb.Focus();
return true;
}
else if (ActiveControl.Parent.Name.ToString() == "order_city_eb")
{
order_state_eb.Select();
order_state_eb.Focus();
return true;
}
else if (ActiveControl.Parent.Name.ToString() == "order_state_eb")
{
order_zip_eb.Select();
order_zip_eb.Focus();
return true;
}
else if (ActiveControl.Parent.Name.ToString() == "order_zip_eb")
{
// get_driver_facility();
order_BOL_eb.Select();
order_BOL_eb.Focus();
return true;
}
else if (ActiveControl.Parent.Name.ToString() == "order_note")
{
//auto send to the grid if checkboxed
if (autoSend_cb.Checked)
{
send_to_orderGrid();
}
//otherwise just sit there...
return true;
}
}
if (keyData == Keys.Enter) //On enter key for these controls fire the tab key so that it will move to the next control
{
if (ActiveControl.Parent.Name.ToString() == "order_loose_weight")
{
SendKeys.Send("{TAB}");
}
else if (ActiveControl.Parent.Name.ToString() == "order_loose_qty")
{
SendKeys.Send("{TAB}");
}
else if (ActiveControl.Parent.Name.ToString() == "order_pallets_qty")
{
SendKeys.Send("{TAB}");
}
else if (ActiveControl.Parent.Name.ToString() == "order_pallets_weight")
{
SendKeys.Send("{TAB}");
}
else if (ActiveControl.Parent.Name.ToString() == "order_hazmat_weight")
{
SendKeys.Send("{TAB}");
}
else if (ActiveControl.Parent.Name.ToString() == "order_COD")
{
SendKeys.Send("{TAB}");
}
}
if (keyData == (Keys.Control | Keys.OemCloseBrackets))
{
this.order_shipper_dd.Select();
this.order_shipper_dd.ShowDropDown();
return true;
}
if (keyData == (Keys.Control | Keys.Enter)) //when ctrl-enter is pressed in the Order Entry screen
{
send_to_orderGrid();
return true;
}
}
return base.ProcessCmdKey(ref msg, keyData);
}
我什至可以注释掉 整个 例程,它仍然可以。
在您发现回车键的真正问题之前考虑这样做:
private void Form1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
if (e.KeyCode == Keys.Return)
e.IsInputKey = true;
}
它的作用很明显,但它现在应该可以解决您的问题。有帮助吗?
我不确定你需要什么。您尚未定义 "process" 回车键的含义。我的最佳猜测:
1- 是否要使用回车键在文本框中换行?将 AcceptsReturn 属性 从 False 更改为 True,您的文本框应该允许您使用回车键来创建新行。
2 - 使用回车键跳转到下一个控件?将 keyDown 事件添加到文本框并执行如下操作:
switch (e.KeyCode)
{
case Keys.Enter:
SendKeys.Send("{TAB}");
break;
}