• Status: Solved
  • Priority: Low
  • Security: Public
  • Views: 38
  • Last Modified:

using an array to input certain numbers in a listbox

im having trouble setting up this code, i have a file with a bunch of number in it, and its placed in my debug bin folder. i have 3 list boxes and i need to make 1 listbox have numbers 0-50, the second to have 51-75 and the third 75-100, how do i do that using an array.
scores.txt
0
bob saget
Asked:
bob saget
  • 2
1 Solution
 
Fernando SotoRetiredCommented:
Hi Bob;

Before I could update your other question I was working on loading the ListBox's with the data. Here is that code.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    '' Linq quert to read the data from the file and parse them into there groups
    '' Group 1 = 0 - 50
    '' Group 2 = 51 - 75
    '' Group 3 = 76 - 100
    Dim parseData = (From valStr As String In File.ReadAllLines("C:\Working Directory\scores.txt")
                     Let valInt = GetRange(valStr)
                     Group By valInt.Item1 Into Group
                     Select New With {
                        .Grouping = Group.First().valInt.Item1,
                        .Scores = Group.Select(Function(s) s.valInt.Item2).ToArray(),
                        .Count = Group.Count
                     }).ToList()

    '' Place the values into the correct label control
    For Each g In parseData
        If g.Grouping = 1 Then
            '' Load the label with the results
            lbl0_50.Text = g.Count.ToString()
            '' Load the ListBox with the values
            lbG1.DataSource = g.Scores
        ElseIf g.Grouping = 2 Then
            lbl51_75.Text = g.Count.ToString()
            lbG2.DataSource = g.Scores
        Else
            lbl76_100.Text = g.Count.ToString()
            lbG3.DataSource = g.Scores
        End If
    Next

End Sub

'' Helper function to determin what group a score is in.
Private Function GetRange(ByVal inp As String) As Tuple(Of Integer, Integer)
    Dim itemGroup As Integer = -99
    Dim itemValue As Integer = -99
    Integer.TryParse(inp, itemValue)

    If itemValue >= 0 AndAlso itemValue <= 50 Then
        itemGroup = 1
    ElseIf itemValue >= 51 AndAlso itemValue <= 75 Then
        itemGroup = 2
    Else
        itemGroup = 3
    End If

    Return New Tuple(Of Integer, Integer)(itemGroup, itemValue)
End Function

Open in new window

0
 
bob sagetAuthor Commented:
thx boss
0
 
Fernando SotoRetiredCommented:
Not a problem Bob, glad to help.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now