Results 1 to 3 of 3

Thread: a little assistance needed

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2012
    Posts
    6

    a little assistance needed

    I have tried everything I could think of with no luck to speak of. What I am attempting to do is very simple. I would like to be able to put up a message box with an error and allow the user to enter data ( in the event that the user clicks on the calculate button without any data in the form. I am halfway there, but run into a strong wall in getting the code right. I welcome any and all helpful comments and or suggestions.

    Code:
    Public Class LongDistanceCall
    
        Dim notANumber As Boolean
        Dim DAYRATE As Double = 0.07
        Dim EVENRATE As Double = 0.12
        Dim OFFPEAK As Double = 0.05
        Dim MinutesUsed As Double
    
        Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
    
     ' ********************* start of problem area   **********************************
            MinutesUsed = CDbl(txtMinutesUsed.Text)
    
            If (MinutesUsed) <= 0 Then
                MessageBox.Show("Can not less then 1 Minute", "ERROR DETECTED", MessageBoxButtons.OK, MessageBoxIcon.Error)
                txtMinutesUsed.Text = String.Empty
            End If
    
     ' ********************* end of problem area   **********************************
    
            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("Pleae 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
    
            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
            Me.Close()
            End
    
        End Sub

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: a little assistance needed

    Ideally you would not have the validation code inside the Click event handler of the Button. Each control has it's own Validating even for that. You should handle the Validating event of the TextBox, validate the contents and set e.Cancel to True if it fails. That will prevent the user navigating away from the control until they enter valid data. To ensure that every control has been validated, even if they have never received focus, you call the form's ValidateChildren method in your Click event handler. That will return False if any control fails validation, so you only proceed to use the data if it returns True.

    That said, in this case, you could always just use a NumericUpDown and set its Minimum to 1. That would also prevent the user entering non-numeric text, which your current code doesn't take into consideration.

    If you must stick with the pattern you have then you need to do two things:

    1. Do not do anything further if the data is not valid. You have an If statement that does something if the data is invalid. If you want to do something else if the data is valid then isn't that what an Else statement is for?
    2. If the data is invalid then you want to focus the TextBox so that the user can just type and the input goes into that control. To do that you do NOT call the Focus method of the TextBox, as you might think. You call its Select method.

    If you are sticking with a TextBox then you might also consider the case where the TextBox is blank or contains other non-numeric text. Your current code will crash in that case. If this is an assignment and you've been told to assume that the data is numeric then that's OK but you'd never make such an assumption in a real-world app.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    New Member
    Join Date
    Sep 2012
    Posts
    6

    Wink Re: a little assistance needed

    Thank you for responding. The assignment book stated to allow only numbers and at the end of my code I allowed only numbers to be considered. The instructor does care about format or the "bells" the book states. I trying to start as close the book as possible. I can see your point and ask if you would give me some code to look at in resolving the issue. I can turn in the program has is, but I would like to be as real-world as possible.

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