Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 769
  • Last Modified:

Flex Grid Cut and Paste

I have a problem with a flex grid, I don't know how to retreive the coordinates that the user is in when he/she selects a cell. I need this information to do cut and paste.

If you have any algorithms for cut and paste for a flex grid please send it to me.
0
aomega
Asked:
aomega
1 Solution
 
daveamourCommented:
Private Sub Form_Load()
MSFlexGrid1.AddItem "1" & vbTab & "2" & vbTab & "3" & vbTab & "4"
MSFlexGrid1.AddItem "1" & vbTab & "2" & vbTab & "3" & vbTab & "4"
MSFlexGrid1.AddItem "1" & vbTab & "2" & vbTab & "3" & vbTab & "4"

End Sub

Private Sub MSFlexGrid1_Click()
MsgBox "X=" & MSFlexGrid1.Row & vbCrLf & "Y=" & MSFlexGrid1.Col
End Sub

Does this help?
0
 
FarewellIIArmsCommented:
Hi,

have a look at the ColSel & RowSel Properties, help says these are for:

You can use these properties to select a specific region of the MSHFlexGrid programmatically, or to read the dimensions of an area that the user selects into code.

0
 
mcriderCommented:
Here you Go...

The two subroutines are CopyFromGrid and PasteToGrid.  Basically they work this way... You have a flexgrid called "MSFlexGrid1".

A user highlights a set of cells and then hits CTRL-C, clicks on another cell and hits CTRL-V.

In the keypress event of the MSFlexGrid1 object, you intercept the CTRL-C and CTRL-V and call the subroutines like this:

'_______________________________________________________________________________
    Private Sub MSFlexGrid1_KeyPress(KeyAscii As Integer)
        Select Case KeyAscii
            Case 3 'CTL-C (Copy)
                CopyFromGrid MSFlexGrid1
                KeyAscii = 0
            Case 22 'CTL-V (Paste)
                PasteToGrid MSFlexGrid1
                KeyAscii = 0
        End Select
    End Sub
'_______________________________________________________________________________



Here are the subroutines:

'_______________________________________________________________________________
    Public Sub CopyFromGrid(GridObject As Object, _
        Optional StartRow As Variant, Optional EndRow As Variant, _
        Optional StartCol As Variant, Optional EndCol As Variant)
        'THIS FUNCTION WILL COPY THE SELECTED CELLS FROM A FLEXGRID TO THE CLIPBOARD
        'IF StartRow AND StartCol ARE NOT SPECIFIED, THE CURRENT GRID ROW/COL ARE USED
        'IF EndRow AND EndCol ARE NOT SPECIFIED, THE CURRENT GRID ROWSEL/COLSEL ARE USED
        Dim Row As Integer
        Dim Col As Integer
        Dim sRow As Integer
        Dim sCol As Integer
        Dim eRow As Integer
        Dim eCol As Integer
        Dim R1 As Integer
        Dim R2 As Integer
        Dim C1 As Integer
        Dim C2 As Integer
        Dim ClipString As String
        ClipString = "[GRIDCLIP]" + vbFormFeed
        With GridObject
            sRow = .Row: If IsMissing(StartRow) = False Then sRow = StartRow
            sCol = .Col: If IsMissing(StartCol) = False Then sCol = StartCol
            eRow = .RowSel: If IsMissing(EndRow) = False Then eRow = EndRow
            eCol = .ColSel: If IsMissing(EndCol) = False Then eCol = EndCol
            If sCol > eCol Then
                C1 = eCol: C2 = sCol
            Else
                C1 = sCol: C2 = eCol
            End If
            If sRow > eRow Then
                R1 = eRow: R2 = sRow
            Else
                R1 = sRow: R2 = eRow
            End If
            For Row = R1 To R2
                For Col = C1 To C2
                    ClipString = ClipString + .TextMatrix(Row, Col) + vbVerticalTab
                Next Col
                ClipString = ClipString + vbFormFeed
            Next Row
            Clipboard.Clear
            Clipboard.SetText ClipString, vbCFText
        End With
    End Sub
    Public Function PasteToGrid(GridObject As Object, Optional StartRow As Variant, Optional StartCol As Variant) As Boolean
        'THIS FUNCTION WILL PASTE THE CLIPBOARD CONTENTS OF A CopyFromGrid CALL
        'IF StartRow AND StartCol ARE NOT SPECIFIED, THE CURRENT GRID ROW/COL ARE USED
        'THIS FUNCTION WILL RETURN TRUE IF SUCCESSFUL
        Dim Row As Long
        Dim Col As Long
        Dim sRow As Long
        Dim sCol As Long
        Dim ClipString As String
        Dim ClipLine As String
        Dim Cindex As Long
        On Error Resume Next
        PasteToGrid = False
        ClipString = Clipboard.GetText
        If Not (Left$(ClipString, 11) = "[GRIDCLIP]" + vbFormFeed) Then Exit Function
        ClipString = Mid$(ClipString, 12)
        With GridObject
            sRow = .Row: If IsMissing(StartRow) = False Then sRow = StartRow
            sCol = .Col: If IsMissing(StartCol) = False Then sCol = StartCol
            Row = sRow
            Do While Not (ClipString = "")
                Cindex = InStr(1, ClipString, vbFormFeed)
                ClipLine = Left$(ClipString, Cindex - 1)
                ClipString = Mid$(ClipString, Cindex + 1)
                Col = sCol
                Do While Not (ClipLine = "")
                    Cindex = InStr(1, ClipLine, vbVerticalTab)
                    .TextMatrix(Row, Col) = Left$(ClipLine, Cindex - 1)
                    If Not (Err = 0) Then Exit Function
                    ClipLine = Mid$(ClipLine, Cindex + 1)
                    Col = Col + 1
                Loop
                Row = Row + 1
            Loop
        End With
        PasteToGrid = True
    End Function
'_______________________________________________________________________________


By the way, don't you think this is worth more than 30 points... ;-)


Cheers!.)


0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now