Results 1 to 4 of 4

Thread: Need some insight

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2012
    Posts
    6

    Unhappy Need some insight

    I am working on a task given by my instructor who has stated that his skills are not in programming at all but he agree to teach the class. The assignment itself is very basic and I am trying to follow the directions given in the book due to the fact the instructor does not state exactly what he wants. The issue I need insight with is in the btnCalculate where I am trying to code an event handler that can deal with the off chance that a user could click the Calculate button without any data entered. I run into a wall (InvalidCastException) with getting it to allow the user to enter data. I have tried While, Do until loops and decided the if statement would work. This occurs only after the messagebox is cleared. Here is the code:
    Code:
    Public Class LongDistanceCall
    
        Dim notANumber As Boolean
        Dim DAYRATE As Double = 0.07  ' long distance day time calling plan rate per minute
        Dim EVENRATE As Double = 0.12 ' Long distance evening calling plan rate per minute
        Dim OFFPEAK As Double = 0.05  ' long distance off peak hours calling plan rate per minute
        Dim MinutesUsed As Double
    
    
        Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
    
       ' ********************************************************************************************* problem area
        ' Vaildates the value of the number enter is higher then 0
            If (txtMinutesUsed.Text) = "" Then
                MessageBox.Show("No data? Really?? Come on, I need something here to work with please.", "ERROR DETECTED", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End If
    
            MinutesUsed = CDbl(txtMinutesUsed.Text) ' converts the value entered into a double of computation
    
      ' *********************************************************************************************  problem area
            If (MinutesUsed) <= 0 Then
                MessageBox.Show("Can not less then 1 Minute", "ERROR DETECTED", MessageBoxButtons.OK, MessageBoxIcon.Error)
                txtMinutesUsed.Text = String.Empty
            End If
    
            ' Performs the calculation based on which option is picked and displays a error in the event no option is selected 
            If RadDayTime.Checked = True Then
                lblCharges.Text = FormatCurrency(MinutesUsed * DAYRATE)
            ElseIf RadEvening.Checked = True Then
                lblCharges.Text = FormatCurrency(MinutesUsed * EVENRATE)
            ElseIf RadOffPeak.Checked = True Then
                lblCharges.Text = FormatCurrency(MinutesUsed * OFFPEAK)
            Else
                MessageBox.Show("Please Select one of the offered plans", "Missing information", MessageBoxButtons.OK, MessageBoxIcon.Information)
            End If
    
        End Sub
    
        Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
    
            ' Resets the values on clear or empty to prevent data overlaying
            lblCharges.Text = String.Empty
            RadDayTime.Checked = False
            RadEvening.Checked = False
            RadOffPeak.Checked = False
            txtMinutesUsed.Clear()
    
        End Sub
    
        Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
    
            ' closes the form and clears the memory
            Me.Close()
            End
    
        End Sub
    
        Private Sub txtMinutesUsed_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtMinutesUsed.KeyDown
    
            ' ensures only numbers from keypad or keys 2 thru 11 on row two work
    
            notANumber = False
    
            If e.KeyCode < Keys.D0 OrElse e.KeyCode > Keys.D9 Then
    
                If e.KeyCode < Keys.NumPad0 OrElse e.KeyCode > Keys.NumPad9 Then
    
                    If e.KeyCode <> Keys.Back Then
    
                        notANumber = True
    
                    End If
    
                End If
    
            End If
    
            If Control.ModifierKeys = Keys.Shift Then
    
                notANumber = True
    
            End If
    
        End Sub
    
       
        Private Sub txtMinutesUsed_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtMinutesUsed.KeyPress
    
            ' turns off non-numeric keys
    
            If notANumber = True Then
    
                e.Handled = True
    
            End If
    
        End Sub
    
    
    End Class

    Thank you for your time and input.

  2. #2
    Super Moderator FunkyDexter's Avatar
    Join Date
    Apr 2005
    Location
    An obscure body in the SK system. The inhabitants call it Earth
    Posts
    7,957

    Re: Need some insight

    Chit Chat is really the wrong place to post this unless you want an irrelevant discussion about politics, religion, IPhones or oceanic predators. Your post really belongs in the VB.Net forum so I've reported it to a mod to get it moved.

    I believe your problem is that you're detecting that the user has entered a blank in txtMinutesUsed and then trying to convert it to a double anyway. You need to exit your sub if txtMinutesUsed is blank.

    Orcas and Blue Whales FTW!
    The best argument against democracy is a five minute conversation with the average voter - Winston Churchill

    Hadoop actually sounds more like the way they greet each other in Yorkshire - Inferrd

  3. #3
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Need some insight

    Welcome to the forums DSUstudent59

    I have moved your question to VB.Net from Chit Chat. Here you will get appropriate responses.

    @FunkyDexter: Thanks for the report.
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: Need some insight

    FD has the problem. You need a Return statement right after the messagebox. Once the MessageBox has gone away, the next line will be the Return, which will exit the sub. The way you have it now, the next line will be the conversion, which can't happen because the textbox is empty.

    By the way, while the solution you have will catch empty strings, it is not all that robust. If somebody enters "Blue", the box won't be empty and the conversion will still fail. Rather than checking for empty, take a look in MSDN (or search on here, if the search feature is working, which it may not be yet) for Double.TryParse, which is really what you want to be using in this situation.
    My usual boring signature: Nothing

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width