Henry Instruction - DataGrid Event Response
Han Rui (12/31/2002)
Happy new year! Hard work for a year, everyone can relax, although this New Year's Day is only a day. Thank you for the 9CBS staff, Xin Xinking is busy for a year, the server expansion, the website is revised ... I hope to recover in next year, it is better, so that netizens are more happy. I wish you all the best, all the best!
This article mainly introduces the event response of .NET DataGrid control, please refer to the "Windows Form Introduction (2)" on the event response mechanism. This article discusses the DataGrid mouse response and keyboard response problem:
First, the mouse response
Due to the aggregation structure of DataGrid (this has been described in detail in the series of DataGrid structures), the user does not know the response area and the processor of the mouse response. Please see Figure 1:
Figure 1 DataGrid response area named
1. Click Event
If the user wants to click DataGrid to inspire a corresponding event, you can use the DataGrid.hitTest method to get the location where you click on the DataGrid, you must notify you where you click. As follows:
Private sub DataGrid1_mousedown (Byval e AS _BYVAL E AS _
System.windows.Forms.MouseEventArgs Handles DataGrid1.mousedown
DIM MyGrid As DataGrid = CType (Sender, DataGrid)
Dim hti as system.windows.forms.dataGrid.hittestinfo
HTI = MyGrid.hittest (E.x, E.Y)
SELECT CASE HTI.TYPE 'The following shows all the contents of this type enumeration
Case system.windows.forms.DataGrid.hittesttype.none
Msgbox ("You click on the background")
Case system.windows.forms.DataGrid.hittesttype.cell
Msgbox ("The cell you click is in line:" & HTI.ROW & ", column:" & HTI.column)
Case system.windows.Forms.DataGrid.hittesttype.columnheader
Msgbox ("You click" & HTI.COLUMN & "column")
Case system.windows.forms.DataGrid.hittesttype.rtoweer
Msgbox ("You click on the line of" & HTI.ROW & "line")
Case system.windows.forms.DataGrid.hittesttype.columnresize
Msgbox ("You click Border" "& HTI.COLUMN &" ")
Case system.windows.Forms.DataGrid.hittesttype.rowResize
Msgbox ("You click on Border of" & HTI.ROW & "Border")
Case system.windows.forms.DataGrid.hittesttype.caption
Msgbox ("You click on the title")
Case system.windows.Forms.DataGrid.hittesttype.parentrolsmsgbox ("You click Father")
End SELECT
End Sub
2. Double-click the event (thank you for your assistance of acptvb users)
You can get double-click event to get a double-click on the event, you only need to add the following judgment statement in the SUB:
If E.Button = MouseButtons.Left and E.Clicks = 2 THEN
DIM MyGrid As DataGrid = CType (Sender, DataGrid)
...
End SELECT
END IF
You will try again, but find all other normal, only the cell double-click the event, what is the reason? It is very simple, that is, when you first click Cell, DataGrid can also get a message, but click the result that makes the focus fall into the Cell you click, your second mouse click event The respondent is no longer DataGrid, but the TextBox in Cell. (If you don't understand this, please see the DataGrid structure in my column analysis. So how do you notify DataGrid? Is there a second click on the CELL event? First of all, we should clarify that both clicks and double-click, their difference is that the time interval between the mouse click, the interval is a double-click event. After a certain length, it is considered two clicks (time interval) The settings are actually possible, and there is a "mouse" in the "Control Panel" of your machine, there is a tool for adjusting this time interval). After this time is set, you can get it through the SystemInformation.doubleClickTime method.
Now let's take a look:
(1) Set a public variable GridMouseDowntime, used to record the absolute time at the time of clicking;
(2) Record the first click of time in the DataGrid1_MouseDown event handler:
IF hit.type = system.windows.forms.datagrid.hittesttype.cell dam
GridMouseDowntime = datetime.now
END IF
(3) Set the mouse click event (MouseDown) in the TextBox in DataGrid's Cell;
Setting up the method, please see the DataGrid structure in my column analysis, the event handler's setup method, please see the event response section in the Windows Form Introduction in my column, here you don't repeat it:
DIM TS AS New DataGridtableStyle ()
DIM AcolumnTextColumn As DataGridTextBoxColumn
DataGrid1.datasource = DT
Me.henryDataGrid1.datasource = DT
Ts.mappingname = dt.tablename
DIM Numcols as INTEGER
Numcols = dt.columns.count
Do While (i acolumnTextColumn = New DataGridTextBoxColumn () 'Take the control of the cursor in TextBox, but you can still enter the value. AddHandler AcolumnTextColumn.TextBox.mousedown, New Mouseeventhandler_ (Addressof TextBoxMousedownHandler) 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.tables.Add (TS) (4) The time interval is compared in the event handler of the above event, thereby judge whether it is DataGrid double-click on a CELL: Private Sub TextBoxMousedownHandler (Byval e as mouseeventargs) IF (DateTime.now MsgBox (Doucast ") of" DataGrid ") END IF END IF End Sub It is necessary to pay attention to it here. It is of course that you click on the CELL. Because your two clicks were falling in TextBox, it became the double-click event of TextBox, you can add the following code to the textboxmousedownHandler: If E.Button = MouseButtons.Left and E.Clicks = 2 THEN Messagebox.show ("Double click in the TextBox in the cell") Else IF (DateTime.Now Messagebox.show (DoubleClick "in DataGrid) END IF END IF You can improve the above code to achieve your mouse event response. The keyboard event response seems to be discussed next year. I have to buy a dish, otherwise my girlfriend will come back to work. I wish you all a happy new year! ---- 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/