【发布时间】:2020-02-13 11:39:10
【问题描述】:
我正在尝试格式化两个特定的文本框以显示为 Sq。 ft. (txt.Squareft.Text) 和美国货币 (txtTotalprice.Text) 在 Visual Studio 2019 中作为 Windows 窗体应用程序并使用 Visual Basic 代码进行计算后。我正在使用 .NET framework v4.7.2 并使用 Windows 10。它现在的运行方式,显示在文本框中的数字只是没有添加 Sq 的数字。 ft. 最后没有货币格式。我还要补充一点,我对 VB 和一般编程非常陌生。有什么帮助或建议吗?
Option Explicit On
Option Infer Off
Public Class Form1
Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
'Variables
Dim decTotalprice As Decimal
Dim decLength As Decimal
Dim decWidth As Decimal
Dim decPrice As Decimal
Dim decSquareft As Decimal
Decimal.TryParse(txtLength.Text, decLength)
Decimal.TryParse(txtWidth.Text, decWidth)
Decimal.TryParse(txtPrice.Text, decPrice)
Decimal.TryParse(txtSquareft.Text, decSquareft)
txtTotalprice.Text = decTotalprice.ToString("C2")
txtSquareft.Text = decSquareft.ToString("N2") & " Sq. ft."
' Calculate the square feet and total price
txtSquareft.Text = txtLength.Text * txtWidth.Text
txtTotalprice.Text = txtPrice.Text * txtSquareft.Text
End Sub
Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
' Clears all the text fields with button click
txtLength.Clear()
txtWidth.Clear()
txtPrice.Clear()
txtSquareft.Clear()
txtTotalprice.Clear()
End Sub
Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
' Exits the form
Me.Close()
End Sub
End Class
【问题讨论】:
-
您已经拥有
Option Explicit On。努力也设置Option Strict On。Option Infer你可能想保留它On。您会立即看到问题所在。顺便说一句,您应该使用TryParse来检查输入字符串是否可以转换为类型,所以If Decimal.TryParse() then (...) end if
标签: vb.net textbox currency windows-forms-designer currency-formatting