在 winform 中的 datagrid 中实现单元格双击事件
Public Class Member1
Inherits System.Windows.Forms.Form
'Windows 窗体设计器生成的代码
'省略..
Dim mydb As New Db
Dim dgListMouseDownTime As Date
Private Sub Member1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
mydb.daMember.Fill(Me.DsMember1)
Dim dgStyle As New DataGridTableStyle
dgStyle.MappingName = Me.DsMember1.Tables(0).TableName
Me.dgList.TableStyles.Add(dgStyle)
'在第一列上实现双击事件
Dim memberID As DataGridTextBoxColumn = Me.dgList.TableStyles(0).GridColumnStyles(0)
AddHandler memberID.TextBox.MouseDown, AddressOf memberID_MouseDown
End Sub
'如果鼠标定位到单元格,则设置定位到单元格上的时间
Private Sub dgList_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles dgList.MouseDown
Dim hit As DataGrid.HitTestInfo = Me.dgList.HitTest(e.X, e.Y)
If hit.Type = DataGrid.HitTestType.Cell Then
Me.dgListMouseDownTime = Now
End If
End Sub
Private Sub memberID_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
If (e.Button = MouseButtons.Left And e.Clicks = 2) Or (Now < Me.dgListMouseDownTime.AddMilliseconds(SystemInformation.DoubleClickTime)) Then
MessageBox.Show("双击了单元格!")
End If
End Sub
End Class
Inherits System.Windows.Forms.Form
'Windows 窗体设计器生成的代码
'省略..
Dim mydb As New Db
Dim dgListMouseDownTime As Date
Private Sub Member1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
mydb.daMember.Fill(Me.DsMember1)
Dim dgStyle As New DataGridTableStyle
dgStyle.MappingName = Me.DsMember1.Tables(0).TableName
Me.dgList.TableStyles.Add(dgStyle)
'在第一列上实现双击事件
Dim memberID As DataGridTextBoxColumn = Me.dgList.TableStyles(0).GridColumnStyles(0)
AddHandler memberID.TextBox.MouseDown, AddressOf memberID_MouseDown
End Sub
'如果鼠标定位到单元格,则设置定位到单元格上的时间
Private Sub dgList_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles dgList.MouseDown
Dim hit As DataGrid.HitTestInfo = Me.dgList.HitTest(e.X, e.Y)
If hit.Type = DataGrid.HitTestType.Cell Then
Me.dgListMouseDownTime = Now
End If
End Sub
Private Sub memberID_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
If (e.Button = MouseButtons.Left And e.Clicks = 2) Or (Now < Me.dgListMouseDownTime.AddMilliseconds(SystemInformation.DoubleClickTime)) Then
MessageBox.Show("双击了单元格!")
End If
End Sub
End Class