How to Edit Grid Cells in VB Using Overlapped Text Box

ID Number: Q85109

1.00

WINDOWS

Summary:

The Grid custom control does not provide any text editing capability.

The example program below shows how you can use a text box to perform

text editing in the current cell of a grid.

This information applies to the Grid custom control provided with

Microsoft Professional Toolkit for Microsoft Visual Basic programming

system version 1.0 for Windows.

More Information:

The example program below allows you to edit the contents of a grid

cell. When you press a key, the grid moves a text box to the position

of the current cell and sets the focus to the text box. When you press

the ENTER key, the text box transfers the text and the focus back to

the grid.

The grid and text box overlap. Ordinarily, overlapping controls are

not supported in Visual Basic. Overlapping controls do not work well

because an overlapped control that has the focus does not necessarily

appear on top of the other control. This program works around this

problem by redrawing the text box by setting the Visible property to

False (0), then to True (-1).

Steps to Create Example Program

-------------------------------

1. Run Visual Basic, or from the File menu, choose New Project (ALT,

F, N) if Visual Basic is already running. Form1 is created by

default.

2. From the File menu, choose Add File, and select GRID.VBX. The Grid

tool appears in the Toolbox.

3. Place a grid (Grid1) on Form1.

4. Place a text box (Text1) on Form1.

5. Set the Grid1 Cols and Rows properties both to 4, and size the grid

to show all the cells.

6. Set the Text1 BorderStyle property to None (0) and the Visible

property to False (0).

7. Enter the following declarations in the general Declarations

section:

Const ASC_ENTER = 13 ' ASCII code of ENTER key

Const FALSE = 0

Const TRUE = Not FALSE

Dim twips_per_pixel As Integer

8. Enter the following code to the Form_Load procedure:

Sub Form_Load ()

' Calculate the number of twips per pixel

Form1.ScaleMode = 3 ' pixel units

pixels% = Form1.ScaleWidth

Form1.ScaleMode = 1 ' twip units

twips% = Form1.ScaleWidth

twips_per_pixel = twips% / pixels%

End Sub

9. Enter the following code in the Grid1_KeyPress procedure:

Sub Grid1_KeyPress (KeyAscii As Integer)

' Move the text box to the current grid cell

Call grid_text_move(grid1, text1)

' Make text box same size as current grid cell

Text1.Width = Grid1.ColWidth(Grid1.Col) - 2 * twips_per_pixel

Text1.Height = Grid1.RowHeight(Grid1.Row) - 2 * twips_per_pixel

' Transfer the grid cell text

Text1.Text = Grid1.Text

' Redirect this KeyPress event to the text box

Text1.Visible = TRUE

Text1.SetFocus

If KeyAscii <> ASC_ENTER Then

SendKeys Chr$(KeyAscii)

End If

' Re-draw the text box to display the result of the SendKeys.

' This works around re-draw problems due to overlapping controls.

Text1.Tag = "re-drawing" ' prevent recursion in Text1_LostFocus

Text1.Visible = 0

Text1.Visible = -1

Text1.SetFocus

End Sub

10. Add the following code to the Text1_KeyPress procedure:

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

11. Add the following code to the Text1_LostFocus procedure:

Sub Text1_LostFocus ()

' To prevent endless recursion, do not process this LostFocus

' event if the text box is losing focus due to being re-drawn.

' (when text1.Tag = "re-drawing").

If Text1.Tag = "" Then

Grid1.Text = Text1.Text ' transfer text back to grid

Text1.SelStart = 0 ' return caret to beginning

Text1.Visible = FALSE ' disable text box

End If

Text1.Tag = ""

End Sub

12. In the general Declarations section or in a separate .BAS file,

add the following Sub routine:

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 + twips_per_pixel

y = y + twips_per_pixel

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 + twips_per_pixel

End If

Next

For i = 0 To Grid.FixedRows - 1

y = y + Grid.RowHeight(i)

If Grid.GridLines Then

y = y + twips_per_pixel

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 + twips_per_pixel

End If

Next

For i = Grid.TopRow To grid.Row - 1

y = y + Grid.RowHeight(i)

If Grid.GridLines Then

y = y + twips_per_pixel

End If

Next

' Move the Text Box, and make small adjustments

TextBox.Move x + twips_per_pixel, y + twips_per_pixel

End Sub

13. Press F5 to run the program. Press a key to begin entering text

into a cell. Type in some text. Press ENTER to finish editing the

cell. Use the arrow keys to move to another cell. You can press

enter to begin editing a cell without changing the contents of the

cell.

Additional reference words: 1.00