Results 1 to 19 of 19

Thread: Cool Textbox Class

Threaded View

  1. #18
    Fanatic Member Kaverin's Avatar
    Join Date
    Oct 2000
    Posts
    930
    Here's some other stuff you may want to add to the class. I see people asking about getting/changing lines in text files/boxes, and about the row/column numbers too. These props will do those for you. There's an API method of the property get version of Lines which I'd say is faster because it doesn't use the split method, but I don't know of a way to implement the API in a property let version. I had to do that with Split().
    VB Code:
    1. Private Const EM_GETLINE As Long = &HC4
    2. Private Const EM_GETLINECOUNT As Long = &HBA
    3. Private Const EM_GETSEL As Long = &HB0
    4. Private Const EM_LINEFROMCHAR As Long = &HC9
    5. Private Const EM_LINEINDEX As Long = &HBB
    6. Private Const EM_LINELENGTH As Long = &HC1
    7.  
    8. 'here's the API way if anyone wants to know how it was done
    9. Public Property Get LinesAPI(ByVal lngLineNumber As Long) As String
    10.    Dim lngLineCount As Long
    11.    Dim lngFirstChar As Long
    12.    Dim lngLineLength As Long
    13.    Dim hWnd As Long
    14.    Dim abyteBuffer() As Byte
    15.    LinesAPI = ""
    16.    If Not blnClassBound Then Exit Property
    17.    hWnd = txtSource.hWnd
    18.    lngLineCount = SendMessage(hWnd, EM_GETLINECOUNT, 0, 0)
    19.    If (lngLineNumber < 0) Or (lngLineNumber > (lngLineCount - 1)) Then Exit Property
    20.    lngFirstChar = SendMessage(hWnd, EM_LINEINDEX, lngLineNumber, 0)
    21.    lngLineLength = SendMessage(hWnd, EM_LINELENGTH, lngFirstChar, 0)
    22.    If lngLineLength = 0 Then Exit Property
    23.    ReDim abyteBuffer(0 To (lngLineLength - 1))
    24.    abyteBuffer(0) = lngLineLength And &HFF
    25.    If lngLineLength > 255 Then abyteBuffer(1) = lngLineLength And &H100
    26.    SendMessage hWnd, EM_GETLINE, lngLineNumber, VarPtr(abyteBuffer(0))
    27.    LinesAPI = StrConv(abyteBuffer, vbUnicode)
    28. End Property
    29.  
    30. 'this is the Split() way, which takes less work, but may be slower for longer text
    31. Public Property Get Lines(ByVal lngLineNumber As Long) As String
    32.    Dim astrLines As Variant 'may be astrLines() As String for VB6 users
    33.    Lines = ""
    34.    If Not blnClassBound Then Exit Property
    35.    If txtSource.Text = "" Then Exit Property
    36.    astrLines = Split(txtSource.Text, vbCrLf)
    37.    If (lngLineNumber < 0) Or (lngLineNumber > UBound(astrLines)) Then Exit Property
    38.    Lines = astrLines(lngLineNumber)
    39. End Property
    40.  
    41. Public Property Let Lines(ByVal lngLineNumber As Long, ByVal strNewLine As String)
    42.    Dim astrLines As Variant 'may be astrLines() As String for VB6 users
    43.    If Not blnClassBound Then Exit Property
    44.    If txtSource.Text = "" Then
    45.       txtSource.Text = strNewLine
    46.       Exit Property
    47.    End If
    48.    astrLines = Split(txtSource.Text, vbCrLf)
    49.    If (lngLineNumber < 0) Or (lngLineNumber > UBound(astrLines)) Then Exit Property
    50.    astrLines(lngLineNumber) = strNewLine
    51.    txtSource.Text = Join(astrLines, vbCrLf)
    52. End Property
    53.  
    54. 'the next two are mainly for multiline textboxes, but work on single line ones too
    55. 'the row will always be 1 for a single line textbox
    56. Public Property Get Column() As Long
    57.    Dim lngReturn As Long
    58.    Dim lngCharIndex As Long
    59.    Dim lngRow As Long
    60.    If Not blnClassBound Then Exit Property
    61.    lngReturn = SendMessage(txtSource.hWnd, EM_GETSEL, 0, 0)
    62.    lngCharIndex = (lngReturn / 65536) And &HFFFF
    63.    lngReturn = SendMessage(txtSource.hWnd, EM_LINEINDEX, -1, 0)
    64.    Column = lngCharIndex - lngReturn + 1
    65. End Property
    66.  
    67. Public Property Get Row() As Long
    68.    Dim lngReturn As Long
    69.    Dim lngCharIndex As Long
    70.    If Not blnClassBound Then Exit Property
    71.    lngReturn = SendMessage(txtSource.hWnd, EM_GETSEL, 0, 0)
    72.    lngCharIndex = (lngReturn / 65536) And &HFFFF
    73.    Row = SendMessage(txtSource.hWnd, EM_LINEFROMCHAR, lngCharIndex, 0) + 1
    74. End Property
    Last edited by Kaverin; Sep 14th, 2001 at 05:38 PM.
    I'm baaaack...
    VB5 Professional Edition, VC++ 6
    Using a 1 gHz Thunderbird, 256 mb RAM, 40 gb HD system with Win98se

    I feel special because I finally figured out how to loop midis: Post link
    I'm a fanatic too

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width