iT邦幫忙

0

VB6 flexgrid edit by text 移動問題

小的是剛撰寫VB6的小小菜鳥
近期在練習VB6功能時
想利用text 去修改flexgrid 顯示的文字
參考幾隻程式後撰寫如下
在的問題是 跑出來只有第一行可以修改
懇請各位大大 幫忙我找出錯誤 並指導我如何修改 3Q

Dim arrName(5) As String
Dim arrScore(100, 100) As Integer
Dim arrJoint(100, 100) As String
Dim n As Integer
Dim eCol As Integer
Dim eRow As Integer

Private Sub button_Click()
 
  If txtName = "" Then
    MsgBox "姓名不可空白!", 0 + 64, "訊息"
    Exit Sub
  End If
  
If button.Caption = "送出" Then
 
    n = n + 1
    With fg
      .Cols = 6
      .Rows = 6
      .TextMatrix(0, 0) = "    "
      .TextMatrix(0, 1) = " 國 文 "
      .TextMatrix(0, 2) = " 數 學 "
      .TextMatrix(0, 3) = " 英 文 "
      .TextMatrix(0, 4) = " 平 均 "
      .TextMatrix(0, 5) = " 成績排名 "
      .ColWidth(0) = 1000
      .ColWidth(1) = 1000
      .ColWidth(2) = 1000
      .ColWidth(3) = 1000
      .ColWidth(4) = 1000
      .ColWidth(5) = 1000
      .TextMatrix(n, 0) = txtName.Text '姓名
      .TextMatrix(n, 1) = txtCh.Text   '中文成績
      .TextMatrix(n, 2) = txtEng.Text  '英文成績
      .TextMatrix(n, 3) = txtMath.Text '數學成績
      .TextMatrix(n, 4) = Round((CInt(fg.TextMatrix(n, 1)) + CInt(fg.TextMatrix(n, 2)) + CInt(fg.TextMatrix(n, 3))) / 3) '計算平均
    End With
End If
  txtName.Text = ""
  txtCh.Text = ""
  txtEng.Text = ""
  txtMath.Text = ""
End Sub
Private Sub fg_Click()
  eCol = fg.Col
  eRow = fg.Row
EditGrid
End Sub
Sub EditGrid()
With fg
    txtEdit.Move .Left + .CellLeft, .Top + .CellTop, .CellWidth, .CellHeight
    txtEdit.Text = .Text
    txtEdit.SelStart = Len(.Text)
    txtEdit.Visible = True
    txtEdit.SetFocus
End With
End Sub
Private Sub fg_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyF2 And fg.Col = 0 Then
    eCol = fg.Col
    eRow = fg.Row
    EditGrid
End If
End Sub
Private Sub fg_KeyPress(KeyAscii As Integer)
 If InStr(0, "0123456789") >= 0 Then
    If fg.Col = 0 Or fg.Col = 2 Or fg.Col = 3 Then
        eCol = fg.Col
        eRow = fg.Row
        EditGrid
        txtEdit.Text = Chr(KeyAscii)
        txtEdit.SelStart = Len(txtEdit.Text)
    End If
 End If
End Sub
Private Sub Form_Load()
   n = 0
   fg.Clear
End Sub
Private Sub txtEdit_LostFocus()
  If Len(Trim(txtEdit.Text)) > 0 Then
    If fg.TextMatrix(eRow, eCol) = txtEdit.Text Then
        fg.SetFocus
        txtEdit.Visible = False
        Exit Sub
    End If
    fg.TextMatrix(eRow, eCol) = txtEdit.Text
    fg.SetFocus
    txtEdit.Visible = False
Else
    fg.SetFocus
    txtEdit.Visible = False
End If
End Sub
抱歉 排版後來亂掉了ˊˋ
小財神 iT邦好手 1 級 ‧ 2016-10-21 06:41:53 檢舉
我幫您調整了,麻煩您程式需要套用程式碼區塊
也就是用‵‵‵上下包覆

1 個回答

1
海綿寶寶
iT邦超人 1 級 ‧ 2016-10-22 18:53:16

送你四個字:砍掉重練
資料來源

Option Explicit
Private lRow As Long	'Selected Row
Private lCol As Long	'Selected Column

Private Sub Form_Load()
Dim i As Integer
On Error GoTo err_h
	With MSFGTable
' Generate 4 columns and 1 fixed row used as Names of the cells 
		.FixedRows = 1
		.FixedCols = 0
		.Rows = 1
		.Cols = 4
		
		.TextMatrix(0, 0) = "Value1"   'First row is for cells names
		.TextMatrix(0, 1) = "Value2"
		.TextMatrix(0, 2) = "Value3"
		.TextMatrix(0, 3) = "Value4"

' Add a data in the cells that will be edited later 
	For i = 1 To 10
		.Rows = i + 1
		.TextMatrix(i, 0) = i
		.TextMatrix(i, 1) = i + 1
		.TextMatrix(i, 2) = "val" & i
		.TextMatrix(i, 3) = "val" & i & "= " & i + 1
	Next i
	End With
Exit Sub
err_h:
	MsgBox Err.Description, vbCritical
End Sub

Private Sub MSFGTable_DblClick()
	Dim tL As Long, tH As Long, tW As Long, tT As Long 'MSFlexGrid cell boundary
On Error GoTo error_h
	With MSFGTable
	lRow = .RowSel
	lCol = .ColSel
	If lCol = 1 Or lCol = 3 Then 'Check if the clolumns we want to edit are 2 and 4
		tT = .CellTop
		tL = .CellLeft
		tH = .CellHeight
		tW = .CellWidth
		'Move the text box on the cell we dbl click on
		txtEdit.Move tL + .Left, tT + .Top, tW + 2 * Screen.TwipsPerPixelX, tH
		txtEdit.Visible = True
		txtEdit.ZOrder 0
		txtEdit.SetFocus
		
	End If
	
	End With
Exit Sub

error_h:
	MsgBox Err.Description, vbCritical
End Sub

Private Sub txtEdit_KeyPress(KeyAscii As Integer)
	If KeyAscii = Str(13) Then	'Enter
		Select Case lCol
			Case 1
				'Making Cell2 to accept numbers only
				If IsNumeric(txtEdit.Text) Then
					Change_FGCell_Value lRow, lCol
				Else
					' An error and exite the function awaiting right value
					MsgBox "Numbers only!", vbCritical
					Exit Sub
				End If
			Case 3
				Change_FGCell_Value lRow, lCol
			Case Else
				' An error becose is edited wrong Column
				MsgBox "Error invalid Cell", vbCritical
		End Select
	End If
End Sub

Private Sub Change_FGCell_Value()
	'Change selected value of the cell in MSFlexGrid
	MSFGTable.TextMatrix(lRow, lCol) = txtEdit.Text
	txtEdit.Text = ""
	txtEdit.Visible = False
End Sub

Private Sub txtEdit_LostFocus()
	Private Sub txtEdit_LostFocus()
	Select Case lCol
		Case 1
			'Making cell2 to accept numbers only
			If IsNumeric(txtEdit.Text) Then
				Change_FGCell_Value lRow, lCol
			Else
				txtEdit.Visible = False
				' put txtEdit.Text="" if you want your textbox text is deleted
			End If
			
		Case 3
			Change_FGCell_Value lRow, lCol
		Case Else
			'An error for editing wrong column
			MsgBox "Error invalid Cell", vbCritical
	End Select
End Sub

我要發表回答

立即登入回答