Could someone please test this code or tell me what's wrong with it? It uses a single form with a richtextbox named rtfScripts. I am trying to get it to resize according to the number of lines in it... but I can't even get the lineindex to return anything but -1.

VB Code:
  1. Private Const LF_FACESIZE = 32
  2. Private Const LF_FULLFACESIZE = 64
  3.  
  4. Private Const WM_GETFONT = &H31
  5. Private Const WM_USER = &H400
  6. Private Const EM_GETLINECOUNT = &HBA
  7. Private Const EM_GETLINE = &HC4
  8. Private Const EM_LINELENGTH = &HC1
  9. Private Const EM_LINEINDEX = &HBB
  10. Private Const EM_GETRECT = &HB2
  11. Private Const EM_GETCHARFORMAT = (WM_USER + 58)
  12.  
  13. Private Const SCF_SELECTION = &H1
  14.  
  15. Private Type CHARFORMAT
  16.     cbSize As Long
  17.     dwMask As Long
  18.     dwEffects As Long
  19.     yHeight As Long
  20.     yOffset As Long
  21.     crTextColor As Long
  22.     bCharSet As Byte
  23.     bPitchAndFamily As Byte
  24.     szFaceName(LF_FACESIZE) As String
  25. End Type
  26.  
  27. Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, wParam As Any, LParam As Any) As Long
  28.  
  29. Private Sub Form_Load()
  30.     Dim nLine As Long
  31.     Dim nIndex As Long
  32.     Dim nCurrentPos As Long
  33.     Dim nTextsize As Long
  34.     Dim cf As CHARFORMAT
  35.     Dim nLineCount As Long
  36.    
  37.     rtfScripts.Text = "This is a test... This is a test... This is a test... This is a test... This is a test... This is a test... This is a test... This is a test..."
  38.    
  39.     nLineCount = SendMessage(rtfScripts.hwnd, EM_GETLINECOUNT, ByVal 0&, ByVal 0&)
  40.    
  41.     For nLine = 0 To (nLineCount - 1) Step 1
  42.        
  43.         rtfScripts.SelStart = nCurrentPos
  44.         nIndex = SendMessage(rtfScripts.hwnd, EM_LINEINDEX, nLine, ByVal 0&)
  45.         nCurrentPos = nCurrentPos + SendMessage(rtfScripts.hwnd, EM_LINELENGTH, nIndex, ByVal 0&)
  46.         rtfScripts.SelLength = nCurrentPos
  47.        
  48.         SendMessage rtfScripts.hwnd, EM_GETCHARFORMAT, SCF_SELECTION, cf
  49.        
  50.         nTextsize = nTextsize + cf.yHeight
  51.        
  52.     Next nLine
  53.    
  54.     rtfScripts.Height = nTextsize + 10
  55. End Sub