Results 1 to 7 of 7

Thread: [RESOLVED]RichTextBox to Listbox? or Inet HTML Source to .txt?

  1. #1

    Thread Starter
    Member
    Join Date
    Jan 2005
    Location
    Canada, Ontario
    Posts
    44

    [RESOLVED]RichTextBox to Listbox? or Inet HTML Source to .txt?

    How could I take data from a RichTextBox line by line and add it to a listbox?

    Any help is greatly apreciated...
    Last edited by Computer_Wiz; Jan 24th, 2005 at 03:15 PM.
    ' Comments go here...
    ~ Computer_Wiz ~

  2. #2
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: RichTextBox to Listbox?

    Here is what I have so far. In my test it isn't stripping the last line correctly though.

    VB Code:
    1. Option Explicit
    2. Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As String) As Long
    3. Private Const EM_GETLINE = &HC4
    4. Private Const EM_LINELENGTH = &HC1
    5. Private Const EM_GETLINECOUNT = &HBA
    6.  
    7. Private Sub Command1_Click()
    8.  
    9.     Dim strBuffer As String
    10.     Dim lngLength As Long
    11.     Dim intCurrentLine As Integer
    12.     Dim lngLines As Long
    13.     Dim lngIndex As Long
    14.    
    15.     RichTextBox1.SelStart = 1
    16.     lngLines = SendMessage(RichTextBox1.hwnd, EM_GETLINECOUNT, 0, 0)
    17.     For lngIndex = 1 To lngLines
    18.         intCurrentLine = RichTextBox1.GetLineFromChar(RichTextBox1.SelStart)
    19.         'get line length
    20.         lngLength = SendMessage(RichTextBox1.hwnd, EM_LINELENGTH, intCurrentLine, 0)
    21.         'resize buffer
    22.         strBuffer = Space(lngLength)
    23.         'get line text
    24.         Call SendMessage(RichTextBox1.hwnd, EM_GETLINE, intCurrentLine, ByVal strBuffer)
    25.        
    26.         List1.AddItem strBuffer
    27.         RichTextBox1.SelStart = RichTextBox1.SelStart + lngLength
    28.     Next
    29. End Sub
    30.  
    31. Private Sub Form_Load()
    32. RichTextBox1.Text = "This is a test of the quick brown fox running off with the dog"
    33. End Sub

  3. #3

    Thread Starter
    Member
    Join Date
    Jan 2005
    Location
    Canada, Ontario
    Posts
    44

    Re: RichTextBox to Listbox?

    Ok well that doesnt really work for what I'm attempting..

    Cause I'm trying to receive the source code from a html document using the INet and I get display it in a textbox because of bold lines which the source returns.

    Otherwise I could easily save the textbox to a .txt and the input line by line and kill the file.

    The listbox is crucial to what I have to do, if the source can be added to a .txt I can easily import the source code, just I can't get the source code without the bold lines, without using a RichTextBox
    ' Comments go here...
    ~ Computer_Wiz ~

  4. #4
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: RichTextBox to Listbox? or Inet HTML Source to .txt?

    So, you want the source code for any input web page. Is this what you are after?

  5. #5

    Thread Starter
    Member
    Join Date
    Jan 2005
    Location
    Canada, Ontario
    Posts
    44

    Re: RichTextBox to Listbox? or Inet HTML Source to .txt?

    Pretty much, but it returns vbcr and vblf. And when I remove these, the source stays to one line. In which will counter react when I attempt to add the source to a listbox.

    Edit: Well I've gotten closer to what I need, now I just need to remove 1 char... not sure which one it is as of yet.
    Last edited by Computer_Wiz; Jan 24th, 2005 at 02:30 PM.
    ' Comments go here...
    ~ Computer_Wiz ~

  6. #6
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: RichTextBox to Listbox? or Inet HTML Source to .txt?

    Quote Originally Posted by Computer_Wiz
    Pretty much, but it returns vbcr and vblf. And when I remove these, the source stays to one line. In which will counter react when I attempt to add the source to a listbox.
    Well, this should get you whatever source code is available on the page, and dump it to a richtextbox. From there, I guess you would dump it to a text file, load the listbox, and kill the text file. It should all be pretty much transparent to the user, just a little more coding on your part.
    VB Code:
    1. Option Explicit
    2.  
    3. Private Declare Function InternetOpen Lib "wininet.dll" Alias "InternetOpenA" (ByVal sAgent As String, ByVal lAccessType As Long, ByVal sProxyName As String, ByVal sProxyBypass As String, ByVal lFlags As Long) As Long
    4. Private Declare Function InternetOpenUrl Lib "wininet.dll" Alias "InternetOpenUrlA" (ByVal hInternetSession As Long, ByVal sURL As String, ByVal sHeaders As String, ByVal lHeadersLength As Long, ByVal lFlags As Long, ByVal lContext As Long) As Long
    5. Private Declare Function InternetReadFile Lib "wininet.dll" (ByVal hFile As Long, ByVal sBuffer As String, ByVal lNumBytesToRead As Long, lNumberOfBytesRead As Long) As Integer
    6. Private Declare Function InternetCloseHandle Lib "wininet.dll" (ByVal hInet As Long) As Integer
    7.  
    8. Private Const IF_FROM_CACHE = &H1000000
    9. Private Const IF_MAKE_PERSISTENT = &H2000000
    10. Private Const IF_NO_CACHE_WRITE = &H4000000
    11.        
    12. Private Const BUFFER_LEN = 256
    13.  
    14. Private Function GetUrlSource(sURL As String) As String
    15.     Dim sBuffer As String * BUFFER_LEN
    16.     Dim Result As Integer
    17.     Dim sData As String
    18.     Dim hInternet As Long
    19.     Dim hSession As Long
    20.     Dim lReturn As Long
    21.  
    22.     hSession = InternetOpen("vb wininet", 1, vbNullString, vbNullString, 0)
    23.     If hSession Then hInternet = InternetOpenUrl(hSession, sURL, vbNullString, 0, IF_NO_CACHE_WRITE, 0)
    24.  
    25.     If hInternet Then
    26.         iResult = InternetReadFile(hInternet, sBuffer, BUFFER_LEN, lReturn)
    27.         sData = sBuffer
    28.         Do While lReturn <> 0
    29.             iResult = InternetReadFile(hInternet, sBuffer, BUFFER_LEN, lReturn)
    30.             sData = sData + Mid(sBuffer, 1, lReturn)
    31.         Loop
    32.     End If
    33.    
    34.     iResult = InternetCloseHandle(hInternet)
    35.  
    36.     GetUrlSource = sData
    37. End Function
    38.  
    39. Private Sub Command1_Click()
    40. Dim strURL As String
    41. Dim HTMLSource As String
    42. strURL = "http://www.google.com/"
    43. HTMLSource = Inet1.OpenURL(strURL, icString)
    44. RichTextBox1.Text = HTMLSource
    45. End Sub

  7. #7

    Thread Starter
    Member
    Join Date
    Jan 2005
    Location
    Canada, Ontario
    Posts
    44

    Re: RichTextBox to Listbox? or Inet HTML Source to .txt?

    Well thanks Hack, but you made it more complicated then it really needed to be. I figured it out and for feature reference to any other beginners. Here's my source.

    VB Code:
    1. Dim strRemVBCRLF() As String
    2. Dim strArray As String
    3.  
    4. With Inet1
    5.     strsource = .OpenURL(txtURL.Text, icString) 'Open url according to what user entered
    6.   End With
    7.  
    8.   strsource = Replace(strsource, vbLf, vbCrLf) 'Remove bad chars
    9.   strsource = Replace(strsource, vbCr, vbCrLf) ' ""
    10.    
    11.   strRemVBCRLF() = Split(strsource, vbCrLf & vbLf) 'Split each line
    12.   strArray = UBound(strRemVBCRLF) - LBound(strRemVBCRLF) 'Find Out Array Count
    13.  
    14.     For x = 0 To strArray
    15.         lstWebSource.AddItem strRemVBCRLF(x) 'Add each line
    16.     Next
    17. End Sub

    The less complicated coding, the smaller the application and greater the speed
    ' Comments go here...
    ~ Computer_Wiz ~

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