Pages

9/13/2009

Membuat Grid Jadi editable dlm VB6

Ini Source Code untuk buat value MSFlexGrid di VB6 jadi bisa di edit..

Tambahkan 1 textbox,MSFlexGrid

Option Explicit

Dim bDoNotEdit As Boolean
Dim bOnFixedPart As Boolean
Private Sub Form_Click()
Call pSetCellValue
End Sub

Private Sub gridTest_Click()

If bOnFixedPart Then Exit Sub
Call pEditGrid(32)
End Sub

Private Sub pEditGrid(KeyAscii As Integer)
With txtEdit
Select Case KeyAscii
Case 0 To 32
.Text = gridTest
.SelStart = 0
.SelLength = 1000

Case 8, 46, 48 To 57
.Text = Chr(KeyAscii)
.SelStart = 1
Case Else
.Text = "0"
End Select
End With

With gridTest
If .CellWidth < 0 Then Exit Sub txtEdit.Move .Left + .CellLeft, .Top + .CellTop, .CellWidth, .CellHeight ' ' NOTE: ' Ini tergantung dari style grid yang xan set. ' Kalau xan menggunakan Raised gridline,Pakai code di bawah: ' 'txtEdit.Move .Left + .CellLeft, .Top + .CellTop, .CellWidth - 8, .CellHeight - 8 End With txtEdit.Visible = True txtEdit.SetFocus End Sub Private Sub gridTest_GotFocus() If bDoNotEdit Then Exit Sub Call pSetCellValue End Sub Private Sub gridTest_KeyPress(KeyAscii As Integer) Call pEditGrid(KeyAscii) End Sub Private Sub gridTest_LeaveCell() If bDoNotEdit Then Exit Sub Call gridTest_GotFocus End Sub Private Sub gridTest_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single) Dim l As Long Dim lWidth As Long With gridTest For l = 0 To .Cols - 1 If .ColIsVisible(l) Then lWidth = lWidth + .ColWidth(l) End If Next bOnFixedPart = (x < .ColWidth(0)) Or _ (x > lWidth) Or _
(y < .RowHeight(0)) Or _ (y > .Rows * .RowHeightMin)
End With
End Sub
Private Sub gridTest_Scroll()
Call gridTest_GotFocus
End Sub
Private Sub Form_Load()
Dim i As Long

With txtEdit.Font
.Name = gridTest.Font.Name
.Size = gridTest.Font.Size
.Weight = gridTest.Font.Weight
End With
txtEdit.BackColor = vb3DLight

With gridTest
.RowHeightMin = txtEdit.Height

.ColWidth(0) = .ColWidth(0) / 2
.ColAlignment(0) = 1 ' Center center.

For i = .FixedRows To .Rows - 1
.TextArray(fLabel(i, 0)) = i
Next

For i = .FixedCols To .Cols - 1
.TextArray(fLabel(0, i)) = i
Next

For i = .FixedCols To .Cols - 1
.ColAlignment(i) = flexAlignRightCenter
Next
End With

txtEdit = ""
bDoNotEdit = False
End Sub

Private Function fLabel(lRow As Long, lCol As Long) As Long
fLabel = lCol + gridTest.Cols * lRow
End Function

Private Sub txtEdit_KeyDown(KeyCode As Integer, Shift As Integer)
With gridTest
Select Case KeyCode
Case 13 'ENTER
.SetFocus
Case 27 'ESC
txtEdit.Visible = False
.SetFocus
Case 38 'Up arrow
.SetFocus
DoEvents
If .Row > .FixedRows Then
bDoNotEdit = True
.Row = .Row - 1
bDoNotEdit = False
End If
Case 40 'Down arrow
.SetFocus
DoEvents
If .Row < .Rows - 1 Then bDoNotEdit = True .Row = .Row + 1 bDoNotEdit = False End If End Select End With End Sub Private Sub txtEdit_KeyPress(KeyAscii As Integer) Select Case KeyAscii Case Asc(vbCr) KeyAscii = 0 Case 8, 46 Case 48 To 57 Case Else KeyAscii = 0 End Select End Sub Private Sub pSetCellValue() If txtEdit.Visible Then gridTest.Text = txtEdit.Text txtEdit.Visible = False End If End Sub

0 comments:

Post a Comment