Solved

Listbox number format

Posted on 2004-09-27
13
252 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

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…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

695 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