Question What is wrong here?

CNC_Man

Member
Joined
Dec 5, 2009
Messages
16
Programming Experience
Beginner
Hi all,
I'm trying to create a Lotto Number Generator.
My Form has a 3 numeric up down's
2 for the Range "Min & Max"
1 for the Nuber of Numbers that are supposed to be displayed in my TextBox "Mumber"
1 TextBox"tbNumbers"
2 Buttons
The first just generates a single Number in the Range.
VB.NET:
 Dim SingleNumber As New Random
        Dim NumberRange As Integer = SingleNumber.Next(CInt(Min.Value), CInt(Max.Value))
        tbNumbers.Text = NumberRange.ToString
The Second Button needs to generate a given Number of Numbers in that Range, that is where I have a problem.
VB.NET:
' Single Number as Random
        Dim SingleNumber As New Random
        'Number Range as Min. and Max Values of Random
        Dim NumberRange As Integer = SingleNumber.Next(CInt(Min.Value), CInt(Max.Value))
        'Number, How many Numbers to Display
        Dim Number As Integer = Number
        For I As Integer = 0 To Number
            tbNumbers.Text = NumberRange.ToString & vbCrLf & Number

        Next
Thanks in Advance!
 
First up, you want your loop to start at 1, not 0.

Secondly, the secondly, you need to +1 to your max value.

Thirdly, if you want to generate multiple values then you have to call Next multiple times, which means calling it inside the loop.
 
Thanks for the reply!
Biggest problem here is that I'm overstepping my boundaries.
I have tried a number of different approaches but with no success.
Maybe I'll try again some other time.
 
This is the one you want

Private Sub ShowRandomNumbersInARange()
Randomize(Timer)
Dim Min = 1000
Dim Max = 9999
Dim Numbers = 5
Dim Result As String = ""
For i As Integer = 1 To Numbers
Result &= Min + Int(Rnd() * (Max - Min + 1)) & vbNewLine
Next
MsgBox(Result)
End Sub
[/COLOR]
 
Last edited:
Thank you so much!!
Sometimes it's the little things that make a big differance

In case someone else is looking for this here is the Modification I made for my App.

VB.NET:
Private Sub ShowRandomNumbersInARange()
        Randomize()
        Dim Min = Min1.Value
        Dim Max = Max1.Value
        Dim Numbers = Number.Value
        Dim Result As String = ""
        For i As Integer = 1 To CInt(Numbers)
            Result &= Min + Int(Rnd() * (Max - Min + 1)) & vbNewLine
        Next
        tbNumbers.Text = Result

    End Sub

   

    Private Sub btnMulti_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMulti.Click
        ShowRandomNumbersInARange()
    End Sub
 
I do have one more Question
I'm displaying the Numbers in a Text Box and was wondering if there would be any way to change the Last Numbers Fore color to Blue instead of Black.
Thanks!
 
Back Color

TextBox1.BackColor=color.Blue
If you want partial color Black and Partial Blue then use RichTextBox at the place of TextBox
 
Also I don't know what is mean by LAST NUMBERS

OK! Let me try to explain There may be a better solution to what I'm trying to accomplish if so, I'm very open for suggestions.

As you know the App. that I'm working on generates numbers into a Text Box
they are supposed to be lottery Numbers. I'm not interested in Changing the BackColor of the Text box What I would like to do, is Change the ForeColor of the Last generated Number in my TextBox.
It would be like telling someone that it is lets say "the Power ball"

Hope that this makes sense now!
 
Ok I found a partial solution
VB.NET:
            tbNumbers.ScrollToCaret()
            tbNumbers.SelectionStart = tbNumbers.TextLength = -1
            tbNumbers.SelectionLength = 2
            tbNumbers.SelectionColor = Color.Red

But I soon realized that the ScrollToCaret in a RichTextBox goes to the Top and not the Bottemlike a TextBox would.
So how would I calculate where to go with it?
Thanks!
 
ScrollToCaret does exactly what it says: it scrolls to the caret. If the caret is at the beginning of the text then it will scroll to the beginning; if the caret's at the end it will scroll to the end. That goes for both a RichTextBox and a TextBox.
 
Having reviwed it with a frech set of eyes "so to speak" I'm almost embarrest
but anyway just in case someone else needs this.

VB.NET:
'Start Selection in front of last 2 numbers
            tbNumbers.SelectionStart = tbNumbers.TextLength - 8
            'If desired, Scroll to caret
            tbNumbers.ScrollToCaret()
            'Set my Selection Lenght
            tbNumbers.SelectionLength = 10
            'Display Selection in Red
            tbNumbers.SelectionColor = Color.Red
 
Back
Top