Henry Instruction - DataGrid Keyboard Event Response (2)
Han Rui (03/11/2003)
This article is a bit late, and then the previous launch has been almost two months. I saw a net friend of a netizen today, saying that he is still waiting to see the handling of "Enter", and then let go of the hand, Advakers this article, I also hope that netizens will not be too disappointed.
For DataGrid's keyboard event response, we must distinguish between DataGrid itself, or the keyboard event response of the Cell cell, processing method is very different. Everyone can refer to the first method, process the keyboard event in Cell.
But there is a problem in the previous section, how is it to intercept and handle the ENTER key, arrow keys, TAB keys, PGUP / PGDN? General netizens encountered this problem, ten eight nine is for this purpose: I hope to make the cursor in a line in one line in a line (from "name" to "123") when pressing the Enter key (from "Name" to "123"), not immediately To the next line of the same column (to "xxx"). (As shown in Figure 1)
Figure 1 Trim demo
However, we have exhausted the way in the previous one, but it seems that this keyboard response has been encapsulated by the control for protected. The control writer encapsulates multiple predefined keyboard bindings, called shortcuts. We can't intercept the Enter key and other keyboard buttons in KeyDown / KeyPress. The following is a shortcut to hidden:
operating
hot key
Complete the cell input and move to the next cell.
If the focus is on the sub-table link, navigate to the table.
ENTER
If you are in a cell edit mode, the cell editing is canceled.
If you are in subtitle selection mode, you will cancel the editing on the line.
ESC
When editing a cell, delete the characters before the insertion point.
Backspace key
When editing a cell, delete the characters after the insertion point.
DELETE button
Move to the first cell of the current line.
HOME button
Move to the last cell of the current line.
End key
Highlight the characters in the current cell and place the insertion point at the end of the row. The same as double-click cell.
F2 key
If the focus is on a cell, move to the next cell in the row.
If the focus is on the last cell in a row, move to the first sub-table link of the row and expand it.
If the focus is on the sub-link, move to the next sub-link.
If the focus is on the last sub-link, move to the first cell of the next row.
Tab key
If the focus is on a cell, move to the previous cell in the row.
If the focus is on the first cell in a row, move to the last unfolded sub-table link in the previous row, or move to the last cell in the previous row.
If the focus is on the sub-link, move to the previous sub-link.
If the focus is on the first sub-link, move to the last cell of the previous row.
Shift Tab
Move it in the Tab key to the next control.
Ctrl Tab
Press the Tab key to move to the previous control.
Ctrl Shift Tab
If in the sub-table, move up to the parent table. Similarly to the behavior of clicking the "Back" button.
Alt left arrow key
Expand the sub-table link. Alt down arrow keys expand all links, not just selected links.
Alt down arrow keys or CTRL plus key
Folding sub-table link. Alt arrow keys fold all links, not just selected links.
ALT arrow key or Ctrl minus key
Move in the direction of the arrow to the farthest non-empty unit.
Ctrl arrow keys
The selected content is extended in the direction of the arrow (not including the sub-table link). Shift up / down arrow keys
Press the direction of the arrow to extend the selected content to a non-airline (excluding the sub-table link).
Ctrl shift up / down arrow keys
Move to the cell in the upper left corner.
Ctrl home button
Move to cells in the lower right corner.
Ctrl End key
Extend the selected content to the top row.
Ctrl Shift HOME button
Extend the selected content to the bottom line.
Ctrl Shift End button
Select the current row (excluding the sub-table link).
Shift SpaceBAR button
Choose the entire grid (excluding sub-table links).
Ctrl A button
When in the sub-table, the father is displayed.
Ctrl Page Down Key
When in the sub-table, hide the father's line.
Ctrl Page Up
Extend the selected content down a screen (excluding the sub-table link).
SHIFT PAGE DOWN
Extend the selected content upward (not including sub-table link).
SHIFT PAGE UP
Call the DataGrid.Endedit method for the current line.
Ctrl ENTER key
When in edit mode, enter the dbnull value into a cell.
Ctrl 0 key
Shortcuts and menu shortcuts are called command keys, and they are processed during the message pre-processing before processing the regular input. The command key is always priority than the regular input key.
The ProcessCmdKey method first determines if the control has a context menu. If so, the ContextMenu is allowed to process the command key. If the command key is not a menu shortcut, and the control has a parent, then the key is passed to the parent's ProcessCmdKey method. The net effect is that the command key is "take" in the control hierarchy. In addition to the keys pressed, the key data also indicates which (if any) modifies the key to press the button simultaneously. The modification key includes Shift, Ctrl, and Alt keys (becoming a combination key).
Here to note: This method must return True to indicate that it has been processed the command key, or false to indicate that the key is not a command button. When rewriting the ProcessCmdKey method in the derived class, the control should return TRUE to indicate that it has been processed. For keys that are not processed by the control, the results of the processcmdkey method that call the base class should be returned.
If you don't add a return value, you will default to false. This way you have clearly modified the processing method, but will continue to perform the processing method of the keyboard button defined in the parent class after executing your command.
So how do we deal with the proposition of Wen He? The solution is to write a control, inherit from the existing DataGrid control, rewrite the processing command key response program processcmdkey to achieve our needs.
Step 1: In the VS.NET editor, "File" -> "New" -> "Project", then select New Item New A "Windows Control Library": HenryDataGrid. The result of this run will generate a DLL file instead of an Exe execution file;
Step 2: Add this sentence with shadows in the HenryDataGrid.vb file code editing window:
Public Class HenryDataGrid
Inherits System.Windows.Forms.DataGrid 'This means that the new control is DataGrid's derived control.
Step 3: Select Overrides in the Class Name window, then select "ProcessCmdKey" in the Method Name window (shown in Figure 2) Figure 2 Select the method you want to rewritten.
Then there will be an empty processcmdkey code segment, we can write your own code:
Protected Overrides Function ProcessCmdkey (byref msg as system.windows.forms.Meys, Byval KeyData as system.windows.forms.keys) as boolean
DIM WM_KEYDOWN AS INTEGER = 256 'Message Response problem can refer to other Win32 programming articles
DIM WM_SYSKEYDOWN AS INTEGER = 260
IF ((msg.msg = wm_keydown) or (msg.msg = WM_SYSKEYDOWN)) THEN
Select Case Keydata
Case keys.down
MSGBOX ("Ascended Down Arrow Key")
Return True
Case keys.up
MSGBOX ("Ascending Arrow Key")
Return True
Case keys.enter
SendKeys.send ("{tab}")
Return True
Case Keys.Control keys.m
Msgbox ("
Return True
Case keys.alt keys.z
Msgbox ("
Return True
End SELECT
END IF
END FUNCTION
Then run, generate a HenryDataGrid.dll file
Step 4: Build a project, then right-click on the toolbox of the new project design window, select Add References in the pop-up menu, then take up "Browse" in the .NET tab, select HenryDataGrid .dll, join in, and then a HenryDataGrid's icon on your toolbox, use HenryDataGrid in the new project instead of the DataGrid control. Look, the "Enter" event you need is completed.
Recommendation: You remove Return True in the Keys.Enter code to see what happens.
Here is the following, we must only use the Overridable keyword modified protected when rewriting the type of method.
method. This is because in VB is to specify attributes or methods in the derived class in the derived class in the VB. We have no eligibility to rewrite this stuff.
-------------------------
Disclaimer: The right to copyright and interpretation of this article belongs to Han Rui, if you need to reprint, please keep your full content and this statement.
QQ: 18349592
E-mail: Henry7685@hotmail.com
Please visit my column: http://www.9cbs.net/develop/author/netauthor/latitude/