Sub StartEdit()
With msfgUserData
If .Row <> 0 And .Col <> 0 And .Col <> 3 Then
txtEdit.Text = .Text
txtEdit.SelStart = 0
txtEdit.SelLength = Len(txtEdit.Text)
txtEdit.SetFocus
ElseIf .Row <> 0 And .Col = 3 Then
cmbEdit.Left = .Left + .CellLeft
cmbEdit.Top = .Top + .CellTop
cmbEdit.Width = .ColWidth(.Col)
cmbEdit.Visible = True
cmbEdit.Text = .Text
cmbEdit.SetFocus
End If
End With
End SubSub EndEdit()
If cmbEdit.Visible Then cmbEdit.Visible = False
msfgUserData.SetFocus '★★★
End SubPrivate Sub msfgUserData_KeyDown(KeyCode As Integer, Shift As Integer)
'当在Cell中按回车键时,实现编辑
If KeyCode = vbKeyReturn Then StartEdit
End Sub我想在FlexGrid中嵌入ComboBox实现编辑,在这里查了一种方法,人家的例程运行通过,我的代码也是模仿这种思路,然而在我这里,当在应该出现ComboBox的Cell中按回车时,表面上并无任何反应,请问这是为什么啊?另外执行到注有★★★的那句时就会弹出“实时错误5, 无效的过程调用或参数”,请问这又是为什么呢?关键代码都在上面了,请大侠指教!
With msfgUserData
If .Row <> 0 And .Col <> 0 And .Col <> 3 Then
txtEdit.Text = .Text
txtEdit.SelStart = 0
txtEdit.SelLength = Len(txtEdit.Text)
txtEdit.SetFocus
ElseIf .Row <> 0 And .Col = 3 Then
cmbEdit.Left = .Left + .CellLeft
cmbEdit.Top = .Top + .CellTop
cmbEdit.Width = .ColWidth(.Col)
cmbEdit.Visible = True
cmbEdit.Text = .Text
cmbEdit.SetFocus
End If
End With
End SubSub EndEdit()
If cmbEdit.Visible Then cmbEdit.Visible = False
msfgUserData.SetFocus '★★★
End SubPrivate Sub msfgUserData_KeyDown(KeyCode As Integer, Shift As Integer)
'当在Cell中按回车键时,实现编辑
If KeyCode = vbKeyReturn Then StartEdit
End Sub我想在FlexGrid中嵌入ComboBox实现编辑,在这里查了一种方法,人家的例程运行通过,我的代码也是模仿这种思路,然而在我这里,当在应该出现ComboBox的Cell中按回车时,表面上并无任何反应,请问这是为什么啊?另外执行到注有★★★的那句时就会弹出“实时错误5, 无效的过程调用或参数”,请问这又是为什么呢?关键代码都在上面了,请大侠指教!
======================================================================Option ExplicitPrivate Sub EndEdit()
If Combo1.Visible Then
MSFlexGrid1.Text = Combo1.Text
Combo1.Visible = False
ElseIf Text1.Visible Then
MSFlexGrid1.Text = Text1.Text
Text1.Visible = False
End If
End SubPrivate Sub StartEdit()
If MSFlexGrid1.Col = 2 Then
Combo1.Clear
Combo1.AddItem "Paranoia City"
Combo1.AddItem "Langonsville"
Combo1.AddItem "Codertown"
Combo1.AddItem "Bugvanna"
Combo1.AddItem "Abend"
On Error Resume Next
Combo1.Text = MSFlexGrid1.Text
On Error GoTo 0 GridEditCombo
ElseIf MSFlexGrid1.Col = 3 Then
Combo1.Clear
Combo1.AddItem "CA"
Combo1.AddItem "VT"
Combo1.AddItem "MN"
Combo1.AddItem "WY"
Combo1.AddItem "TX"
On Error Resume Next
Combo1.Text = MSFlexGrid1.Text
On Error GoTo 0 GridEditCombo
Else
GridEditText Asc(" ")
End If
End Sub' Make the change.
Private Sub Combo1_Click()
MSFlexGrid1.SetFocus
End Sub
Private Sub Form1_Load()
Dim r As Integer
Dim c As Integer
Dim max_len As Single
Dim new_len As Single ' Use no border.
Text1.BorderStyle = vbBSNone ' Make the grid's cell height match
' that of the ComboBox.
MSFlexGrid1.RowHeightMin = Combo1.Height ' Match the grid's font.
Text1.FontName = MSFlexGrid1.FontName
Text1.FontSize = MSFlexGrid1.FontSize
Text1.Visible = False
Combo1.FontName = MSFlexGrid1.FontName
Combo1.FontSize = MSFlexGrid1.FontSize
Combo1.Visible = False ' Create some data.
MSFlexGrid1.FixedCols = 0
MSFlexGrid1.Cols = 5
MSFlexGrid1.FixedRows = 1
MSFlexGrid1.Rows = 6 MSFlexGrid1.TextMatrix(0, 0) = "Name"
MSFlexGrid1.TextMatrix(0, 1) = "Street"
MSFlexGrid1.TextMatrix(0, 2) = "City"
MSFlexGrid1.TextMatrix(0, 3) = "State"
MSFlexGrid1.TextMatrix(0, 4) = "Zip" MSFlexGrid1.TextMatrix(1, 0) = "Andy Avaricious"
MSFlexGrid1.TextMatrix(1, 1) = "827 Problem Blvd"
MSFlexGrid1.TextMatrix(1, 2) = "Paranoia City"
MSFlexGrid1.TextMatrix(1, 3) = "CA"
MSFlexGrid1.TextMatrix(1, 4) = "98765" MSFlexGrid1.TextMatrix(2, 0) = "Betty Boisterous"
MSFlexGrid1.TextMatrix(2, 1) = "4352 Main St Apt 254"
MSFlexGrid1.TextMatrix(2, 2) = "Langonsville"
MSFlexGrid1.TextMatrix(2, 3) = "VT"
MSFlexGrid1.TextMatrix(2, 4) = "01234" MSFlexGrid1.TextMatrix(3, 0) = "Charles Cheerful"
MSFlexGrid1.TextMatrix(3, 1) = "89 North A Ln"
MSFlexGrid1.TextMatrix(3, 2) = "Codertown"
MSFlexGrid1.TextMatrix(3, 3) = "MN"
MSFlexGrid1.TextMatrix(3, 4) = "54583" MSFlexGrid1.TextMatrix(4, 0) = "Denise Delightful"
MSFlexGrid1.TextMatrix(4, 1) = "89 75th"
MSFlexGrid1.TextMatrix(4, 2) = "Bugvanna"
MSFlexGrid1.TextMatrix(4, 3) = "WY"
MSFlexGrid1.TextMatrix(4, 4) = "82738" MSFlexGrid1.TextMatrix(5, 0) = "Ed Enlightened"
MSFlexGrid1.TextMatrix(5, 1) = "2765 N South St E"
MSFlexGrid1.TextMatrix(5, 2) = "Abend"
MSFlexGrid1.TextMatrix(5, 3) = "TX"
MSFlexGrid1.TextMatrix(5, 4) = "67583" ' Size the columns.
Font.Name = MSFlexGrid1.Font.Name
Font.Size = MSFlexGrid1.Font.Size
For c = 0 To MSFlexGrid1.Cols - 1
max_len = 0
For r = 0 To MSFlexGrid1.Rows - 1
new_len = TextWidth(MSFlexGrid1.TextMatrix(r, c))
If max_len < new_len Then max_len = new_len
Next r
MSFlexGrid1.ColWidth(c) = max_len + 240
MSFlexGrid1.ColAlignment(c) = flexAlignLeftCenter
Next c MSFlexGrid1.AllowUserResizing = flexResizeBoth
End Sub
Private Sub GridEditText(ByVal KeyAscii As Integer)
' Position the TextBox over the cell.
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 Select Case KeyAscii
Case 0 To Asc(" ")
Text1.Text = MSFlexGrid1.Text
Text1.SelStart = Len(Text1.Text)
Case Else
Text1.Text = Chr$(KeyAscii)
Text1.SelStart = 1
End Select
End SubPrivate Sub GridEditCombo()
' Position the ComboBox over the cell.
Combo1.Left = MSFlexGrid1.CellLeft + MSFlexGrid1.Left
Combo1.Top = MSFlexGrid1.CellTop + MSFlexGrid1.Top
Combo1.Width = MSFlexGrid1.CellWidth
Combo1.Visible = True
Combo1.SetFocus
End Sub
Private Sub Form_Resize()
MSFlexGrid1.Move 0, 0, ScaleWidth, ScaleHeight
End Sub
Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)
Select Case KeyCode
Case vbKeyEscape
' Leave the text unchanged.
Text1.Visible = False
MSFlexGrid1.SetFocus Case vbKeyReturn
' Finish editing.
MSFlexGrid1.SetFocus Case vbKeyDown
' Move down 1 row.
MSFlexGrid1.SetFocus
DoEvents
If MSFlexGrid1.Row < MSFlexGrid1.Rows - 1 Then
MSFlexGrid1.Row = MSFlexGrid1.Row + 1
End If Case vbKeyUp
' Move up 1 row.
MSFlexGrid1.SetFocus
DoEvents
If MSFlexGrid1.Row > MSFlexGrid1.FixedRows Then
MSFlexGrid1.Row = MSFlexGrid1.Row - 1
End If End Select
End Sub
' Do not beep on Return or Escape.
Private Sub Text1_KeyPress(KeyAscii As Integer)
If (KeyAscii = vbKeyReturn) Or _
(KeyAscii = vbKeyEscape) _
Then KeyAscii = 0
End Sub
Private Sub MSFlexGrid1_DblClick()
StartEdit
End SubPrivate Sub MSFlexGrid1_KeyPress(KeyAscii As Integer)
If MSFlexGrid1.Col <> 2 And _
MSFlexGrid1.Col <> 3 _
Then
GridEditText KeyAscii
End If
End SubPrivate Sub MSFlexGrid1_LeaveCell()
EndEdit
End Sub
Private Sub MSFlexGrid1_GotFocus()
EndEdit
End Sub
我以前是这么做的,试试看
你说的“开始的事情”是指定位ComboBox吗?
另外我已经试过把特定代码放到双击事件当中,可是仍然不行。