如何将列表框项目保存到记事本

时间:2013-06-08 12:34:38

标签: vb.net-2010

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

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

有人可以帮助我吗?

`Public Function 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%......

1 个答案:

答案 0 :(得分:0)

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

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

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

首先,在项目中添加一个Class:

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)