Article ID: 140888
Article Last Modified on 1/8/2003
Const ASC_ENTER = 13 ' ASCII code of ENTER key. Dim gRow As Integer Dim gCol As Integer
Private Sub Grid1_KeyPress (KeyAscii As Integer)
' Move the text box to the current grid cell:
Call grid_text_move(Grid1, Text1)
' Save the position of the grids Row and Col for later:
gRow = Grid1.Row
gCol = Grid1.Col
' Make text box same size as current grid cell:
Text1.Width = Grid1.ColWidth(Grid1.Col) - 2 * Screen.TwipsPerPixelX
Text1.Height = Grid1.RowHeight(Grid1.Row) - 2 * Screen.TwipsPerPixelY
' Transfer the grid cell text:
Text1.Text = Grid1.Text
' Show the text box:
Text1.Visible = True
Text1.ZOrder 0
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
Grid1.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 = Grid1.Row
tmpCol = Grid1.Col
' Set Row and Col back to what they were before Text1_LostFocus:
Grid1.Row = gRow
Grid1.Col = gCol
Grid1.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:
Grid1.Row = tmpRow
Grid1.Col = tmpCol
End Sub
Public Sub grid_text_move (Grid As Control, TextBox As Control)
' Move a text box to the position of the current cell in a grid:
Dim X As Single ' x position of current grid cell.
Dim Y As Single ' y position of current grid cell.
Dim i As Integer ' Column/row index.
' Skip grid border:
X = Grid.Left
Y = Grid.Top
If Grid.BorderStyle = 1 Then
X = X + Screen.TwipsPerPixelX
Y = Y + Screen.TwipsPerPixelY
End If
' Skip fixed columns and rows:
For i = 0 To Grid.FixedCols - 1
X = X + Grid.ColWidth(i)
If Grid.GridLines Then
X = X + Screen.TwipsPerPixelX
End If
Next
For i = 0 To Grid.FixedRows - 1
Y = Y + Grid.RowHeight(i)
If Grid.GridLines Then
Y = Y + Screen.TwipsPerPixelY
End If
Next
' Find current data cell:
For i = Grid.LeftCol To Grid.Col - 1
X = X + Grid.ColWidth(i)
If Grid.GridLines Then
X = X + Screen.TwipsPerPixelX
End If
Next
For i = Grid.TopRow To Grid.Row - 1
Y = Y + Grid.RowHeight(i)
If Grid.GridLines Then
Y = Y + Screen.TwipsPerPixelY
End If
Next
' Move the Text Box, and make small adjustments:
TextBox.Move X + Screen.TwipsPerPixelX, Y + Screen.TwipsPerPixelY
End Sub
Additional query words: 3.00 4.00 vb4win vb416
Keywords: KB140888