MSFlexGrid Edit VB6

chotai.mit picture chotai.mit · Dec 23, 2012 · Viewed 24.3k times · Source

Currently I am working on vb6 application. I want to show data in MSFlexgrid But there is no edit Facility in MSFlexgrid Control.

Is there Any way to Edit MSFlexgrid?

Answer

chotai.mit picture chotai.mit · Dec 23, 2012

There is a way using hidden Textbox. On the double click on the cell the textbox will be visible and Edit is possible here is code snippet check it

Private Sub Form_Load()

   'Setting Col And row

    MSFlexGrid1.Cols = 3
    MSFlexGrid1.Rows = 10
    'First row
    MSFlexGrid1.TextMatrix(0, 0) = "ID"
    MSFlexGrid1.TextMatrix(0, 1) = "Date"
    MSFlexGrid1.TextMatrix(0, 2) = "Voucher Type"
    'some data
    MSFlexGrid1.TextMatrix(1, 0) = "E0000001"
    MSFlexGrid1.TextMatrix(2, 0) = "E0000001"
    MSFlexGrid1.TextMatrix(1, 1) = "01/04/10"
    MSFlexGrid1.TextMatrix(2, 1) = "01/04/10"    
    MSFlexGrid1.TextMatrix(1, 2) = "Jrnl"
    MSFlexGrid1.TextMatrix(2, 2) = "Jrnl"
End Sub

Private Sub MSFlexGrid1_DblClick()

    'If MSFlexGrid1.Col = 3 Or MSFlexGrid1.Col = 6 Or MSFlexGrid1.Col = 7 Then
        GridEdit Asc(" ")
    'End If
End Sub

Private Sub MSFlexGrid1_KeyPress(KeyAscii As Integer)

        GridEdit KeyAscii

End Sub

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.text = Trim(Text1.text)
        Text1.SelStart = 1000

     Case Else
         Text1 = MSFlexGrid1
         Text1.text = Trim(Text1.text)
        Text1.SelStart = 1000

 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

Private Sub MSFlexGrid1_LeaveCell()

  If Text1.Visible Then

    If MSFlexGrid1.Col = 6 Or MSFlexGrid1.Col = 7 Then
        If Text1.text = "" Then
            Text1.text = " "
        End If
    End If
     MSFlexGrid1 = Text1
     Text1.Visible = False

  End If

End Sub

Private Sub MSFlexGrid1_GotFocus()

  If Text1.Visible Then

    If MSFlexGrid1.Col = 6 Or MSFlexGrid1.Col = 7 Then

        If Text1.text = "" Then
            Text1.text = " "
        End If

    End If

     MSFlexGrid1 = Text1.text
     Text1.Visible = False

  End If

End Sub

Private Sub Text1_KeyPress(KeyAscii As Integer)

  'noise suppression

  If MSFlexGrid1.Col <> 6 And MSFlexGrid1.Col <> 7 Then

    KeyAscii = 0

  End If

  If KeyAscii = vbKeyReturn Then

    KeyAscii = 0

  End If

End Sub