Henry Notes-DataGrid keyboard Event Response (i)

Source: Internet
Author: User
Tags integer first row tostring
datagrid| response 1. The keyboard response on the DataGrid

When the DataGrid is bound to the data source, it presents the form shown in Figure 1. When the DataGrid is focused, the focus falls first on the cell in the first row (note: not in the cell), as shown in Figure N (3). Click the mouse on the cell, and the focus falls into the cell, as shown in Figure N (4).
Fig. 1 The DataGrid focus diagram
What we are discussing in this section are the cases shown in graphs (1) and 1-(2), where the focus falls on the DataGrid's box. The General keyboard event response is in KeyPress, KeyDown, or KeyUp event handlers (the order of the keyboard responses is Keydown->keypress->keyup), and we'll do the same:
Private Sub datagrid1_keypress (ByVal sender as Object, ByVal e As _

System.Windows.Forms.KeyPressEventArgs) Handles datagrid1.keypress

MsgBox ("You entered is:" + e.keychar.tostring)

End Sub
Now run the program, when you click on the keyboard "0-1" "A-Z", "Enter key," backspace key "and so on, will pop up" you enter is ... "dialog box, but when you click" Ctrl "" ALT key "" Shift key "will not respond, click the direction key, When you tab, the focus changes, and the dialog box does not pop up accordingly. In other words, KeyPress cannot intercept the virtual key because Keychar represents some key code in the ASCII table. To intercept the virtual key, we use KeyDown to try:
Private Sub Datagrid1_keydown (ByVal sender as Object, ByVal e As _

System.Windows.Forms.KeyEventArgs) Handles Datagrid1.keydown

MsgBox ("You entered is:" + e.keycode.tostring)

End Sub
OK, we can intercept the "CTRL" "ALT" "Shift" (the combination key problem is solved, right?). And so on, the direction Key, TAB key, PgUp, pgdown Why still ignore us? It looks like the DataGrid controls hid them. Let's go back.

Now please pay attention to such a demand, we can not already intercept the "enter" it? I want to respond to the tab-key event handler when the user types enter. Come on, try it with the KeyDown (KeyPress also do):
Private Sub Datagrid1_keydown (ByVal sender as Object, ByVal e As _

System.Windows.Forms.KeyEventArgs) Handles Datagrid1.keydown

If E.keycode = Keys.enter Then

Sendkeys.send (' {tab} ') ' Notifies the system to call the Tab-key event handler

End If

End Sub
To run the program, we will focus on the position of the diagram (1), knock down the ENTER key, how? Sure enough, the program to enter the key as a tab to execute, focus on the first row of the second column on that cell!

Then I hit the ENTER key again, why, the focus is not beating? Oh, of course not move, because now the focus has been cell access.

2. The keyboard response on the DataGrid cell

The keyboard response on the cell is primarily for the character input that can be displayed (this is also the primary function of the cell). And, of course, the function keys such as backspace/delete/home/end. So how to intercept the keyboard information? The object of the event is now a cell, but we do not see the class that can be used in the "Class name" Drop-down box in the Vs.net code Editor, what is the Keydown/keypress/keyup event?

If you've seen my series of DataGrid anatomy articles, you should be aware that the direct leadership of a cell is "column." So we should use our brains on the list. Let the column undertake this task. The need to do this is to implement the cell's Value box function, which is to enter only the number 0-9 in the cell, and the decimal point "." With the rollback key (backspace key), form_load the code in the parameter declaration see DataGrid structure Analysis (i):
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 < numcols) ' Redraw all columns '

Acolumntextcolumn = New DataGridTextBoxColumn ()

AddHandler aColumnTextColumn.TextBox.KeyPress, New keypresseventhandler (AddressOf column_keypress)
' Let the cell 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) ' Adds a custom table style

......

' Event handlers, which also apply to situations where the other textbox is used for numerical box processing.

Private Sub column_keypress (ByVal sender as Object, _

ByVal e as System.Windows.Forms.KeyPressEventArgs)



MsgBox ("You have pressed" & E.keychar)

If Not (IsNumeric (e.keychar) or E.keychar = ChrW (8) or E.keychar = ChrW ()) Then

e.handled = True

End If
Can you also enter a "A-Z" character in a cell by running the program?

Of course, you can also use the assignment method to achieve:

Declare in the class first (reason see Flops Windows forms Introduction (II)):
Friend WithEvents Column1 as TextBox

Friend WithEvents Column2 as TextBox
Then write the code in Form_Load as follows (see also the parameter declaration of the DataGrid Anatomy (i)):
Dim Mygridtablestyle as DataGridTableStyle = New DataGridTableStyle ()

mygridtablestyle.mappingname = dt. TableName

DATAGRID1.TABLESTYLES.ADD (Mygridtablestyle)

Dim Tempcolumn as DataGridTextBoxColumn

Tempcolumn = datagrid1.tablestyles (0). GridColumnStyles (0)

Column1 = Tempcolumn.textbox

Tempcolumn = datagrid1.tablestyles (0). GridColumnStyles (1)
Event handlers:
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 ()) 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" & E.keycode)

End Sub
Run the program, you will find that the first column has implemented the Value box function, the second column will be after receiving the keyboard request to report the user pressed what key. In this case, we can easily deal with the cells in each column we want (whether it is Keypress,keydown, or other textbox can respond to events, we can handle it!) What a cool word! )

Continuing the issues raised in section I, when we hit the "Enter" button when the cell gets a response, we find two things: (1) when the contents of the cell are not changed, press this key without any response; (2) when the contents of the cell are changed, press this key, The focus falls into the cell in the next row of the same column.

We intercept the "enter key" in the above event handler, but we can't intercept it, whether it's keypress or keydown!. Those in the first section of the helpless virtual key: Direction key, TAB key, PGUP/PGDN, of course, still can not intercept. What to do? Where have they been hiding? Let's talk about it next time, stay tuned!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.