VB6 SCGrid 文本框

VB6 SCGrid Textbox

在 VB6 表单中,我使用的是 SCGrid 对象,单元格可通过文本框进行编辑。

我想只要用户单击一个单元格,网格控件就会创建一个 TextBox 对象。

我需要对此文本框的引用。

特别是,当用户按下 [Left] 或 [Right] 键时,我需要光标在文本框中的当前位置。然后我可以简单地调用 TextBox.SelStart.

有人知道编辑单元格时如何使用控件吗?

我自己想出来了:

' Declaration needed for getting the current position of the cursor
Private Type Point
  x As Long
  y As Long
End Type

Private Declare Function GetCaretPos Lib "user32" (ByRef lpPoint As Point) As Long

' This function returns the position of the cursor within the cell currently being edited as a TextBox
' The value is expressed as the number of characters preceeding the position
Private Function GetCaretPosition() As Integer

  ' Variables
  Dim position As Point
  Dim result   As Long
  Dim contents As String

  ' Get position
  result = GetCaretPos(position)

  ' Convert it into number of characters
  ' I figured out the factor 15 by trial and error
  ' Don't know where it comes from but it seems independent of the font size
  contents = myGrid.Text(row_gvar, column_gvar)
  result = 0
  While ((result < Len(contents)) And _
         (position.x > (grid_gvar.Parent.TextWidth(Left(contents, result)) / 15)))
    result = result + 1
  Wend

  ' Done
  GetCaretPosition = result

End Function