[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
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:
Dim List As String = client.DownloadString("http://mywebsite.com/mytextfile.txt")
Dim lines As String() = List.Split(New String() {ControlChars.CrLf}, StringSplitOptions.RemoveEmptyEntries)
Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbShowA.CheckedChanged
ComboBox1.Items.Clear()
For i As Integer = 0 To lines.Count - 1
If lines(i).Trim.EndsWith("a)") Then
ComboBox1.Items.Add(lines(i))
End If
Next
End Sub
Private Sub RadioButton2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbShowG.CheckedChanged
ComboBox1.Items.Clear()
For i As Integer = 0 To lines.Count - 1
If lines(i).Trim.EndsWith("g)") Then
ComboBox1.Items.Add(lines(i))
End If
Next
End Sub
Private Sub RadioButton3_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbShowBoth.CheckedChanged
ComboBox1.Items.Clear()
ComboBox1.Items.AddRange(lines)
End Sub
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:
Public Class Form1
Dim lines() As String
Dim loaded As Boolean = False
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim client As New Net.WebClient
Dim List As String = client.DownloadString("http://mywebsite.com/mytextfile.txt")
lines = List.Split(New String() {ControlChars.CrLf}, StringSplitOptions.RemoveEmptyEntries)
ComboBox1.Items.Clear()
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() {}))))
loaded = True
End Sub
Private Sub RadioButtons_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton2.CheckedChanged, RadioButton1.CheckedChanged, RadioButton3.CheckedChanged
If Not loaded Then Return
ComboBox1.Items.Clear()
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() {}))))
End Sub
End Class
i used RadioButton1 = all,RadioButton2 = g,RadioButton3 = a
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:
Dim aLines = (From line In lines
Where line.EndsWith("a)")
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.
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
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?
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?
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
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
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 =(
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?
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
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.
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