Listbox number format

In the code below, how can I get the numbers 0 through 9 that may show up in Listbox1 to be in two digit number format such as 00, 01, 02 etc... instead of single digit 0, 1, 2 etc... ?  

I also have questions about embedded fonts Q_21145120.html and embedded CHM files Q_21145835.html still open if anyone would like to see if they can assist there too.

Dim R As New System.Random(Now.Millisecond)

    Private Sub rangeMin_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rangeMin.ValueChanged, rangeMax.ValueChanged
        rangeMax.Minimum = rangeMin.Value
        numsPerLine.Maximum = (rangeMax.Value - rangeMin.Value) + 1
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim i As Integer
        Dim j As Integer
        Dim results() As Integer
        Dim output As String

        ListBox1.Items.Clear()
        For i = 1 To numLines.Value
            results = pickFromRange(rangeMin.Value, rangeMax.Value, numsPerLine.Value)

            output = CStr(results(0))
            For j = 1 To results.GetUpperBound(0)
                output = output & " - " & CStr(results(j))
            Next

            ListBox1.Items.Add(output)
        Next
    End Sub

    Private Function pickFromRange(ByVal minValue As Integer, ByVal maxValue As Integer, ByVal numberToPick As Integer) As Integer()
        Dim i As Integer
        Dim temp As Integer
        Dim swapWith As Integer
        Dim values(maxValue - minValue) As Integer
        Dim upperBound As Integer

        upperBound = values.GetUpperBound(0)

        ' create array with the range of values
        For i = 0 To upperBound
            values(i) = i + minValue
        Next

        ' for every value, pick another and swap them
        For i = 0 To upperBound
            swapWith = R.Next(0, upperBound + 1)
            temp = values(i)
            values(i) = values(swapWith)
            values(swapWith) = temp
        Next

        ' return the number of values requested
        Dim output(numberToPick - 1) As Integer
        Array.Copy(values, output, numberToPick)
        Return output
    End Function
teamdadAsked:
Who is Participating?
 
KGregConnect With a Mentor Commented:
   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim i As Integer
        Dim j As Integer
        Dim results() As Integer
        Dim output As String

        ListBox1.Items.Clear()
        For i = 1 To numLines.Value
            results = pickFromRange(rangeMin.Value, rangeMax.Value, numsPerLine.Value)

            For j = 0 To results.GetUpperBound(0)
                dim temp as String = IIf(cstr(results(j)).Length = 1, "0" + cstr(results(j)), cstr(results(j)))
   
                if output <> "" then output += " - "

                output += temp
            Next

            ListBox1.Items.Add(output)
           
            output = ""

        Next
    End Sub

KGREG
0
 
KGregCommented:
       IIf(output.Length = 1, output = "0" + output, Nothing)


KGreg
0
 
teamdadAuthor Commented:
KGreg,

Your code causes the wavey blue lines on the "0" + output and it says that Operator '+' is not defined for types String and 1 dimensional array of intiger.

Am I putting it in the wrong place or could something else be a problem?  Im using vb.net 2002 if it makes a diffrence.

' return the number of values requested
        Dim output(numberToPick - 1) As Integer
        Array.Copy(values, output, numberToPick)

        IIf(output.Length = 1, output = "0" + output, Nothing)

        Return output
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
KGregCommented:
   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim i As Integer
        Dim j As Integer
        Dim results() As Integer
        Dim output As String

        ListBox1.Items.Clear()
        For i = 1 To numLines.Value
            results = pickFromRange(rangeMin.Value, rangeMax.Value, numsPerLine.Value)

            output = CStr(results(0))
            For j = 1 To results.GetUpperBound(0)
                output = IIf(output.Length = 1, output = "0" + output, Nothing)
 & " - " & CStr(results(j))
            Next

            ListBox1.Items.Add(output)
        Next
    End Sub
0
 
teamdadAuthor Commented:
It doesn't give me the wavey blue line now and doesn't cause any errors when debugging but it don't produce two digit numbers either.
0
 
KGregCommented:
my mistake.. the line should be:

                output = IIf(output.Length = 1, "0" + output, output)

so in your code..

   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim i As Integer
        Dim j As Integer
        Dim results() As Integer
        Dim output As String

        ListBox1.Items.Clear()
        For i = 1 To numLines.Value
            results = pickFromRange(rangeMin.Value, rangeMax.Value, numsPerLine.Value)

            output = CStr(results(0))
 
            For j = 1 To results.GetUpperBound(0)
                output = IIf(output.Length = 1, "0" + output, output)
 & " - " & CStr(results(j))
            Next

            ListBox1.Items.Add(output)
        Next
    End Sub


Sorry about that,

KGREG
0
 
teamdadAuthor Commented:
Getting closer.... it works on the first number of each new line but it won't work on any numbers after the first.  The code I use produces random numbers from 0 to 99 in a format that looks like 1 - 19 - 5 - 10 etc... .  The code you have assisted me with so far does this: 01 - 19 - 5 - 10 etc...  any number that is less than 10 should have the extra 0 put in front of it.
0
 
KGregCommented:
god what an oversight!  sorry again :)

Here you go:


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim i As Integer
        Dim j As Integer
        Dim results() As Integer
        Dim output As String

        ListBox1.Items.Clear()
        For i = 1 To numLines.Value
            results = pickFromRange(rangeMin.Value, rangeMax.Value, numsPerLine.Value)

            For j = 0 To results.GetUpperBound(0)
                output = IIf(output.Length = 1, "0" + output, output)
                output += " - " + IIf(output.Length = 1, "0" + output, output)
            Next

            ListBox1.Items.Add(output)
        Next
    End Sub

Hope this helps!

KGreg

0
 
teamdadAuthor Commented:
It's still somehow leaving out   & " - " & CStr(results(j)) and isn't working quite the way I think it should.
0
 
KGregCommented:
man oh man.. this is what i get for doing 5 things at once:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim i As Integer
        Dim j As Integer
        Dim results() As Integer
        Dim output As String

        ListBox1.Items.Clear()
        For i = 1 To numLines.Value
            results = pickFromRange(rangeMin.Value, rangeMax.Value, numsPerLine.Value)

            For j = 0 To results.GetUpperBound(0)
                dim temp as String = IIf(cstr(results(j)).Length = 1, "0" + cstr(results(j)), cstr(results(j)))
   
                if output <> "" then output += " - "

                output += temp
            Next

            ListBox1.Items.Add(output)
        Next
    End Sub
0
 
teamdadAuthor Commented:
Still closer.... it does give every number 0 through 9 double digits but it doesn't provide a random set of numbers, they are all the same and stepped down. The row following the previous row has two more numbers added to it than what the user selects.
0
 
teamdadAuthor Commented:
KGreg,
Your code works great and I accept your answer.  I have another open question on embedded fonts if you would like to take a try at it too. http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/VB_DOT_NET/Q_21145120.html

And if you don't mind just one more tiny thing on this question, if I wanted to use a blank space instead of the extra 0 infront of the single digit numbers would I just use " " or would I have to use an ASCII symbol for a space?
0
 
KGregCommented:
you can use " " for the space.. you'd just change:

IIf(cstr(results(j)).Length = 1, "0" + cstr(results(j)), cstr(results(j)))


to:


IIf(cstr(results(j)).Length = 1, " " + cstr(results(j)), cstr(results(j)))


However, the numbers might not line up correctly due to the difference in character widths for some fonts.  You may want to use a font that has the same width for every character (courier, terminal, or system).. but these may not look so nice.


Thanks, and good luck!

KGREG
 

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.

All Courses

From novice to tech pro — start learning today.