Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27085

Format String Textbox

$
0
0
Basically my code is good but using P0 it multiplies the value inserted in the textbox by 100, since it user input I need it to display exactly what they type just a percentage. Also the value come back to the database without the percentage sign.

Private Sub txtTotalSpotStan_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtTotalSpotStan.Validating
Try
'if theres nothing entered default it to 0
If txtTotalSpotStan.Text.Length = 0 Then txtTotalSpotStan.Text = "0%"
'convert the text to a value
Dim dblAmt As Double = Decimal.Parse(txtTotalSpotStan.Text, Globalization.NumberStyles.Currency)
'convert it for display
txtTotalSpotStan.Text = dblAmt.ToString("P0")
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.OkOnly + MsgBoxStyle.Exclamation, "Invalid Amount")
End Try

End Sub
Private Sub txtTotalSpotStan_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtTotalSpotStan.KeyPress
'keep the user from typing any characters except for backspace, comma, and minus
If Not Char.IsDigit(e.KeyChar) And e.KeyChar <> Chr(8) And e.KeyChar <> Chr(46) And e.KeyChar <> Chr(45) Then e.Handled = True
End Sub

Viewing all articles
Browse latest Browse all 27085

Trending Articles