An Error with a program

Jblake00

Well-known member
Joined
Sep 27, 2003
Messages
76
Ok here is my code.

Dim sum As Decimal
sum = CDbl(TxtListNumBooks.Text) * 0.1 * CDbl(TxtListNumDaysLate.Text)


LblShowFines.Text = "Your total fine is: " & CStr(FormatCurrency(sum))
LblShowFines.BorderStyle = BorderStyle.FixedSingle


What this does is make a simple program that calculates a fine.
It seems to work fine as long as I type an input into the test boxs.
I can even put 0 and 0 in them.
The problem occurs when I dont put an input into them.
Any suggestions.??

Btw I need a little input it making another form popup if fine = more than so many dollars.
 
You can check if the Text of the Textboxes is empty or not and if it is then stop the execution of the calculation.
Code:
If TextBoxObject.Text = " Then
tell the user to input something
End If
 
Not exactly what I was looking for but thanks.
Im sorry I didnt clarify what I wanted my result to be the first time.
But this is what I what it to do.
If no input is added when the Button calculate fines is pushed I want it to display " your fine is: $0.00
 
[VB]ublic Class Form1
Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

This call is required by the Windows Form Designer.
InitializeComponent()

Add any initialization after the InitializeComponent() call

End Sub

Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

NOTE: The following procedure is required by the Windows Form Designer
It can be modified using the Windows Form Designer.
Do not modify it using the code editor.
Friend WithEvents LblTypeNumBooks As System.Windows.Forms.Label
Friend WithEvents LblTypeNumDaysLate As System.Windows.Forms.Label
Friend WithEvents LblShowFines As System.Windows.Forms.Label
Friend WithEvents LlbThankYou As System.Windows.Forms.Label
Friend WithEvents TxtListNumBooks As System.Windows.Forms.TextBox
Friend WithEvents TxtListNumDaysLate As System.Windows.Forms.TextBox
Friend WithEvents BtnCalulateFines As System.Windows.Forms.Button
Friend WithEvents BtnExit As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.LblTypeNumBooks = New System.Windows.Forms.Label()
Me.LblTypeNumDaysLate = New System.Windows.Forms.Label()
Me.LblShowFines = New System.Windows.Forms.Label()
Me.LlbThankYou = New System.Windows.Forms.Label()
Me.TxtListNumBooks = New System.Windows.Forms.TextBox()
Me.TxtListNumDaysLate = New System.Windows.Forms.TextBox()
Me.BtnCalulateFines = New System.Windows.Forms.Button()
Me.BtnExit = New System.Windows.Forms.Button()
Me.SuspendLayout()

LblTypeNumBooks

Me.LblTypeNumBooks.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.LblTypeNumBooks.ForeColor = System.Drawing.Color.Blue
Me.LblTypeNumBooks.Location = New System.Drawing.Point(8, 40)
Me.LblTypeNumBooks.Name = "LblTypeNumBooks"
Me.LblTypeNumBooks.Size = New System.Drawing.Size(112, 32)
Me.LblTypeNumBooks.TabIndex = 0
Me.LblTypeNumBooks.Text = "Type the number of books here ----->"

LblTypeNumDaysLate

Me.LblTypeNumDaysLate.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.LblTypeNumDaysLate.ForeColor = System.Drawing.Color.Blue
Me.LblTypeNumDaysLate.Location = New System.Drawing.Point(8, 88)
Me.LblTypeNumDaysLate.Name = "LblTypeNumDaysLate"
Me.LblTypeNumDaysLate.Size = New System.Drawing.Size(112, 32)
Me.LblTypeNumDaysLate.TabIndex = 1
Me.LblTypeNumDaysLate.Text = "Type the number of days late ------>"

LblShowFines

Me.LblShowFines.Font = New System.Drawing.Font("Microsoft Sans Serif", 10.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.LblShowFines.Location = New System.Drawing.Point(336, 56)
Me.LblShowFines.Name = "LblShowFines"
Me.LblShowFines.Size = New System.Drawing.Size(144, 32)
Me.LblShowFines.TabIndex = 2
Me.LblShowFines.TextAlign = System.Drawing.ContentAlignment.MiddleCenter

LlbThankYou

Me.LlbThankYou.Font = New System.Drawing.Font("Microsoft Sans Serif", 12.0!, (System.Drawing.FontStyle.Bold Or System.Drawing.FontStyle.Italic), System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.LlbThankYou.ForeColor = System.Drawing.Color.Blue
Me.LlbThankYou.Location = New System.Drawing.Point(136, 136)
Me.LlbThankYou.Name = "LlbThankYou"
Me.LlbThankYou.Size = New System.Drawing.Size(280, 23)
Me.LlbThankYou.TabIndex = 3
Me.LlbThankYou.Text = "Thanks for your prompt payment"
Me.LlbThankYou.TextAlign = System.Drawing.ContentAlignment.MiddleCenter

TxtListNumBooks

Me.TxtListNumBooks.BorderStyle = System.Windows.Forms.BorderStyle.None
Me.TxtListNumBooks.Font = New System.Drawing.Font("Microsoft Sans Serif", 12.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.TxtListNumBooks.Location = New System.Drawing.Point(144, 40)
Me.TxtListNumBooks.Multiline = True
Me.TxtListNumBooks.Name = "TxtListNumBooks"
Me.TxtListNumBooks.Size = New System.Drawing.Size(72, 20)
Me.TxtListNumBooks.TabIndex = 4
Me.TxtListNumBooks.Text = ""
Me.TxtListNumBooks.TextAlign = System.Windows.Forms.HorizontalAlignment.Center

TxtListNumDaysLate

Me.TxtListNumDaysLate.AllowDrop = True
Me.TxtListNumDaysLate.BorderStyle = System.Windows.Forms.BorderStyle.None
Me.TxtListNumDaysLate.Font = New System.Drawing.Font("Microsoft Sans Serif", 12.0!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.TxtListNumDaysLate.Location = New System.Drawing.Point(144, 88)
Me.TxtListNumDaysLate.Multiline = True
Me.TxtListNumDaysLate.Name = "TxtListNumDaysLate"
Me.TxtListNumDaysLate.Size = New System.Drawing.Size(72, 20)
Me.TxtListNumDaysLate.TabIndex = 5
Me.TxtListNumDaysLate.Text = ""
Me.TxtListNumDaysLate.TextAlign = System.Windows.Forms.HorizontalAlignment.Center

BtnCalulateFines

Me.BtnCalulateFines.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.BtnCalulateFines.Location = New System.Drawing.Point(224, 56)
Me.BtnCalulateFines.Name = "BtnCalulateFines"
Me.BtnCalulateFines.Size = New System.Drawing.Size(104, 32)
Me.BtnCalulateFines.TabIndex = 6
Me.BtnCalulateFines.Text = "Calculate Fine"

BtnExit

Me.BtnExit.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.BtnExit.Location = New System.Drawing.Point(392, 168)
Me.BtnExit.Name = "BtnExit"
Me.BtnExit.Size = New System.Drawing.Size(56, 23)
Me.BtnExit.TabIndex = 7
Me.BtnExit.Text = "E&xit"

Form1

Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.BackColor = System.Drawing.Color.FromArgb(CType(128, Byte), CType(255, Byte), CType(128, Byte))
Me.ClientSize = New System.Drawing.Size(488, 198)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.BtnExit, Me.BtnCalulateFines, Me.TxtListNumDaysLate, Me.TxtListNumBooks, Me.LlbThankYou, Me.LblShowFines, Me.LblTypeNumDaysLate, Me.LblTypeNumBooks})
Me.Name = "Form1"
Me.Text = "Library Fine Calulation"
Me.ResumeLayout(False)

End Sub

#End Region

Private Sub BtnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnExit.Click
End

End Sub

Private Sub LblShowFines_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LblShowFines.Click


End Sub

Private Sub BtnCalulateFines_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnCalulateFines.Click


Dim sum As Double

sum = CDbl(TxtListNumBooks.Text) * 0.1 * CDbl(TxtListNumDaysLate.Text)

LblShowFines.Text = "Your total fine is: " & CStr(FormatCurrency(sum))
LblShowFines.BorderStyle = BorderStyle.FixedSingle


End Sub

Private Sub TxtListNumBooks_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TxtListNumBooks.TextChanged
TxtListNumBooks.TextAlign = HorizontalAlignment.Center

End Sub

Private Sub TxtListNumDaysLate_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TxtListNumDaysLate.TextChanged
TxtListNumDaysLate.TextAlign = HorizontalAlignment.Center

End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

End Sub

Private Sub LblTypeNumBooks_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LblTypeNumBooks.Click

End Sub
End Class
[/VB]
 
Ok there is my whole code if some could please tell me what and were to add something to make it display "Your total fine is: $0.00 if there is no input in the text boxes.
 
Code:
    Private Sub BtnCalulateFines_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnCalulateFines.Click


        Dim sum As Double

        Try
           sum = CDbl(TxtListNumBooks.Text) * 0.1 * CDbl(TxtListNumDaysLate.Text)
        Catch ex As Exception
            sum = 0
        End Try

        LblShowFines.Text = "Your total fine is:   " & CStr(FormatCurrency(sum))
        LblShowFines.BorderStyle = BorderStyle.FixedSingle


    End Sub
 
Thanks
That solves half of the problem at least. The program no longer crashes in the debug stage when the calculate fine button is pressed, however it does not display my message "Your total fine is: $0.00.....
I really need to figure out how to do that.
Thank again, I really appreciate all the help I have been getting from you people.
This is a learning experience for me. It mean allot more to me than just getting a good grade. Programing is something I have really wanted to learn for a long time. I want yall to know that I dont just come here looking to be told about how to do something before I make many attemps to solve the problem myself. I use my help and search features that come we VB.net and search the forums looking for an answer before I post. If the answer to my question has already been post before and I have overlooked it. A link to that post would be a fine way to answer my question.
 
Oops that caused the program to display no results when I added to the code even when I had an input in the text boxes.
I replaced the code with it I wander if I should have added it under my code?
 
Code:
Private Sub BtnCalulateFines_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnCalulateFines.Click
        Clayton I am have much problems with this code.
        When I use this code it works fine as long as there is an input.
        But when there is no input it brings up an exception form when I debug.
        So I tryed this try It keeps the program from crashing but also,
        gives no output when there is an input 
        REM Try
        Dim sum As Double

        sum = CDbl(TxtListNumBooks.Text) * 0.1 * CDbl(TxtListNumDaysLate.Text)
        REM Catch ex As Exception
        REM Dim sum As DialogResult

        REM sum = 0.0


        LblShowFines.Text = "Your total fine is:   " & CStr(FormatCurrency(sum))
        LblShowFines.BorderStyle = BorderStyle.FixedSingle

        REM End Try

    End Sub

    Private Sub TxtListNumBooks_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TxtListNumBooks.TextChanged
        TxtListNumBooks.TextAlign = HorizontalAlignment.Center

    End Sub
 
I am not sure I am going about this calculating proccess in the right way. So if someone knows a better method please inform me. In another words I could be possibly trying to use the wrong code all together.
 
Ok I learned how to create a message box
Code:
MsgBox("Warning if your debt is not payed promptly, Then it will be turned over to the StrongArmCollectionAgency.", , "Consolation")

But here is my two troubles.
  1. If there is no input in my text boxes the program pops up an
    error dialog.
  2. I only want the message box to popup if the amount of the fine = 25 dollars or more.
    [/list=1]
    Here is my code.
    Code:
    Private Sub BtnCalulateFines_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnCalulateFines.Click
            Clayton I am have much problems with this code.
            When I use this code it works fine as long as there is an input.
            But when there is no input it brings up an exception form when I debug.
            So I tryed this try It keeps the program from crashing but also,
            gives no output when there is an input 
            REM Try
            Dim sum As Double
    
            sum = CDbl(TxtListNumBooks.Text) * 0.1 * CDbl(TxtListNumDaysLate.Text)
    
            REM Catch ex As Exception
            REM Dim sum As DialogResult
    
            REM sum = 0.0
    
            LblShowFines.Text = "Your total fine is:   " & CStr(FormatCurrency(sum))
            LblShowFines.BorderStyle = BorderStyle.FixedSingle
            MsgBox("Warning if your debt is not payed promptly, Then it will be turned over to the StrongArmCollectionAgency.", , "Consolation")
    
            REM End Try
    
        End Sub
 
Last edited by a moderator:
Look again at the code I gave you above. :) You could also consider using Double.Parse() instead of CDbl().

As for displaying an error when the fines are $25 or more, use an if statement.

Now that I keep reading this, it seems more and more like a class assignment (is it?). In this case, I dont want to help too much, as giving you the right answers would defeat the purpose of learning. :)
 
The problem is solved.
Here is the solution.
Code:
    Private Sub BtnCalulateFines_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnCalulateFines.Click
        REM this button is used to do all the calculations 
        Dim sum As Double
        If TxtListNumBooks.Text = "" Then
            TxtListNumBooks.Text = "0"
        End If
        If TxtListNumDaysLate.Text = "" Then
            TxtListNumDaysLate.Text = "0"
        End If
        sum = CDbl(TxtListNumBooks.Text) * 0.1 * CDbl(TxtListNumDaysLate.Text)
        LblShowFines.Text = "Your total fine is:   " & CStr(FormatCurrency(sum))
        LblShowFines.BorderStyle = BorderStyle.FixedSingle
        This is the code used to make a popup message appear when the fine is more than $1.50
        If (sum > "1.5") Then

            MsgBox("Warning if your debt is not payed promptly, Then it will be turned over to the StrongArmCollectionAgency.", , "Consolation")
        End If
        If (sum < "1.49") Then


            LlbThankYou.Text = "Thanks for your prompt payment"

        End If
    End Sub
I think Mutant was telling me basically what to do but I could quite grasp it at the time. And you too are right wyrd, I need to do some further reading. And that thing about catching an exception is very interesting, although it wasnt quite what I was looking for. But it may help me with something later on.
Also as I said I try very hard to come up with the solution before I ask for help here.
But if I can not get help when I need it, then I will become discusted and that is not a good thing.
Thanks again for your input.
 
Actually, the error checking I gave you is needed. Try entering a non-numeric value (ie; letters) into your TxtListNumBooks and see what happens.
 
Ok I will try it later, but first I got to reveiw for a test on chapter 3.
Which I am hopeing to ace. Then I have to read on in the book an try to comprehend the idea behind the code you are giveing me.
I have seen an example similar to what you give me.
 
Ah.. well if you havent gotten to error checking yet then dont worry, youll get there eventually.
 
Back
Top