Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help using MSFlexGrid as Spreadsheet

Status
Not open for further replies.

Bohdii

Technical User
Aug 2, 2001
6
CA
I typed out the code in the next post (from Planet Source Code by David Hitchner, thanks David and Josh5414 for the link) in the code window of a visual basic program.

I understand the logic of the code, what it's suppose to do. What I'm having trouble with is getting the code to run with the Form.

I've retyped the code verbatim (rather than copy and pasting - so I could work through the logic) and named my FlexGrid grdEditGrid and the TextBox as txtEditCell. I'm getting the compile error "Member already exists in a object module from which this object module derives"

Could someone please walk me through using this code in a program: how I set up the forms, what to call them, etc. (I'm using VB5).

My eternal thanks in advance! (~This is incredibly frustrating~)

Any help at all would be appreciated, even if you don't have time to go through the whole process.
 
'**************************************
'Windows API/Global Declarations for :Us
' e MSFlexGrid as Spreadsheet
'**************************************
' Row and Column Pointers
Private iGridRow%
Private iGridCol%
' References to Grid and Edit TextBox
Private txtEditCell As TextBox
Private grdEditGrid As MSFlexGrid

Public Sub cellGotFocus()
' Select all text in cell.


With txtEditCell
.SelStart = 0
.SelLength = Len(.Text)
End With
End Sub


Public Function cellKeyDown%(keyCode%)
' Process "arrow" keys.
' All other keys are passed through.


Select Case keyCode
Case vbKeyUp


With grdEditGrid


If .Row > 1 Then
.Row = .Row - 1
.SetFocus
Else
keyCode = 0
End If
End With
Case vbKeyDown


With grdEditGrid


If .Row < (.Rows - 1) Then
.Row = .Row + 1
.SetFocus
Else
keyCode = 0
End If
End With
Case vbKeyLeft


If txtEditCell.SelStart = 0 Then


With grdEditGrid


If .Col > 1 Then
.Col = .Col - 1
.SetFocus
Else
keyCode = 0
End If
End With
End If
Case vbKeyRight


If txtEditCell.SelStart = Len(txtEditCell.Text) Then


With grdEditGrid


If .Col < (.Cols - 1) Then
.Col = .Col + 1
.SetFocus
Else
keyCode = 0
End If
End With
End If
End Select
cellKeyDown = keyCode
End Function


Public Function cellKeyPress%(keyAscii%)
' Process &quot;ENTER&quot; key.
' All other keys are passed through.


If keyAscii = vbKeyReturn Then
grdEditGrid.SetFocus' Set focus back To grid.
keyAscii = 0' Ignore &quot;ENTER&quot; key.
End If
cellKeyPress = keyAscii
End Function


Public Sub cellLostFocus()
Dim iTmpRow%, iTmpCol%


With grdEditGrid
' Save the current settings of grid row
' and col.
' This is needed if the focus is set els
' ewhere
' on the grid.
iTmpRow = .Row
iTmpCol = .Col
' Restore grid row and col to location
' before it lost focus.
.Row = iGridRow
.Col = iGridCol
' Move text back to grid.
.Text = txtEditCell.Text
' Move grid row and col to new position.
'
.Row = iTmpRow
.Col = iTmpCol
End With


With txtEditCell
.SelStart = 0' Move cursor To beginning.
.Visible = False' Hide text box.
End With
End Sub


Public Sub initialize(grid As MSFlexGrid, cell As TextBox)
' Initialize GridEdit Class.
Set grdEditGrid = grid
Set txtEditCell = cell
' Make sure that TextBox is not visible.
'
txtEditCell.Visible = False
End Sub


Public Sub gridKeyPress(keyAscii%)
Dim x!, y!, width%, height%
' Process KeyPress event on grid to ente
' r edit mode.


With grdEditGrid
' Do not enter edit mode if more the one
' cell is selected.
If (.Col <> .ColSel) Or (.Row <> .RowSel) Then Exit Sub
' Save row and col.
iGridRow = .Row
iGridCol = .Col
' Set textbox position skipping the bord
' er.
x = .Left + .CellLeft
y = .Top + .CellTop
' Set textbox size.
width = .CellWidth
height = .CellHeight
End With


With txtEditCell
' Move textbox.
.Move x, y
' Set textbox size.
.width = width
.height = height
' Move grid text.
.Text = grdEditGrid.Text
' Display textbox and set focus.
.Visible = True
.ZOrder 0
.SetFocus
End With
' Redirect KeyPress
If keyAscii <> vbKeyReturn Then SendKeys Chr$(keyAscii)
End Sub

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top