将文本加载到列表框中

时间:2016-02-05 17:55:05

标签: vb.net

我需要实现的主要目标是在填写完所有框后,将用户输入的详细信息(例如使用的分钟数和总费用等)显示给用户。然后,他们可以将该框/列表框中的文本保存到PC上的txt文件中。

这几乎是我需要做的。我似乎无法实现这一点,也不知道我该怎么做。这是我到目前为止的代码,它应该如何工作。

Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    End Sub

    Private Sub btn1TotalCost_Click(sender As Object, e As EventArgs) Handles btn1TotalCost.Click

        'Declaring the variables.
        Dim MinutesAllowed As Integer
        Dim MinutesUsed As Integer
        Dim TextsAllowed As Integer
        Dim TextsUsed As Integer
        Dim DataAllowed As Integer
        Dim DataUsed As Integer
        Dim MinutesTotalCost As Double
        Dim TextsTotalCost As Double
        Dim DataTotalCost As Double
        Dim MonthlyCost As Double
        Dim TotalCost As Double

        MinutesAllowed = Val(txtBoxMinutesAllowed.Text)
        MinutesUsed = Val(txtBoxMinutesUsed.Text)
        TextsAllowed = Val(txtBoxTextsAllowed.Text)
        TextsUsed = Val(txtBoxTextsUsed.Text)
        DataAllowed = Val(txtBoxDataAllowed.Text)
        DataUsed = Val(txtBoxDataUsed.Text)
        MinutesTotalCost = Val(txtBoxTotalCost.Text)
        TextsTotalCost = Val(txtBoxTotalCost.Text)
        DataTotalCost = Val(txtBoxTotalCost.Text)
        TotalCost = Val(txtBoxTotalCost.Text)

        'Calculation For Minutes
        MinutesAllowed = Val(txtBoxMinutesAllowed.Text)
        MinutesUsed = Val(txtBoxMinutesUsed.Text)
        MonthlyCost = Val(txtBoxMonthlyCost.Text)
        'If the minutes allowed is greater than or equal to the minutes used, no extra charge is added.
        If MinutesAllowed >= MinutesUsed Then
            MinutesTotalCost = 0
        Else 'If the minutes used is greater than the minutes allowed, each minute is multiplied by 00.30p.
            MinutesTotalCost = (MinutesUsed - MinutesAllowed) * 0.3
        End If
        txtBoxTotalCost.Text = CType(MinutesTotalCost + MonthlyCost, String)



        'Calculation For Texts
        TextsAllowed = Val(txtBoxTextsAllowed.Text)
        TextsUsed = Val(txtBoxTextsUsed.Text)
        MonthlyCost = Val(txtBoxMonthlyCost.Text)
        'If the texts allowed is greater than or equal to the texts used, no extra charge is added.
        If TextsAllowed >= TextsUsed Then
            TextsTotalCost = 0
        Else 'If the texts used is greater than the texts allowed, each text is multiplied by 00.15p.
            TextsTotalCost = (TextsUsed - TextsAllowed) * 0.15
        End If
        txtBoxTotalCost.Text = CType(TextsTotalCost + MonthlyCost, String)



        'Calculation For Data
        DataAllowed = Val(txtBoxDataAllowed.Text)
        DataUsed = Val(txtBoxDataUsed.Text)
        MonthlyCost = Val(txtBoxMonthlyCost.Text)
        'If the data allowed is greater than or equal to the data used, no extra charge is added.
        If DataAllowed >= DataUsed Then
            DataTotalCost = 0
        Else 'If the data used is greater than the data allowed, 10.00 is added to the total cost.
            DataTotalCost = 10
        End If
        txtBoxTotalCost.Text = CType(DataTotalCost + MonthlyCost + TextsTotalCost + MinutesTotalCost, Double)
    End Sub
End Class

1 个答案:

答案 0 :(得分:1)

行。这段代码是残酷的诚实并不好。这些行

Dim FullName As String
Dim PhoneNumber As Decimal
Dim AccNumber As Decimal
Dim MinutesAllowed As Integer
Dim TextsAllowed As Integer
Dim DataAllowed As Integer
Dim MinutesUsed As Integer
Dim TextsUsed As Integer
Dim DataUsed As Integer
Dim MonthlyCost As Double
Dim TotalCost As Double

声明特定类型的变量 - 字符串,小数和整数等。这意味着他们只能接受相同类型的值。在代码中使用 Option Strict On 可以帮助您更正这些错误,因为它会显示您的代码出错。这些行应该是:

FullName = ""
PhoneNumber = 0D
AccNumber = 0D
MinutesAllowed = 0
TextsAllowed = 0
DataAllowed = 0
MinutesUsed = 0
TextsUsed = 0
DataUsed = 0
MonthlyCost = 0R
TotalCost = 0R

您遇到问题的原因是您无法将空字符串分配给整数或任何其他数字。启用选项严格后,系统会显示这些错误。或许你会更好地将phoneNumber声明为字符串以允许破折号和括号等。另外如果TotalCost是双倍,那么MonthlyCost也应该是双倍

数字后面的字母表示被指定号码的类型。 D 表示它是小数0而 R 表示它是双0.但是在代码中,当声明变量时,它们是已经分配了默认值,因此您并不需要分配值的行

继续......

listBox.Items.Add("Full Name: " + txtBoxFullName.Text)
listBox.Items.Add("Phone Number: " + txtBoxPhoneNumber.Text)

等等

上面的行与上面的变量声明完全无关。这是故意的吗?例如,txtBoxFullName.Text根本不是与FullName相同的变量,除非你有其他代码,否则我看不到上述变量的需要。我猜你发布的代码不完整。当您发布问题时,您确实需要发布所有相关代码。看看这里.. https://stackoverflow.com/help/mcve

听听Plutonix - 他很聪明。