【问题标题】:VB6 SCGrid TextboxVB6 SCGrid 文本框
【发布时间】:2016-02-22 15:25:23
【问题描述】:

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

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

我需要一个对这个文本框的引用。

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

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

【问题讨论】:

    标签: textbox vb6


    【解决方案1】:

    我自己想通了:

    ' 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多