[此贴子已经被作者于2007-6-22 15:33:38编辑过]
[此贴子已经被作者于2007-6-22 15:33:38编辑过]
已经实现功能的代码如下:
Private Sub Form_Load()
Dim I As Integer, lRow As Long
Dim Width, Text
Width = Array("300", "1800", "500", "1000", "800", "1200", "1000")
Text = Array("行", " 商 品 名 称", "单位", " 数 量", " 单 价", " 金 额", " 年 份")
Text1.Visible = False
Combo1.Visible = False
Form1.BackColor = RGB(231, 235, 247)
With MSFlexGrid1 '初始化表格
.BackColorFixed = RGB(231, 235, 247)
.GridColor = RGB(90, 158, 214)
.BackColorBkg = RGB(90, 158, 214)
.Cols = 7
.Rows = 6
.RowHeight(0) = 320
For I = 0 To 6
MSFlexGrid1.ColWidth(I) = Width(I)
MSFlexGrid1.TextMatrix(0, I) = Text(I)
Next I
lRow = 1
For I = 1 To 5
.TextMatrix(lRow, 0) = I
.TextMatrix(lRow, 4) = "单价" & I
lRow = 1 + lRow
.RowHeight(I) = 320
Next I
End With
For I = 2005 To 2010
Combo1.AddItem I
Next I
End Sub
Private Sub MSFlexGrid1_Click()
With MSFlexGrid1
If .MouseCol = 0 Or .MouseRow = 0 Then Exit Sub
If .MouseCol <= 6 Then
Text1.Left = .Left + .CellLeft
Text1.Top = .Top + .CellTop
Text1.Height = .CellHeight
Text1.Width = .CellWidth
Text1.Text = .Clip
Text1.Visible = True
Text1.SetFocus
Text1.SelStart = 0
Text1.SelLength = Len(Text1.Text)
End If
End With
End Sub
Private Sub MSFlexGrid1_KeyUp(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyDelete Then
MSFlexGrid1.Clip = ""
End If
End Sub
Private Sub MSFlexGrid1_LeaveCell()
MSFlexGrid1.CellBackColor = DefColor
End Sub
Private Sub MSFlexGrid1_Scroll()
With MSFlexGrid1
End With
End Sub
Private Sub MSFlexGrid1_RowColChange()
With MSFlexGrid1
Text1.Visible = False
End With
End Sub
Private Sub Text1_Change()
MSFlexGrid1.Clip = Text1.Text
End Sub
Private Sub Text1_KeyUp(KeyCode As Integer, Shift As Integer)
Dim C As Long
Horizenw = 7 '默认到第7列时返回,可以取消
Select Case KeyCode
Case vbKeyRight, vbKeyReturn '向右、回车
If MSFlexGrid1.Col < Horizenw - 1 Then
MSFlexGrid1.Col = MSFlexGrid1.Col + 1
MSFlexGrid1_Click
ElseIf MSFlexGrid1.Row < MSFlexGrid1.Rows - 1 Then
MSFlexGrid1.Col = 1
MSFlexGrid1.Row = MSFlexGrid1.Row + 1
MSFlexGrid1_Click
Else
With MSFlexGrid1 '追加一行
.Rows = .Rows + 1
C = .Rows
.Col = 1
.Row = .Row + 1
.TextMatrix(C - 1, 0) = C - 1 '行头标志
.RowHeight(C - 1) = 320 '保持行高
.TextMatrix(.Rows - 1, 4) = "单价" & .Rows - 1
End With
MSFlexGrid1_Click
End If
Case vbKeyLeft '向左
If MSFlexGrid1.Col > 1 Then
MSFlexGrid1.Col = MSFlexGrid1.Col - 1
MSFlexGrid1_Click
ElseIf MSFlexGrid1.Row > 1 Then
MSFlexGrid1.Col = MSFlexGrid1.Cols - 1
MSFlexGrid1.Row = MSFlexGrid1.Row - 1
MSFlexGrid1_Click
End If
Case vbKeyUp '向上
If MSFlexGrid1.Row > 1 Then
MSFlexGrid1.Row = MSFlexGrid1.Row - 1
MSFlexGrid1_Click
End If
Case vbKeyDown '向下
If MSFlexGrid1.Row < MSFlexGrid1.Rows - 1 Then
MSFlexGrid1.Row = MSFlexGrid1.Row + 1
MSFlexGrid1_Click
End If
Case vbKeyF4 '删除
If MSFlexGrid1.Row > 1 Then
MSFlexGrid1.RemoveItem MSFlexGrid1.Row
End If
End Select
End Sub
[此贴子已经被作者于2007-6-22 21:53:33编辑过]