如何在C#windows应用程序的datagridview中输入键作为tab键 [英] How enter key behave as tab key in datagridview in C# windows application
本文介绍了如何在C#windows应用程序的datagridview中输入键作为tab键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用了大量的代码,但我不明白输入键的行为方式如同tab键datagridview
我尝试了什么:
i use lots of code but i dont understand how enter key behave as tab key datagridview
What I have tried:
private void dgvSbill_CellEnter(object sender, DataGridViewCellEventArgs e)
{
if (dgvSbill.CurrentRow.Cells[e.ColumnIndex].ReadOnly)
{
SendKeys.Send("{tab}");
}
}
second i used
private void dgvSbill_KeyPress(object sender, KeyPressEventArgs e)
{
switch (e.KeyChar)
{
case (char)Keys.Enter:
SendKeys.Send("{Tab}");
break;
default:
break;
}
}
i also check this process code but how it use i cant understand
using System.Diagnostics;
class MyDataGridView : DataGridView
{
protected override bool ProcessDialogKey(Keys keyData)
{
if (keyData == Keys.Enter) {
base.ProcessTabKey(Keys.Tab);
return true;
}
return base.ProcessDialogKey(keyData);
}
protected override bool ProcessDataGridViewKey(KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter) {
base.ProcessTabKey(Keys.Tab);
return true;
}
return base.ProcessDataGridViewKey(e);
}
}
哪里会用plz回复我非常紧急
提前感谢
where it will use plz reply me it very urgent
thanks in advance
推荐答案
请勿使用SendKeys
。使用关键事件模拟进行UI开发通常被视为滥用;我同意这一点。您的示例只是一个证明这一点的原因。
在您的情况下,只需找出所选内容并选择网格视图的下一个/上一个元素。这取决于选择模式: DataGridView .SelectionMode属性(System.Windows.Forms) [ ^ ]。
假设您要更改所选单元格,如果您使用每单元格选择模式,CellSelect
。然后根据当前单元格的行和列找出选择的单元格,按照您想要的方式计算下一个/上一个单元格,然后选择它。这两件事都是使用属性SelectedCell
完成的: DataGridView.SelectedCells属性(System.Windows.Forms) [ ^ ]。
最有可能的是,你需要它只有一个单元格,所以SelectedCell
不会有多个元素。通过将属性MultiSelect
设置为false来实现: DataGridView.MultiSelect属性(System.Windows.Forms) [ ^ ]。
要捕获Enter,而不是KeyPress
,您应该处理事件KeyDown
。
我有一个更好的主意:不要这样做。DataGridView
已经有了很好的导航和用于更改选择的正确密钥;所有合理的用户都习惯了。
-SA
Don't useSendKeys
. Using key event simulation for UI development is usually considered as abuse; and I agree with that. Your example is just one to justify that.
In your case, just find out what is selected and select next/previous element of the grid view. It depends on the selection mode: DataGridView.SelectionMode Property (System.Windows.Forms)[^].
Let's say, you want to change selected cell, in case you use per-cell selection mode,CellSelect
. Then find out what cell is selected, based on the current cell's row and column, calculate next/previous cell the way you want, and then select it. Both things are done using the propertySelectedCell
: DataGridView.SelectedCells Property (System.Windows.Forms)[^].
Most likely, you will need that it was only one cell, soSelectedCell
won't have more than one element. It is achieved by setting the propertyMultiSelect
to false: DataGridView.MultiSelect Property (System.Windows.Forms)[^].
To capture Enter, instead ofKeyPress
, you should handle the eventKeyDown
.
I have a better idea: don't do it.DataGridView
already has good navigation and proper keys used to change selection; all reasonable users used to them.
—SA
这篇关于如何在C#windows应用程序的datagridview中输入键作为tab键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文