|
I have a entry system. If a user enter a itemcode in column1, then the description for that item is displayed in column2. So my next entry is in column3, but I have to press tab twice to get to column3, from column1. How can I move directly to column3 using the tab key? | | desperate1 Tuesday, February 28, 2006 10:31 AM | Hi, Desperate,
You can set the column for “read only�to keep people from typing into it (do this from the “columns�property on the datagridview), but AFAIK there’s no intrinsic way to skip over a column when tabbing.
To do this programmatically, you’d really need to do something like track the KeyDown event on the datagrid view, see if the key is a Tab (or other key which causes you to move to another cell), figure out where you are in the DGV and then set the selection yourself to the appropriate cell. (You might be able to finesse the CellLeave event as well, but that would involve some logic because you wouldn’t want all cell exits under all conditions to force you into the third column.) Note, however, that this would break accessibility �users who use the keyboard to navigate the grid (and there are lots of folks who do that) will be blocked from tabbing to that cell to (for example) copy the description and paste it to some other document.
Hope this helps,
--Matt--* | | Matthew Gertz MS Tuesday, February 28, 2006 9:37 PM | Hi, Desperate,
You can set the column for “read only�to keep people from typing into it (do this from the “columns�property on the datagridview), but AFAIK there’s no intrinsic way to skip over a column when tabbing.
To do this programmatically, you’d really need to do something like track the KeyDown event on the datagrid view, see if the key is a Tab (or other key which causes you to move to another cell), figure out where you are in the DGV and then set the selection yourself to the appropriate cell. (You might be able to finesse the CellLeave event as well, but that would involve some logic because you wouldn’t want all cell exits under all conditions to force you into the third column.) Note, however, that this would break accessibility �users who use the keyboard to navigate the grid (and there are lots of folks who do that) will be blocked from tabbing to that cell to (for example) copy the description and paste it to some other document.
Hope this helps,
--Matt--* | | Matthew Gertz MS Tuesday, February 28, 2006 9:37 PM | I need to do the same thing.
Tracking what key is pressed is easy. But when I try to set the currentcell to another one, I always get exception say the operation is invalid as it causes reentrant call of SetCurrentCellAddress.
Now, how could I change CurrentCell when the DataGrid has the focus? | | HongmeiWang Saturday, March 25, 2006 1:29 AM | I just found the answer. Just override
SetCurrentCellAddressCore
method.
protected override bool SetCurrentCellAddressCore(int columnIndex, int rowIndex, bool setAnchorCellAddress, bool validateCurrentCell, bool throughMouseClick)
{
if (SkipColumn (columnIndex) && !throughMouseClick)
{
return base.SetCurrentCellAddressCore(NewColumn(columnIndex), rowIndex, setAnchorCellAddress, validateCurrentCell, throughMouseClick);
}
return base.SetCurrentCellAddressCore(columnIndex, rowIndex, setAnchorCellAddress, validateCurrentCell, throughMouseClick);
} | | HongmeiWang Saturday, March 25, 2006 1:41 AM | Not a lot of good information on how to do this, but here's what I ended up doing (in my own custom datagrid):
Code Block
private bool mouseClicked = false;
protected override void OnCellEnter(DataGridViewCellEventArgs e)
{
// Check if cell is read-only. If so the force move to next control.
if (this.Rows[e.RowIndex].Cells[e.ColumnIndex].ReadOnly && mouseClicked == false)
{
SendKeys.Send("{TAB}");
}
base.OnCellEnter(e);
}
protected override void OnMouseDown(MouseEventArgs e)
{
mouseClicked = true;
base.OnMouseDown(e);
}
protected override void OnMouseClick(MouseEventArgs e)
{
mouseClicked = false;
base.OnMouseClick(e);
}
This ensures that you can not enter the read-only cell using the tab key, but will still let you enter it using the mouse (in caseyou want to copy values out of that cell). The OnMouseDown event occurs before the OnCellEnter event, and the OnMouseClick event happens after the OnCellEnter event. It isn't pretty but it works.
| | blars Wednesday, December 26, 2007 7:02 PM | hi, please remark, that sendkeys doesn't work under vista and windows 7, if UAC is turned on. | | DerStauner Thursday, July 23, 2009 8:11 PM |
|