为了不让自己下岗,开始学习编程先从VB开始,然后到VB.NET,在这里收集一些编程的源程序和编程技巧,为了自己方便查看,也为了不想下岗而学习的编程的同志们!
今天先发一个在MSFLEXGRID控件中实现单元格的编辑
[@more@]Const ASC_ENTER = 13 '回车
Dim gRow As Integer
Dim gCol As Integer
Private Sub MSFlexGrid1_KeyPress(KeyAscii As Integer)
' Move the text box to the current grid cell:
Text1.Top = MSFlexGrid1.CellTop + MSFlexGrid1.Top
Text1.Left = MSFlexGrid1.CellLeft + MSFlexGrid1.Left
' Save the position of the grids Row and Col for later:
gRow = MSFlexGrid1.Row
gCol = MSFlexGrid1.Col
' Make text box same size as current grid cell:
Text1.Width = MSFlexGrid1.CellWidth - 2 * Screen.TwipsPerPixelX
Text1.Height = MSFlexGrid1.CellHeight - 2 * Screen.TwipsPerPixelY
' Transfer the grid cell text:
Text1.Text = MSFlexGrid1.Text
' Show the text box:
Text1.Visible = True
Text1.ZOrder 0 ' 把 Text1 放到最前面!
Text1.SetFocus
' Redirect this KeyPress event to the text box:
If KeyAscii <> ASC_ENTER Then
SendKeys Chr$(KeyAscii)
End If
End Sub
Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii = ASC_ENTER Then
MSFlexGrid1.SetFocus ' Set focus back to grid, see Text_LostFocus.
KeyAscii = 0 ' Ignore this KeyPress.
End If
End Sub
Private Sub Text1_LostFocus()
Dim tmpRow As Integer
Dim tmpCol As Integer
' Save current settings of Grid Row and col. This is needed only if
' the focus is set somewhere else in the Grid.
tmpRow = MSFlexGrid1.Row
tmpCol = MSFlexGrid1.Col
' Set Row and Col back to what they were before Text1_LostFocus:
MSFlexGrid1.Row = gRow
MSFlexGrid1.Col = gCol
MSFlexGrid1.Text = Text1.Text ' Transfer text back to grid.
Text1.SelStart = 0 ' Return caret to beginning.
Text1.Visible = False ' Disable text box.
' Return row and Col contents:
MSFlexGrid1.Row = tmpRow
MSFlexGrid1.Col = tmpCol
End Sub
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/97706/viewspace-932536/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/97706/viewspace-932536/