在DataGridView中按住箭头键无法平滑移动光标 [英] Holding down arrow keys in DataGridView not moving cursor smoothly

查看:143
本文介绍了在DataGridView中按住箭头键无法平滑移动光标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我在聚焦于DataGridView时按住上方向键或下方向键,则光标(蓝色突出显示的单元格)在行上的移动不均匀,而是跳了几行一次分批。有时,直到最终释放键,它似乎甚至都没有移动,然后光标直接跳到了大概已经存储了所有按键的前几行。

If I press and hold down either the UP or DOWN arrow key when focused on a DataGridView, the cursor (blue highlighted cell) doesn't move evenly down the rows, instead it jumps several rows at a time in batches. Sometimes it doesn't even seem to move at all until the key is finally released, then the cursor jumps directly to several rows ahead having presumably stored all the key presses.

这是一张GIF图片,显示了按住DOWN箭头,然后释放并按下并按下

此动作看起来是错误的,使用户难以判断何时释放键才能在多行中顺利导航。每次跳转似乎都不会移动很多行(例如每次跳转10-20行,有时是整页,甚至会在用户最终释放键之后重新出现很多行)。

This movement looks wrong and makes it hard for the user to judge when to release the key to navigate smoothly over several rows. It doesn't even seem to move an number of rows on each jump it makes (like 10-20 rows on each jump, sometimes the whole page, or even reappearing many rows beyond when the user finally releases the key).

例如,如果用户希望将光标向下移动到页面的一半,则可以通过按下DOWN箭头键并保持不放来进行移动,因此很难知道何时释放键,因为光标没有均匀地向下移动。

For example if the user wants to move the cursor about half way down the page, by pressing the DOWN arrow key and holding it down, then it is very hard to know when to release the key because the cursor is not moving down in an even progress.

为什么不能一次又一次地平滑地重复移动到每一行

Why doesn't it visibly move to each row one at a time in a smooth repeating manner, instead of doing ugly hopping over batches of rows?

这种方式可以重现如下:

This can be reproduced as follows:


  • 新建一个表单项目。

  • 将DataGridView控件拖放到表单上。

  • 在设计器中添加一个DataGridViewTextBoxColumn。 / li>
  • 在FormLoad事件中向DataGridView添加一些行,并添加一些格式以使足够的行对您的测试可见:

  • Make a new forms project.
  • Drop a DataGridView control onto the form.
  • Add a DataGridViewTextBoxColumn in the designer.
  • Add some rows to the DataGridView, and add a little formatting to make enough rows visible for your test, in the FormLoad event:

private void Form1_Load(object sender, EventArgs e)
{
    dataGridView1.RowTemplate.Height = 14;
    dataGridView1.Rows.Add(50);
    dataGridView1.Dock = DockStyle.Fill;
}


然后运行程序并按住向下箭头键,查看蓝色突出显示如何奇怪地跳下来,而不是在行与行之间平稳地向下移动。

Then run the program and press and hold the DOWN arrow key to see how the blue highlight jumps down strangely instead of moving smoothly down from row to row.

即使DataGridView是只读的,也存在奇怪的行为。

The strange behaviour exists even when the DataGridView is read only.

我尝试制作网格和DoubleBuffered形式,但没有成功。我还尝试将网格设置为虚拟模式和ReadOnly,并几乎更改了网格的每个设置,但没有任何效果。

I have tried making the grid and the form DoubleBuffered but no success. I have also tried setting the grid to Virtual mode and ReadOnly and changing just about every setting of the grid, but nothing works.

我发现制作网格的唯一方法光标可以平滑,快速地在行中上下移动而不会跳过成批的行,这是为了不让DataGridView聚焦。为此,我在窗体上进行了另一个控制(例如,一个文本框,并将焦点放在该文本框上)。然后,我重写表单上的processCmdKey方法以捕获键盘输入,并且如果它检测到箭头键,则以编程方式更改DataGridView中的当前单元格(例如,如果在processCmdKey中检测到DOWN键,则更改为下一行)。这将移动当前单元格,而无需真正关注DataGridView。然后,高光的移动似乎会均匀地进行,并且不会突然跳下。

The only way I have discovered to make the cursor to move smoothly and rapidly up or down the rows without jumping over batches of rows, has been to not let the DataGridView be focused. To do this I make another control on the form (for example a textbox, and give focus to the text Box). Then I override the processCmdKey method on the form to capture the keyboard input and if it detects an arrow key then I programmatically change the current cell in the DataGridView (for example to the next row if the DOWN key was detected in processCmdKey). This moves the current cell without ever giving actual focus to the DataGridView. The movement of the highlight then appears to progress evenly and doesn't jump down in strange batches.


  • 在表格上添加一个文本框

  • 将此代码添加到表单中:

  • Add a text box onto the form
  • Add this code to the form:

protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
    int currentRowIndex = dataGridView1.CurrentCell.RowIndex;
    switch (keyData)
    {
        case Keys.Up:
            // Check not already at the first row in the grid before moving up one row
            if (currentRowIndex > 0)
                dataGridView1.CurrentCell = dataGridView1.Rows[currentRowIndex - 1].Cells[dataGridView1.CurrentCell.ColumnIndex];
            return true;
        case Keys.Down:
            // Check not already at the last row in the grid before moving down one row
            if (currentRowIndex + 1 < dataGridView1.Rows.Count)
                dataGridView1.CurrentCell = dataGridView1.Rows[currentRowIndex + 1].Cells[dataGridView1.CurrentCell.ColumnIndex];
            return true;
    }

    // Line below is reached if we didn't handle the key in this method, it tells the form/control to handle it
    return base.ProcessCmdKey(ref msg, keyData);
}


然后运行程序再次进行如下测试:

Then run the program to test again as follows:


  • 单击文本框使其成为焦点(而不是网格)。然后按住DOWN(向下)键,以查看突出显示的蓝色当前单元格在各行中向下平滑地进行。释放键。

  • 然后单击网格以使其成为焦点,然后按住向上或向下箭头键,见证蓝色突出显示跨越几行而不是平稳进行。

因此,只要DataGridView具有焦点,就存在问题,无论网格是处理按键还是在processCmdKey中处理按键。

So the problem exists whenever the DataGridView has focus, regardless of whether the grid handles the keys presses or if I handle them in processCmdKey.

我不想依赖于使焦点远离网格,因为有时网格需要获得焦点。我还必须向processCmdKey添加更多代码,以处理网格的所有其他可能的按键操作,例如选项卡,上/下翻页,输入等,这似乎并不明智。

I don't want to rely of keeping focus away from the grid, because there are times when a grid needs to have focus. I'd also have to add a lot more code to processCmdKey to handle all the other potential key presses for a grid, such as tab, page up / down, enter etc., which doesn't seem sensible.

网格实际上只会在我的应用程序中读取,因此可能实际上只是我需要使用的平滑移动能力,但尽管如此,它还是具有一个集中控制的虚拟控件,而不是

The grid will actually be read only in my application, so it is probably only really the ability to move around smoothly that I need to use, but nevertheless having a dummy control that is focussed instead of the grid doesn't seem sensible.

这里提到了一个稍微相似的问题( DataGrid不能平滑滚动),但它不是Windows Forms,原始海报似乎表明他的问题已通过消除所有事件处理并打开虚拟化得到解决,但是我我尝试过虚拟模式,但我不处理任何网格事件。他还说自己在非常繁忙的服务器上。因此,我认为这可能值得我将其添加为单独的问题。

A slightly similar problem was mentioned here (DataGrid not scrolling smoothly), but it is not Windows Forms and the original poster seemed to state that his problem was fixed by getting rid of all event handling and turning on virtualization, but I've tried virtual mode and I am not handling any events of the grid. He also states that he was on a very busy server. So I think this probably merits me adding this as its own separate question.

任何帮助将不胜感激。谢谢:)

Any help would be much appreciated. Thanks :)

推荐答案

现在,通过更改计算机上的键盘重复速度设置,解决了该问题。我去了控制面板轻松访问|更改键盘的工作方式向下滚动并选择另请参阅:键盘设置。然后,这将显示一个名为键盘属性的窗口,在该窗口中我不得不降低重复率,我将滑块从最大值(略微偏离最大值的四分之一)略微移动了一下,然后单击应用。

The problem has now been solved by changing the keyboard repeat speed settings on the computer. I went to Control Panel | Ease of Access | Change How Your Keyboard Works | Scroll down and choose 'See also: Keyboard Settings'. This then displays a window called 'Keyboard Properties', where I had to lower the 'Repeat Rate', I moved the slider slightly form Maximum (about a quarter of the way from maximum) and clicked apply.

当我重新测试该程序时,它正常工作,并且当我按住向下箭头键时,光标平滑地向下移动到网格中。

The when I re-tested the program, it worked normally and the cursor moved smoothly down the grid when I pressed and held the Down arrow key.

所以我猜想我的PC有点奇怪,在使用DataGridView的情况下,当使用最大键盘重复率时,它无法很好地应对。

So I am guessing that there is something strange about my PC that made it not able to cope well when using the maximum keyboard repeat rate, in the case of a DataGridView.

在理想的情况下,我可以使用设置为最大值的非常快的键盘重复率,但是我很高兴为降低键盘重复率而感到满意,以便顺利使用方向键来解决我的问题一个数据网格视图。

In an ideal world, I'd be able to use a really fast keyboard repeat rate set to maximum, but I am happy enough to settle for a slower keyboard repeat rate in order to solve my problem using the arrow keys smoothly on a DataGridView.

@Sinatr,再次感谢您通过在您的PC上测试代码来帮助我,只是告诉我您无法复制它,并且我可能有一个我的电脑而不是我的代码有问题,是一个很大的帮助。

@Sinatr, thanks again for helping me by testing the code on your PC, just telling me that you couldn't reproduce it and that I probably had a problem with my PC rather than my code, was a big help.

这篇关于在DataGridView中按住箭头键无法平滑移动光标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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