如何将Enter键按下事件设置为DataGridView C#中的Tab键 [英] how to set enter key press event to work as Tab key in DataGridView C#

查看:392
本文介绍了如何将Enter键按下事件设置为DataGridView C#中的Tab键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个gridview。我需要像Tab键一样输入键bahaves。但它不是这样工作的。

这里是我的代码...

i have a gridview. i need enter key bahaves like a Tab key. but its not working like this.
here is my code...

private void dataGridView1_KeyPress(object sender, KeyPressEventArgs e)
{
    try
    {
        switch (e.KeyChar)
        {
            case (char)Keys.Enter:
                //SendKeys.Send("{Tab}");
                SelectNextEditableCell(DataGridView dataGridView1);

                break;
            default:
                break;
        }
    }
    catch(Exception ex)
    {
        MessageBox.Show("Error"+ex.Message);
    }

}

推荐答案

您正在处理事件 KeyPress 在这?相反,使用 Keys.Enter KeyDown 甚至 KeyUp C>。这是因为没有与Enter键对应的字符,并且您的类型转换为您提供了一些不相关的字符。字符和键值不直接相关,它们在不同级别上处理和映射。许多键没有相应的字符,或者它们在不同的文化中表现不同。



你注释掉了 SendKeys 。这将是一个错误的方法。按键仿真通常是件坏事。



-SA
Are you handling the event KeyPress here? Instead, handle the event KeyDown or even KeyUp with Keys.Enter. This is because there is no a "character" corresponding to the Enter key, and your type cast gives you some unrelated character. Characters and keys values are not directly related, they are handled and mapped on different levels. Many keys do not have corresponding characters, or they behave differently in different cultures.

It's good that you commented out SendKeys. It would be a wrong approach. Simulation of key presses is generally a bad thing.

—SA


访问处理DatagridView的keydown(Enter& & Tab) [ ^ ]







添加链接文字以反映文章/ QA标题。

[/编辑]


检查这两个解决方案:

如何在DataGridView中使用Enter键作为Tab键 [ ^ ]

如何在Enter上的数据网格视图中将焦点移到下一个单元格上关键新闻事件 [ ^ ]
Check these 2 solutions:
How to use Enter key as Tab key in DataGridView[^]
How to move focus on next cell in a datagridview on Enter key press event[^]


这篇关于如何将Enter键按下事件设置为DataGridView C#中的Tab键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆