Results 1 to 14 of 14

Thread: [RESOLVED] how to add to a combobox a list of item (not every item) in a txt located on a server

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2008
    Posts
    75

    Resolved [RESOLVED] how to add to a combobox a list of item (not every item) in a txt located on a server

    Hi, my program downloads into a combobox every item(line) that is in a text file located on a server.
    Every line in the text file ends with "a)" or with "g)".

    Now I just want to make 3 radio buttons:

    - one that will allow the download in the combobox only the item ending with "g)"
    - one that will allow the download in the combobox only the item ending with "a)"
    - one that will allow the download in the combobox of both (I already know how to do).

    How can I do so?

    To download the entire list in the combobox I do:

    Dim List As String = client.DownloadString("http://mywebsite.com/mytextfile.txt)
    Dim lines As String() = List.Split(New String() {ControlChars.CrLf}, StringSplitOptions.RemoveEmptyEntries)
    ComboBox1.Items.Clear()
    ComboBox1.Items.AddRange(lines)


    Thanks in advance,

    Andrea

  2. #2
    Frenzied Member stateofidleness's Avatar
    Join Date
    Jan 2009
    Posts
    1,780

    Re: how to add to a combobox a list of item (not every item) in a txt located on a se

    Try:

    EDIT: Non-Linq method. Not everyone is targeting the latest and greatest

    vb.net Code:
    1. Dim List As String = client.DownloadString("http://mywebsite.com/mytextfile.txt")
    2. Dim lines As String() = List.Split(New String() {ControlChars.CrLf}, StringSplitOptions.RemoveEmptyEntries)
    3.  
    4. Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbShowA.CheckedChanged
    5.         ComboBox1.Items.Clear()
    6.         For i As Integer = 0 To lines.Count - 1
    7.             If lines(i).Trim.EndsWith("a)") Then
    8.                 ComboBox1.Items.Add(lines(i))
    9.             End If
    10.         Next
    11.     End Sub
    12.  
    13.     Private Sub RadioButton2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbShowG.CheckedChanged
    14.         ComboBox1.Items.Clear()
    15.         For i As Integer = 0 To lines.Count - 1
    16.             If lines(i).Trim.EndsWith("g)") Then
    17.                 ComboBox1.Items.Add(lines(i))
    18.             End If
    19.         Next
    20.     End Sub
    21.  
    22.     Private Sub RadioButton3_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbShowBoth.CheckedChanged
    23.         ComboBox1.Items.Clear()
    24.         ComboBox1.Items.AddRange(lines)
    25.     End Sub

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: how to add to a combobox a list of item (not every item) in a txt located on a se

    try this:

    vb Code:
    1. Public Class Form1
    2.  
    3.     Dim lines() As String
    4.     Dim loaded As Boolean = False
    5.  
    6.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    7.         Dim client As New Net.WebClient
    8.         Dim List As String = client.DownloadString("http://mywebsite.com/mytextfile.txt")
    9.         lines = List.Split(New String() {ControlChars.CrLf}, StringSplitOptions.RemoveEmptyEntries)
    10.         ComboBox1.Items.Clear()
    11.         ComboBox1.Items.AddRange(If(RadioButton1.Checked, lines, If(RadioButton2.Checked, lines.Where(Function(l) l.EndsWith("g")).ToArray, If(RadioButton3.Checked, lines.Where(Function(l) l.EndsWith("a")).ToArray, New String() {}))))
    12.         loaded = True
    13.     End Sub
    14.  
    15.     Private Sub RadioButtons_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton2.CheckedChanged, RadioButton1.CheckedChanged, RadioButton3.CheckedChanged
    16.         If Not loaded Then Return
    17.         ComboBox1.Items.Clear()
    18.         ComboBox1.Items.AddRange(If(RadioButton1.Checked, lines, If(RadioButton2.Checked, lines.Where(Function(l) l.EndsWith("g")).ToArray, If(RadioButton3.Checked, lines.Where(Function(l) l.EndsWith("a")).ToArray, New String() {}))))
    19.     End Sub
    20.  
    21. End Class

    i used RadioButton1 = all,RadioButton2 = g,RadioButton3 = a

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

    Re: how to add to a combobox a list of item (not every item) in a txt located on a se

    You're going to have to download the entire file regardless, so that part of the code will remain the same. I'd probably suggest declaring the 'lines' variable at the class level, so you can access it multiple times and change the filter. If that's not required then you can keep the local variable.

    What you basically need to do then is extract out the values you need from that array. You'd use an If...ElseIf...Else block to test which of the three RadioButtons was checked. If it's the one for all then you can just use the array as is. If it's one of the others then you have two main options:

    1. If you're using .NET 3.5 or later, you can use LINQ, e.g.
    vb.net Code:
    1. Dim aLines = (From line In lines
    2.               Where line.EndsWith("a)")
    3.               Select line).ToArray()
    You can then add that array to the ComboBox just as you did the other. If it already contains data then be sure to clear it first.

    2. Loop through the array and add each item to a List(Of String) if it meets the appropriate criterion. You can then use the List or call its ToArray method.
    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: how to add to a combobox a list of item (not every item) in a txt located on a se

    Thanks for the answers guys!
    .paul. and stateofidleness the coe you suggested doe not work thank you anyway!

    jmcilhinney I will try to do as you suggest and I will report back! Thanks.

    Thank you all,

    Andrea

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: how to add to a combobox a list of item (not every item) in a txt located on a se

    it worked when i tested it. which version of vb.net are you using?

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Mar 2008
    Posts
    75

    Re: how to add to a combobox a list of item (not every item) in a txt located on a se

    2008. I just tested it on 2010 and it does not work as well
    What version do you use?

  8. #8
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: how to add to a combobox a list of item (not every item) in a txt located on a se

    oh. right. it's "g)" + "a)", not "g" + "a"
    it should work if you change those

  9. #9
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: how to add to a combobox a list of item (not every item) in a txt located on a se

    i tested it with my own text file in vb2008

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Mar 2008
    Posts
    75

    Re: how to add to a combobox a list of item (not every item) in a txt located on a se

    mmm It works when i select the first button (it loads every item), but it does not load anything when i select the other two radionboxes.
    I changed "a" with "A]" and "g" with "G]" since every line on my txt file ends with one of those but no luck =(

  11. #11
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: how to add to a combobox a list of item (not every item) in a txt located on a se

    did you declare lines at class level + use the loaded boolean variable i used?

  12. #12

    Thread Starter
    Lively Member
    Join Date
    Mar 2008
    Posts
    75

    Re: how to add to a combobox a list of item (not every item) in a txt located on a se

    YES I DID BUT....
    I forgot to chanc "a" and "g" to "A]" and "G]" in the radio buttons procedure.
    I will try and let you know!
    thanks a lot,

    andrea

  13. #13
    Frenzied Member stateofidleness's Avatar
    Join Date
    Jan 2009
    Posts
    1,780

    Re: how to add to a combobox a list of item (not every item) in a txt located on a se

    hmm mine works too...

    in the first post you say they end with "a)" and "g)"
    then, you just said they end with "a]" and "g]"

    which is it? code I posted looks for "a)" and "g)" so that might be your problem.

  14. #14

    Thread Starter
    Lively Member
    Join Date
    Mar 2008
    Posts
    75

    Re: how to add to a combobox a list of item (not every item) in a txt located on a se

    sorry it is "a]" and "g]"
    an b4 i just changed it on the load event! not on the radiobutton event! that is why it did not work!
    Thanks a lot guys! yeyy
    =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