0

对不起“愚蠢”的问题。我无法从列表框中的 txtbox 中获取值。现在它在列表框中给出“07650”,而它应该是 76,50...

我有一个名为 formatdecimal 的函数。如果我保存(保存按钮)文本文件(到 .txt),则该值必须保存为“07650”,否则它必须在列表框中像“76,50”一样给出......

有人能帮我吗?

` 公共函数 FormatDecimal(ByVal perc As Decimal) As String

    Return (perc * 100).ToString("00000")

End Function` (Source code for the function)

以下是添加按钮的源代码...

    Private Sub btnToevoegen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnToevoegen.Click (add button)
    Dim line As String = ""
    Dim number As Integer
    Dim name As String = ""
    Dim Birthday As Integer
    Dim prompt As String
    Dim title As String = "FAILURE"
    Dim perc As Decimal


    If Not CheckLeegtextvak() Then
        MessageBox.Show("Give a value in the textbox", "FAILURE", MessageBoxButtons.OK) 
        Return
    End If (check if the textboxes are empty)

    'If defaultvalues are okay then do something.
    If Not checkdefaultwaarde() Then
        prompt = "Select a value in the combobox."
        MessageBox.Show(prompt, title, MessageBoxButtons.OK)
        Return
    End If (check if defaultvalues are okay (beginning of the file)

    CheckLengteNaam() (check the length of the name)

    number = CInt(txtNumber.Text)
    name = CStr(txtName.Text)
    Birthday = CInt(txtBirthday.Text)
    perc = CDec(txtPercentage.Text)


    line = combobox1.SelectedItem.ToString.PadRight(1) & number.ToString.PadRight(5) & name.ToString.PadLeft(5) & birthday.ToString.PadRight(5) & perc.ToString.PadLeft(5)

lstOutput.Items.Add(行)

    FormatDecimal(perc)

End Sub

当我添加记录时,perc 的值必须是 76,50。当我将表单保存到 txtfile 时,该值必须是 07650(这就是函数的用途),但它没有保存正确的值。它节省了“76,50”。

保存并另存为代码...

Private Sub mnuBestandOpslaan_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuBestandOpslaan.Click (This is the save button)
    Dim index As Integer
    Dim perc As Decimal

    'Zoeken naar opgegeven map om bestand in op te slaan onder dezelfde naam.
    If dlgOpen.FileName = "" Then
        mnuSavefileAs_Click(sender, e)
    Else
        FileOpen(1, dlgOpen.FileName, OpenMode.Output)
        For index = 0 To lstOutput.Items.Count - 1
            PrintLine(1, lstOutput.Items(index))
        Next
        FileClose(1)
    End If

    FormatDecimal(perc)
End Sub

Private Sub mnuBestandOpslaanAls_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuBestandOpslaanAls.Click (This is the save as button) 
    Dim index As Integer
    Dim perc As Decimal

    'Search folder to save file.
    With dlgSave
        .InitialDirectory = "C:\My Documents"
        .Filter = "tekstbestanden (*.txt) | *.txt"
        .Title = "Save as"
    End With

    'Save file.
    If dlgSave.ShowDialog = DialogResult.OK Then
        FileOpen(1, dlgSave.FileName, OpenMode.Output)
        For index = 0 To lstOutput.Items.Count - 1
            PrintLine(1, lstOutput.Items(index))
        Next
        FileClose(1)
    End If


    FormatDecimal(perc)
End Sub

您可以作为数字给出的最大值是 100,如 100%...

4

1 回答 1

0

嗯...callingFormatDecimal(perc)本身并没有做任何有用的事情。我不更改任何变量,也不以任何方式控制格式。

您在 ListBox 中存储了一个 STRING,当您想将其输出到文件时,很难更改其中任何内容的格式。更好的方法是创建一个 CLASS 来保存这些值并覆盖它的 ToString() 函数,以便它在 ListBox 中正确显示。现在您可以将该类的实例添加到您的 ListBox。然后,当您进行打印时,您可以根据需要格式化类实例中的每个单独值。

编辑:这是我的意思的一个例子......

首先,在您的项目中添加一个类:

Public Class MyData

    Public number As Integer
    Public name As String
    Public Birthday As Integer
    Public perc As Decimal
    Public comboBoxValue As String

    ' This will be used by the ListBox:
    Public Overrides Function ToString() As String
        Return comboBoxValue & number.ToString.PadRight(5) & name.PadLeft(5) & Birthday.ToString.PadRight(5) & perc.ToString.PadLeft(5)
    End Function

    Public Function ToStringForPrinter() As String
        Return comboBoxValue & number.ToString.PadRight(5) & name.PadLeft(5) & Birthday.ToString.PadRight(5) & (perc * 100).ToString("00000")
    End Function

End Class

我不知道 comboBox1 代表什么,所以你必须相应地重命名它并更新下面访问它的代码。

接下来,在将项目添加到列表框中时,您将更改此内容:

    number = CInt(txtNumber.Text)
    Name = CStr(txtName.Text)
    Birthday = CInt(txtBirthday.Text)
    perc = CDec(txtPercentage.Text)
    line = combobox1.SelectedItem.ToString.PadRight(1) & number.ToString.PadRight(5) & Name.ToString.PadLeft(5) & birthday.ToString.PadRight(5) & perc.ToString.PadLeft(5)
    lstOutput.Items.Add(line)

到更像这样的东西,它创建一个 MyData 实例并将其添加到 ListBox (而不是像以前那样的字符串):

    Dim data As New MyData
    data.comboBoxValue = combobox1.SelectedItem.ToString.PadRight(1)
    data.number = CInt(txtNumber.Text)
    data.name = txtName.Text
    data.Birthday = CInt(txtBirthday.Text)
    data.perc = CDec(txtPercentage.Text)
    lstOutput.Items.Add(data)

最后,在打印时,你会改变这个:

    FileOpen(1, dlgOpen.FileName, OpenMode.Output)
    For index = 0 To lstOutput.Items.Count - 1
        PrintLine(1, lstOutput.Items(index))
    Next
    FileClose(1)

更像是:

    FileOpen(1, dlgOpen.FileName, OpenMode.Output)
    For Each data As MyData In lstOutput.Items
        PrintLine(1, data.ToStringForPrinter)
    Next
    FileClose(1)
于 2013-06-08T14:35:04.540 回答