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
Solved

Listbox number format

Posted on 2004-09-27
13
236 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

 

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

860 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