Click here to Skip to main content
15,911,646 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a small project that I am trying to generate different currency amounts ranging from under $1.00 to $9.99. I would like to generate these amounts by one of the following options: Dollars & Cents, Dollars (only), Cents (only)
The Dollars & Cents function and the Cents function work OK, but I cannot seem to get just Dollar function to work(ie: $5.00, $2.00, $8.00). What am I doing wrong?

What I have tried:

VB
Public Class Form1
    Public MaxAmount As Integer
    Public FrmattedAmt As New Random

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
        RadioButton4.Hide()
        RadioButton5.Hide()
        RadioButton6.Hide()
        Button2.Enabled = False

    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        If RadioButton1.Checked = False And RadioButton2.Checked = False And RadioButton3.Checked = False Then Exit Sub

        Select Case True

            Case RadioButton1.Checked
                MaxAmount = 999

            Case RadioButton2.Checked
                MaxAmount = 9

            Case RadioButton3.Checked
                MaxAmount = 99

        End Select

        Randomize()

        TextBox1.Text = FormatCurrency(Math.Round(FrmattedAmt.Next(0, MaxAmount) / 100, 2), 2)

        RadioButton1.Enabled = False
        RadioButton2.Enabled = False
        RadioButton3.Enabled = False

        Button1.Enabled = False
        Button2.Enabled = True

    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Button1.Enabled = True
        RadioButton1.Checked = False
        RadioButton1.Enabled = True
        RadioButton2.Checked = False
        RadioButton2.Enabled = True
        RadioButton3.Checked = False
        RadioButton3.Enabled = True
        RadioButton4.Hide()
        RadioButton5.Hide()
        RadioButton6.Hide()
        TextBox1.Clear()
        Button2.Enabled = False

    End Sub
End Class
Posted
Updated 18-Sep-18 7:49am
v5
Comments
[no name] 18-Sep-18 9:55am    
I'm sorry, I was just testing this multiple times and the Dollars & Cents function just came up with Cents only.

I don't understand your approach...

If you're using radio buttons (and put them in the same group), only one of them can be selected (checked). Why don't you subscribe to the checked events of the radio buttons and set the max amount from there, saves you a lot of code. Also it's unknown to us what the radio buttons mean, since you don't name them properly and you didn't provide an image of the GUI or something.

We can't see what the randomize() function does? (by the way, it may be wise to use the seed parameter for the Random() to prevent it from generating the same number sequences.

Why the dollar only doesn't work, is because you device the random generated number by 100 every time. I think, when you generate a round dollar amount, you're max must be set to 9, and you shouldn't divide by 100
 
Share this answer
 
Comments
[no name] 18-Sep-18 12:39pm    
Thank you for the suggestion about the Radio Buttons. I didn't rename them because I am only experimenting with this code at this point. I plan to add more code to change the text of Radio Buttons 4,5 and 6 at a later time. RB1 = Dollars & Cents, RB2 = Dollars, RB3 = Cents. I commented that I checked the Dollars & Cents function and it generates amounts under a dollar too. Try as I might, I cannot add a link to the question with a image of my form - ?
Thank you (sincerely) for the tips. I figured it out:
VB
<pre>Public MaxAmount As Integer
    Public FrmattedAmt As New Random

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
        RadioButton4.Hide()
        RadioButton5.Hide()
        RadioButton6.Hide()
        Button1.Enabled = False
        Button2.Enabled = False

    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        If RadioButton1.Checked = False And RadioButton2.Checked = False And RadioButton3.Checked = False Then Exit Sub

        Randomize()

        Select Case MaxAmount

            Case 9 ' Dollars
                TextBox1.Text = FormatCurrency(Math.Round(FrmattedAmt.Next(1, MaxAmount), 2), 2)
            Case 99 'Cents
                TextBox1.Text = FormatCurrency(Math.Round(FrmattedAmt.Next(10, MaxAmount) / 100, 2), 2)
            Case 999 ' Dollars & Cents
                TextBox1.Text = FormatCurrency(Math.Round(FrmattedAmt.Next(1, MaxAmount) / 100, 2), 2)

        End Select

        TextBox1.Select(0, 0)
        RadioButton1.Enabled = False
        RadioButton2.Enabled = False
        RadioButton3.Enabled = False

        Button1.Enabled = False
        Button2.Enabled = True

    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        RadioButton1.Checked = False
        RadioButton1.Enabled = True
        RadioButton2.Checked = False
        RadioButton2.Enabled = True
        RadioButton3.Checked = False
        RadioButton3.Enabled = True
        RadioButton4.Hide()
        RadioButton5.Hide()
        RadioButton6.Hide()
        TextBox1.Clear()
        Button1.Enabled = False
        Button2.Enabled = False

    End Sub

    Private Sub RadioButton1_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton1.CheckedChanged
        RadioButton1.Font = IIf(RadioButton1.Checked = True, New Font(RadioButton1.Font, FontStyle.Bold), New Font(RadioButton1.Font, FontStyle.Regular))
        If RadioButton1.Checked = True Then
            MaxAmount = 999
            Button1.Enabled = True
        End If
    End Sub

    Private Sub RadioButton2_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton2.CheckedChanged
        RadioButton2.Font = IIf(RadioButton2.Checked = True, New Font(RadioButton2.Font, FontStyle.Bold), New Font(RadioButton2.Font, FontStyle.Regular))
        If RadioButton2.Checked = True Then
            MaxAmount = 9
            Button1.Enabled = True
        End If
    End Sub

    Private Sub RadioButton3_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton3.CheckedChanged
        RadioButton3.Font = IIf(RadioButton3.Checked = True, New Font(RadioButton3.Font, FontStyle.Bold), New Font(RadioButton3.Font, FontStyle.Regular))
        If RadioButton3.Checked = True Then
            MaxAmount = 99
            Button1.Enabled = True
        End If
    End Sub
End Class
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900