Results 1 to 16 of 16

Thread: [RESOLVED] Loading content of a text file located on a website into a textbox/combobox

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2008
    Posts
    75

    Resolved [RESOLVED] Loading content of a text file located on a website into a textbox/combobox

    Is it possible to load the content of a text file (.txt) into a listbox or combobox if the text file is located in a webpage?
    let's suppose that the file is located at http://www.mysite.com/mytextfile.txt
    how would i load the content of the text file into my listbox/combobox?

    Thank you in advance,

    Andrea

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Loading content of a text file located on a website into a textbox/combobox

    First up, that text file is not located "in a web page". A web page is an HTML file. That text file is located "on a web server", i.e. a server that serves web pages, but it's not itself a web page.

    As for your question, yes and no. You can certainly get the data into a control, but it's really a two step process. You must first download the data and store it locally, either in memory or in a file. You can then load that data into your control. Unless you specifically want a local text file, I'd suggest the in-memory approach.

    You can start by creating a WebClient object. It has a DownloadString method, which takes a URL and returns a String containing the contents of that URL, i.e. the contents of your text file. You can then do whatever you like with that String as it's just like any other String. You might call its Split method to split it on the line breaks into an array, then load that array into the control by assigning it to the control's DataSource.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Mar 2008
    Posts
    75

    Re: Loading content of a text file located on a website into a textbox/combobox

    Thanks for the answer.

    I already did a program which downloads a text file in C:\ and then it loads its content in the combobox. But If the user does not have a C:\ drive the program would not work.

    Could you please tell me more on how to load the textfile in memory so It would not mattter if the user does not have a c:\ drive?

    Thank you,

    Andrea

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Loading content of a text file located on a website into a textbox/combobox

    You should never hard-code paths like that. You should always either let the user select a location, e.g. using a SaveFileDialog, or else use a standard Windows folder, the path of which you can get via the Framework in code no matter where its absolute location is. The Environment.GetFolderPath and the My.Computer.FileSystem.SpecialDirectories object can both give you standard folder paths, e.g. the user's Temp folder and My Documents folder.
    Could you please tell me more on how to load the textfile in memory so It would not mattter if the user does not have a c:\ drive?
    You've already got all you need to do your own research and find relevant examples:
    You can start by creating a WebClient object. It has a DownloadString method
    When provided with keywords, the first thing you should do is search for them and see what you can find out for yourself. If you can't find what you need or don't understand what you find, then it's time to ask for more help.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Mar 2008
    Posts
    75

    Re: Loading content of a text file located on a website into a textbox/combobox

    Thanks for the hint! Got it now:

    Code:
            Dim client As WebClient = New WebClient()
            Dim MovieList As String = client.DownloadString("http://mywebsite.com/mytextfile.txt")
            ComboBox1.Items.Add(MovieList)
    Can you tell me how to "put" the list in the "right" order?

    On the website the list is showed:
    item1
    item2
    item3
    .......

    But in the combobox it will be showed like this:

    Item1Item2Item3.....

    Thanks a lot! =D

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Loading content of a text file located on a website into a textbox/combobox

    As I posted previously:
    You can then do whatever you like with that String as it's just like any other String. You might call its Split method to split it on the line breaks into an array, then load that array into the control by assigning it to the control's DataSource.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Mar 2008
    Posts
    75

    Re: Loading content of a text file located on a website into a textbox/combobox

    I have been trying but I cannot get it to work.
    I just tried this:
    Code:
            Dim split As String() = MovieList.Split(New [Char]() {" "c, CChar(vbtab)})
    
            For Each s As String In split
                If s.Trim() <> "" Then
                   ComboBox1.Text = ComboBox1.Items.Add(MovieList)
                End If
            Next s
    but now instead of getting:
    Item1Item2Item3.....

    I get:
    Item1Item2Item3.....
    Item1Item2Item3.....
    Item1Item2Item3.....
    .....

    Any help?

    EDIT:

    i want to add that every item has one or more spaces, for example:
    Item 1
    Item 2
    Item 3
    .......

    And not as I previusly said:

    Item1
    Iitem2
    Item3
    .......
    Last edited by Netmaster; Feb 6th, 2011 at 11:32 PM.

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Loading content of a text file located on a website into a textbox/combobox

    First of all, can you describe the format of your file? You appear to want to split on all space and Tab characters. Is that correct? I would assume not if you want the items to contain spaces.

    Even once you are splitting correctly, your loop is still broken. You are looping through the items in the list and then you are adding the whole list each time. You're supposed to be adding just the current item.

    You don't actually need a loop at all anyway. There is an overload of String.Split that allows you to specify a StringSplitOptions value, which allows you to omit empty items. That way you don't have to check for empty strings and you can just add the whole list to the control in one go. You do that by calling AddRange, which is for multiple items, rather than Add, which is for a single item.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Mar 2008
    Posts
    75

    Re: Loading content of a text file located on a website into a textbox/combobox

    I had already tried with addrange but it returns this error:
    value of type 'String' cannot be converted to 1-dimensional array of Object'

    I was thinking of something simpler too though... Since every item in the textfile ends with ")" I was thinking to do something like:
    If ")" found in string go to next line
    but I just cannot figure out a way to do it.




    EDIT:
    Code:
    First of all, can you describe the format of your file? You appear to want to split on all space and Tab characters. Is that correct? I would assume not if you want the items to contain spaces.
    I am confused, I did not need to do that. This is How the text file looks like:

    Name of Movie (1)
    Name of Movie (2)
    Name of Movie (3)
    Name of Movie (4)
    Name of Movie (5)
    Name of Movie (6)
    Name of Movie (7)
    Name of Movie (8)

    and so on...

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Loading content of a text file located on a website into a textbox/combobox

    That's what I thought it would be like in the first place, which is why I said:
    You might call its Split method to split it on the line breaks into an array
    That's what you should be doing.

    Just note that a line break may differ depending on the platform it was created on. Windows uses a string containing a carriage return and a line feed while other platforms use just a line feed character. If you know what yours is then you can use ControlChars.CrLf or ControlChars.Lf as appropriate. If you don't know then the safest option is to split on the line feeds and then trim the carriage returns off each element. Note that CrLf is a String while Lf is a Char, so they each require different overloads of Split.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Mar 2008
    Posts
    75

    Re: Loading content of a text file located on a website into a textbox/combobox

    I added an @ at the end of each item on the list and I tryed the following code:
    Code:
    Dim aStringArray() As String  'array of strings
            Dim s As String  'single string
            Dim i As Integer
            s = MovieList
    
            aStringArray = Split(s, "@")  'split string up by spaces
            For i = LBound(aStringArray) To UBound(aStringArray)
                ComboBox1.Items.Add(MovieList)
            Next i
    It does not work! I do not understand why!
    if instead of ComboBox1.Items.Add(MovieList) i put msgbox(movielist) the msg box will return every item in the list in the right order! (once per click, not all of them together)

    I am confused!
    Can some one please help me out? Thanks

  12. #12
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Loading content of a text file located on a website into a textbox/combobox

    You are missing something fundamental here. You keep looping through your list and then not using the current item inside the loop. In your latest code, you are using a For loop to loop through 'aStringArray' so, inside the loop, the current item is 'aStringArray(i)', not 'MovieList'.

    Also, don't use that Split function. Call the Split method of your String. Your code should look something like this:
    vb.net Code:
    1. Using client As New WebClient
    2.     Dim text As String = client.DownloadString("URL here")
    3.     Dim lines As String() = text.Split(New String() {ControlChars.CrLf}, StringSplitOptions.RemoveEmptyEntries)
    4.  
    5.     Me.ComboBox1.Items.AddRange(lines)
    6. End Using
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  13. #13

    Thread Starter
    Lively Member
    Join Date
    Mar 2008
    Posts
    75

    Re: Loading content of a text file located on a website into a textbox/combobox

    Wow thanks a lot! Now It finally works! thank you.

    Now I noticed that if i click the button twice the combobox loads an other time the list so I am going to have twice the same items!

    So I tried to do this (thng that I just tested and works fine if I do not use a txt file downloaded from a server):
    Code:
            For X = 0 To ComboBox1.Items.Count - 1
    
                If UCase(Trim(ComboBox1.Items(X))) = UCase(Trim(MovieList)) Then
                    'do nothing
                    Exit Sub
    
                End If
    
            Next
            Me.ComboBox1.Items.AddRange(lines)
    Unfortunatelly it does not work if I use the txt file which is on the server. It just keepsloading the same items everytime i click tu button.
    What i want is to only load the new items.

    My program works in this way:

    When the form is loaded it automaticalllly downloads into the combobox the list which is on the server.
    Then, the user might click on the text box to see if there is something new in the list.

    Any idea on how to do?

    Thanks a lot again,

    Andrea

  14. #14
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Loading content of a text file located on a website into a textbox/combobox

    You can either Clear the Items from the ComboBox first, or else do as I said in post #2 and set the DataSource instead of adding the items directly:
    vb.net Code:
    1. Using client As New WebClient
    2.     Dim text As String = client.DownloadString("URL here")
    3.     Dim lines As String() = text.Split(New String() {ControlChars.CrLf}, StringSplitOptions.RemoveEmptyEntries)
    4.  
    5.     Me.ComboBox1.DataSource = lines
    6. End Using
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  15. #15

    Thread Starter
    Lively Member
    Join Date
    Mar 2008
    Posts
    75

    Re: Loading content of a text file located on a website into a textbox/combobox

    Yes, I did try before to clear the combobox which works fine. But if the program is running and I change the content of the list on the server and then i click the refresh button it will not load the new list.
    To reload the new list i get to re-run the program.
    This is wierd!
    This is what I have under the refresh button:
    Code:
            ComboBox1.Items.Clear()
            ComboBox1.Items.AddRange(lines)
    If I use
    Code:
    ComboBox1.DataSource = lines
    it will not help, it will be the same thing as using
    Code:
     ComboBox1.Items.AddRange(lines)
    =(

    EDIT:

    Ithink i solved the problem by doing this under the refresh button:
    Code:
            Dim MovieListR As String = client.DownloadString("URL")
            Dim linesR As String() = MovieListR.Split(New String() {ControlChars.CrLf}, StringSplitOptions.RemoveEmptyEntries)
    
            ComboBox1.Items.Clear()
            ComboBox1.Items.AddRange(linesR)
    i will make a couple of test and report back!
    Last edited by Netmaster; Feb 7th, 2011 at 01:46 PM.

  16. #16

    Thread Starter
    Lively Member
    Join Date
    Mar 2008
    Posts
    75

    Re: Loading content of a text file located on a website into a textbox/combobox

    Well I just needed to have this:
    Code:
    Dim MovieList As String = client.DownloadString("URL")
            Dim lines As String() = MovieList.Split(New String() {ControlChars.CrLf}, StringSplitOptions.RemoveEmptyEntries)
    Under each sub instead of having it at the beginning under the Public Class Form1.

    Problem solved!
    Thanks a lot jmcilhinney ! ! !
    =D

Tags for this Thread

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