Solved

Listbox number format

Posted on 2004-09-27
13
209 Views
Last Modified: 2010-04-23
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
0
Comment
Question by:teamdad
  • 7
  • 6
13 Comments
 
LVL 6

Expert Comment

by:KGreg
ID: 12163533
       IIf(output.Length = 1, output = "0" + output, Nothing)


KGreg
0
 

Author Comment

by:teamdad
ID: 12164559
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
 
LVL 6

Expert Comment

by:KGreg
ID: 12165615
   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
 

Author Comment

by:teamdad
ID: 12165988
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
 
LVL 6

Expert Comment

by:KGreg
ID: 12169152
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
 

Author Comment

by:teamdad
ID: 12173835
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 6

Expert Comment

by:KGreg
ID: 12173895
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
 

Author Comment

by:teamdad
ID: 12174102
It's still somehow leaving out   & " - " & CStr(results(j)) and isn't working quite the way I think it should.
0
 
LVL 6

Expert Comment

by:KGreg
ID: 12174170
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
 

Author Comment

by:teamdad
ID: 12174503
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
 
LVL 6

Accepted Solution

by:
KGreg earned 150 total points
ID: 12179886
   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
 

Author Comment

by:teamdad
ID: 12180824
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
 
LVL 6

Expert Comment

by:KGreg
ID: 12180912
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

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Need help with a query 6 55
Form design in vb.net 7 20
Javascript Refresh Parent Page from Popup 3 27
Round up to 100% in .NET 10 45
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now