Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Flex grid in-cell editing. Any examples?

47 views
Skip to first unread message

jodasi

unread,
Nov 8, 1999, 3:00:00 AM11/8/99
to
im looking for any samples of Flex grid in cell editing, and changing
the backcolor of cells in the flex grid. Any examples?


Sent via Deja.com http://www.deja.com/
Before you buy.

Jody Gelowitz

unread,
Nov 8, 1999, 3:00:00 AM11/8/99
to
This was taken from Visual Basic Books Online:


MSFlexGrid does not have a built-in cell editing
capability, but it provides the hooks to make it
easy for you to add that capability programmatically.
The advantage of this approach is that you can tailor
editing behavior to your taste. The basic technique
involves smoke and mirrors: the editing occurs not in
MSFlexGrid at all, but in a standard Textbox control
that is positioned precisely over the cell beingedited.
In this example, we will give the user two ways to get
into the edit mode, either by double-clicking on a
cell, or by simply starting to type in the current
cell. The following two routines implement this:

Private Sub MSFlexGrid1_DblClick()
GridEdit Asc(" ")
End Sub

Private Sub MSFlexGrid1_KeyPress(KeyAscii As Integer)
GridEdit KeyAscii
End Sub

In each case we call a grid edit subroutine and pass
it a keystroke. In the case of double-clicking, we
pass the space character as a flag. The GridEdit
routine initializes the edit box and moves it intoposition:

Sub GridEdit(KeyAscii As Integer)
'use correct font
Text1.FontName = MSFlexGrid1.FontName
Text1.FontSize = MSFlexGrid1.FontSize
Select Case KeyAscii
Case 0 To Asc(" ")
Text1 = MSFlexGrid1
Text1.SelStart = 1000
Case Else
Text1 = Chr(KeyAscii)
Text1.SelStart = 1
End Select
'position the edit box
Text1.Left = MSFlexGrid1.CellLeft + MSFlexGrid1.Left
Text1.Top = MSFlexGrid1.CellTop + MSFlexGrid1.Top
Text1.Width = MSFlexGrid1.CellWidth
Text1.Height = MSFlexGrid1.CellHeight
Text1.Visible = True
Text1.SetFocus
End Sub

For demonstration purposes, the Case statement in
the GridEdit routine shows two different behaviors
when entering the edit mode. In practice you would
probably only use one of them, or a different one
of your own creation. If the edit mode is entered
by virtue of a double-click or a control key press,
we copy the contents of the grid cell to the exit
box and place the cursor at the end of the string.
If the edit mode is entered by pressing a normal
key, we ignore the original cell contents and
insert the pressed key into the edit box. The
positioning of the exit box could be done on one
line with the Move method. Here we have used four
lines so that it reads more easily in this article.
Notice that MSFlexGrid conveniently gives us all
the coordinate information we need.Next we need a couple of routines that
handle
housekeeping when the user moves to a different cell,
or moves focus back to the grid from another control.
The LeaveCell event is also the place where you would
put any data validation code that might be applicable.

Private Sub MSFlexGrid1_LeaveCell()
If Text1.Visible Then
MSFlexGrid1 = Text1
Text1.Visible = False
End If
End Sub

Private Sub MSFlexGrid1_GotFocus()
If Text1.Visible Then
MSFlexGrid1 = Text1
Text1.Visible = False
End If
End Sub

Next we place some navigation code in the KeyDown event
of the edit box so that, for instance, the user can
leave the edit mode by pressing Escape, and move to a
different row by pressing an arrow key:

Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)
Select Case KeyCode
Case vbKeyEscape
Text1.Visible = False
MSFlexGrid1.SetFocus
Case vbKeyReturn
MSFlexGrid1.SetFocus
Case vbKeyDown
MSFlexGrid1.SetFocus
DoEvents
If MSFlexGrid1.Row < MSFlexGrid1.Rows - 1 Then
MSFlexGrid1.Row = MSFlexGrid1.Row + 1
End If
Case vbKeyUp
MSFlexGrid1.SetFocus
DoEvents
If MSFlexGrid1.Row > MSFlexGrid1.FixedRows Then
MSFlexGrid1.Row = MSFlexGrid1.Row - 1
End If
End Select
End Sub

Finally we need a line of code to suppress the Beep
that occurs when the Enter key is pressed in aTextbox:

Private Sub Text1_KeyPress(KeyAscii As Integer)
'noise suppression
If KeyAscii = vbKeyReturn Then KeyAscii = 0
End Sub

In order for the edit box to merge seamlessly into the
grid, you need to set several Textbox properties at
design-time: set Appearance = 0 (flat), and
BorderStyle = 0 (none). Also set Visible = False so
that the edit box is not initially visible. To really
fine-tune this code, the edit box needs a slight
additional offset to the southeast (with a
corresponding reduction in size) so that the text in it
lines up exactly with the text in the cell beneath. You
would probably also want to write some code behind the
scroll event of the grid since clicking on the grid's
scroll bar will not cause the edit box to loose focus.
Note that this technique is not limited to using a
Textbox as your edit box. You could modify the sample
code to use a ComboBox, a CheckBox, or even a calendar
control for editing, based on the column being edited.

--
Later,
Jody

jo...@visual-statement.com
http://www.visual-statement.com/vb

jodasi wrote in message <807gt4$lam$1...@nnrp1.deja.com>...

0 new messages