Code the
keypress
event of the textboxes. Allow numbers, the backspace key,
and the decimal point. Only write the code once and use Handles.
Answer
Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress

End Sub

Recommended Answers

All 11 Replies

It sound and taste like homework:->....so us what you already done so far;-> and then little by little we can both fix it:-)..

e.KeyChar contains the key that was pressed. Set e.Handled = true if KeyChar is not a numeric character or a period, that will make the system throw the character away. Hint: see IsNumeric() function.

I am done with all except that one i need to finish
Private Sub btnCalc_Click(sender As Object, e As EventArgs) Handles btnCalc.Click

    Dim decBalance As Decimal
    Dim decDeposits As Decimal
    Dim decWithdrawls As Decimal
    Dim blnConverted As Boolean
    Dim decearned As Decimal
    Dim decending As Decimal
    Dim decSubTotal As Decimal

    blnConverted = Decimal.TryParse(TextBox1.Text, decBalance)
    If blnConverted = False Then
        MessageBox.Show("Error")
        TextBox1.Focus()
        Exit Sub
    End If

    blnConverted = Decimal.TryParse(TextBox2.Text, decDeposits)
    If blnConverted = False Then
        MessageBox.Show("Error")
        TextBox2.Focus()
        Exit Sub
    End If

    blnConverted = Decimal.TryParse(TextBox3.Text, decWithdrawls)
    If blnConverted = False Then
        MessageBox.Show("Error")
        TextBox3.Focus()
        Exit Sub
    End If

    decSubTotal = (decBalance + decDeposits - decWithdrawls)
    If decSubTotal > 5000 Then
        decearned = 0.02
    End If

    decending = decSubTotal + (decSubTotal * decearned)
    decearned = decSubTotal * decearned

    lblearned.Text = decearned.ToString("C")
    lblEnding.Text = decending.ToString("C")
End Sub

Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress

End Sub

Private Sub TextBox1_Enter(sender As Object, e As EventArgs) Handles TextBox1.Enter
    TextBox1.SelectAll()
End Sub

Private Sub Textbox2_Enter(sender As Object, e As EventArgs) Handles TextBox2.Enter
    TextBox2.SelectAll()
End Sub

Private Sub Textbox3_Enter(sender As Object, e As EventArgs) Handles TextBox3.Enter
    TextBox3.SelectAll()
End Sub

Private Sub textbox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged, TextBox2.TextChanged, TextBox3.TextChanged
    lblearned.Text = ""
    lblEnding.Text = ""
End Sub

End Class

Which one have you not finished? The keypress event handler? I already told you what you need to do.

yes that one but i am a little confused how to do it could you help me?

Private Sub TextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox1.KeyPress, TextBox2.KeyPress, TextBox3.KeyPress

    If (e.KeyChar < "0" OrElse e.KeyChar > "9") AndAlso e.KeyChar <> ControlChars.Back Then
        e.Handled = True

You got it :)

hmmmm when I test it i got wrong :(

        If (e.KeyChar < "0" Or e.KeyChar > "9") AndAlso e.KeyChar <> ControlChars.Back Then
            e.Handled = True
        End If

Worked ok for me. It only allows numeric digits and the backspace key.

Forgot the decimal point -- you need to add that condition too.

what condition?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.