Henry Instruction - DataGrid Keyboard Event Response (1)
Han Rui (01/19/2003)
Hello everyone! Have you written down your New Year plan for your New Year, and urge you to do something? My most hoped is of course a support for you to support me, let me more powerfully write more and better articles dedicated to friends who care about .NET.
Previous article on DataGrid's mouse response gives you the characteristics of the DataGrid event response, one of the important issues is to distinguish between the DataGrid event or the event in the cell, the response object is different. Today we discuss the keyboard event, please also pay attention to the relationship between them.
1. Keyboard response on DataGrid
When the DataGrid is bound to the data source, the form shown in FIG. 1 will be present. When DataGrid is focused, the focus will first fall on the cells of the first column of the first row (note: not even in the cell), as shown in Figure 1- (3). In this unit, click on the mouse to appear in Figure 1- (4), the focus falls into the cell.
Figure 1 DataGrid focus map
We discussed in this section, as shown in Figure 1- (1) and 1- (2), that is, the focus is on the framework of the DataGrid. The general keyboard event response is in the KEYPRESS, KeyDown, or Keyup event handler (the order in which the keyboard response is keydown-> keypress-> keyup), we are also letting the artillery:
Private sub DataGrid1_KeyPress (Byval E AS _BYVAL E AS _
System.windows.Forms.KeyPressEventArgs) Handles DataGrid1.KeyPress
Msgbox ("You entered:" E.Keychar.toString)
End Sub
Now run the program, when you click on "0-1" "AZ", "Enter", "Enter", "Enter", "Enter", "Enter", "you entered ..." dialog, but when you click "Ctrl The key "" ALT key "" Shift key "will have no reactions, click the arrow keys, the focus changes accordingly, and the dialog box will not popup. That is, KeyPress cannot be cut off because Keychar is represented by keycases in the ASCII code table. To cut off, we try to use KeyDown:
Private sub DataGrid1_keydown (Byval E AS _BYVAL E AS _
System.windows.forms.keyeventargs) Handles DataGrid1.Keydown
Msgbox ("You entered:" E.KeyCode.toString)
End Sub
It's time to get "Ctrl" "ALT" "SHIFT button" (the key key problem is not shown, right?), Etc., the direction keys, TAB keys, pgup, pgdown Why ignore we? It seems that the DataGrid control hides them, how to deal with? Let's take another way.
Now please pay attention to such a need, is we not already cut "Enter"? I want to respond to the event handler of the Tab key when the user is typed. Come on, try to try with KeyDown (KeyPress):
Private sub DataGrid1_keydown (Byval e as _system.windows.forms.keyeventargs) Handles DataGrid1.Keydown
If E.Keycode = Keys.enter Then
SendKeys.send ("{tab}") 'Notifying the system to call the TAB key event handler
END IF
End Sub
Run the program, we will focus on the position of Figure 1- (1), knocking the Enter key, how? Sure enough, the Enter key was executed as a TAB button, and the focus fell into the second column of the first line of the cell!
Then I will knock the Enter key, why, how can I don't be jumped? Oh, of course, it will not be moved, because the focus has been obtained by a cell.
2. DataGrid cell keyboard response
The keyboard response in cells is primarily for characters that can be displayed (this is also the main role of cells!), Of course, there is a function key such as Backspace / delete / Home / End. So how do you cut the keyboard information? The object of the event is now a cell, but we can't see the classes you can use in the "class name" drop-down box in the vs.net code editor, what is the keydown / keypress / keyup event?
If you have seen the DataGrid structure of the DataGrid structure, you should be very clear that the direct leadership of the cell is "column". So we should move your brains in the column. Let the list undertake this task. The demand for us is that the value of the value of the cell can only enter the number 0-9, and the decimal point "," and the backspace key), the parameter declaration of the code in the Form_Load, in the cell. Analysis of DataGrid Structure (1):
DIM TS AS New DataGridtableStyle ()
DIM AcolumnTextColumn As DataGridTextBoxColumn
DataGrid1.datasource = DT
Ts.mappingname = dt.tablename
DIM Numcols as INTEGER
Numcols = dt.columns.count
DIM I as integer = 0
Do While (i acolumnTextColumn = New DataGridTextBoxColumn () AddHndler AcolumnTextColumn.TextBox.Keypress, New KeyPressEventHandler (Addressof Column_KeyPress "Let the cells in the column respond to the keypress event AcolumnTextColumn.Headertext = DT.COLUMNS (i) .Columnname AcolumnTextColumn.mappingname = dt.columns (i) .columnname Ts.GridColumnStyles.add (acolumntextColumn) 'Add a custom Column style i = (i 1) Loop DataGrid1.tableStyles.Add (TS) 'Add a custom table style ...... 'Event handler, the same applies to other TEXTBOX for numeric box processing. Private sub column_keypress (Byval Sender as Object, _ Byval e as system.windows.Forms.keypressEventArgs) msgbox ("You Have Pressed The" & E.Keychar) IF NOT (ISNUMERIC (E.Keychar) OR E.Keychar = Chrw (8) or E.Keychar = Chrw (46)) THEN e.handled = true END IF Run a program, can you enter the character such as "A-Z" in the cell? Of course, you can also use the assignment method to implement: Declaration in the class (for reasons "Windows Form Introduction (2)": Friend Withevents Column1 As TextBox Friend Withevents Column2 As TextBox Then write code in Form_Load as follows (see also seeing the parameter declaration of DataGrid structure (1): Dim MyGridtableStyle As DataGridtableStyle = New DataGridTableLestyle () MyGridtablestyle.mappingname = dt.tablename DataGrid1.tables.add (MyGridTableStyle) DIM Tempcolumn as DataGridTextBoxColumn TempColumn = DataGrid1.tables (0) .GridColumnStyles (0) Column1 = TempColumn.TextBox TempColumn = DataGrid1.tables (0) .GridColumnStyles (1) ...... Event handler: Private sub column1_keypress (Byval Sender as object, _ Byval e as system.windows.Forms.KeyPressEventArgs) Handles Column1.KeyPress IF NOT (ISNUMERIC (E.Keychar) OR E.Keychar = Chrw (8) or E.Keychar = Chrw (46)) THEN e.handled = true END IF End Sub Private sub column2_keydown (byval sender as object, _ Byval e as system.windows.forms.keyeventargs_ Handles Column2.Keydown Msgbox ("You Have Pressed The" & E.Keycode) End Sub Run a program, you will find that the first column has implemented a numeric box function, and the second column will report what the button is pressed after receiving the keyboard request. For such a process, we can easily handle our handles for each column (whether it is keypress, keydown, or other Textbox can respond events, we can handle it! How to be cool! ) Continue the question of the first section, we have two cases when you get the response when you get a response: (1) When the content of the cell does not change, press this button without any response; (2) After changing the content of the cell, press this button, the focus will fall into the cell in the same column. We intercept the "Enter" in the above event handler, but it is not arrived, whether it is KeyPress or keydown! In those first quarters, there is no virtual key: arrow keys, TAB keys, pgup / pgdn, of course, still not cut. How to do? Where did they hide? Let's discuss it next time, please continue to pay attention! ---- 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/