Solved

Listbox number format

Posted on 2004-09-27
13
244 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

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
 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

726 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