为什么我的函数写入文件时与组合框组合时不写入文件

时间:2014-04-29 22:10:29

标签: vb.net combobox

我已将其缩小到我相信这是我写入文件功能。当我运行程序并添加一个实例时,它甚至没有写任何东西到指定的文件。

我想弄清楚如何从组合框中选择一个项目,然后在它旁边放置以下文本框,将其信息添加到文件中的正确位置。

这是我到目前为止的代码:

Option Strict On
Public Class frmSemseter_Project_ll
    Dim ComicArray() As String = IO.File.ReadAllLines("Comics.txt")

    Private Sub frmSemseter_Project_ll_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        dgvComics.DataSource = ComicArray.ToList
        DisplayComicData(dgvComics, "Comics.txt")
    End Sub

    Private Sub mnuFile_Exit_Click(sender As Object, e As EventArgs) Handles mnuFile_Exit.Click
        'Closes the program
        Me.Close()
    End Sub

    Private Sub DisplayComicData(ByRef dgv As DataGridView, filename As String)
        dgvComics.Refresh()
        If IO.File.Exists(filename) Then
            Dim query = From Line In IO.File.ReadAllLines(filename)
                        Let Title = Line.Split(","c)(0)
                        Let Issue = Line.Split(","c)(1)
                        Let Publisher = Line.Split(","c)(2)
                        Let CoverDate = Line.Split(","c)(3)
                        Let CoverPrice = FormatCurrency(Line.Split(","c)(4))
                        Let AddedDate = Line.Split(","c)(5)
                        Order By Title Ascending
                        Select Title, Issue, Publisher, CoverDate, CoverPrice, AddedDate
            dgvComics.DataSource = query.ToList()
        ElseIf Not IO.File.Exists("Comics.txt") Then
            MessageBox.Show("ERROR: Please Insert the Comics File.", "No File",
                                                          MessageBoxButtons.OK, MessageBoxIcon.Error)
        End If
    End Sub

    Private Sub mnuSort_Ascending_Title_Click(sender As Object, e As EventArgs) Handles mnuSort_Ascending_Title.Click
        Dim query = From Line In ComicArray
                    Let Title = Line.Split(","c)(0)
                    Let Issue = Line.Split(","c)(1)
                    Let Publisher = Line.Split(","c)(2)
                    Let CoverDate = Line.Split(","c)(3)
                    Let CoverPrice = FormatCurrency(Line.Split(","c)(4))
                    Let AddedDate = Line.Split(","c)(5)
                    Order By Title Ascending
                    Select Title, Issue, Publisher, CoverDate, CoverPrice, AddedDate
        dgvComics.DataSource = query.ToList()
    End Sub

    Private Sub mnuSort_Desending_Title_Click(sender As Object, e As EventArgs) Handles mnuSort_Desending_Title.Click
        Dim query = From Line In ComicArray
                    Let Title = Line.Split(","c)(0)
                    Let Issue = Line.Split(","c)(1)
                    Let Publisher = Line.Split(","c)(2)
                    Let CoverDate = Line.Split(","c)(3)
                    Let CoverPrice = FormatCurrency(Line.Split(","c)(4))
                    Let AddedDate = Line.Split(","c)(5)
                    Order By Title Descending
                    Select Title, Issue, Publisher, CoverDate, CoverPrice, AddedDate
        dgvComics.DataSource = query.ToList()
    End Sub

    Private Sub mnuSort_Ascending_Issue_Click(sender As Object, e As EventArgs) Handles mnuSort_Ascending_Issue.Click
        Dim query = From Line In ComicArray
            Let Title = Line.Split(","c)(0)
            Let Issue = Line.Split(","c)(1)
            Let Publisher = Line.Split(","c)(2)
            Let CoverDate = Line.Split(","c)(3)
            Let CoverPrice = FormatCurrency(Line.Split(","c)(4))
            Let AddedDate = Line.Split(","c)(5)
            Order By Issue Ascending
            Select Title, Issue, Publisher, CoverDate, CoverPrice, AddedDate
        dgvComics.DataSource = query.ToList()
    End Sub

    Private Sub mnuSort_Desending_Issue_Click(sender As Object, e As EventArgs) Handles mnuSort_Desending_Issue.Click
        Dim query = From Line In ComicArray
            Let Title = Line.Split(","c)(0)
            Let Issue = Line.Split(","c)(1)
            Let Publisher = Line.Split(","c)(2)
            Let CoverDate = Line.Split(","c)(3)
            Let CoverPrice = FormatCurrency(Line.Split(","c)(4))
            Let AddedDate = Line.Split(","c)(5)
            Order By Issue Descending
            Select Title, Issue, Publisher, CoverDate, CoverPrice, AddedDate
        dgvComics.DataSource = query.ToList()
    End Sub

'Everything works fine until this point
'from here through the whole write to file function does not seem to be working 
'whenever i click the add button that i made it does not even add the values into the file

    Private Sub btnAddComic_Click(sender As Object, e As EventArgs) Handles btnAddComic.Click
        If cbComicAdder.SelectedText = "Title" Then
            WriteToFile("Comics.txt", CStr(txtComicAdder.Text), "Title")
        End If

        If cbComicAdder.SelectedText = "Issue" Then
            WriteToFile("Comics.txt", CStr(txtComicAdder.Text), "Issue")
        End If

        If cbComicAdder.SelectedText = "Publisher" Then
            WriteToFile("Comics.txt", CStr(txtComicAdder.Text), "Publisher")
        End If

        If cbComicAdder.SelectedText = "Cover Date" Then
            WriteToFile("Comics.txt", CStr(txtComicAdder.Text), "Cover Date")
        End If

        If cbComicAdder.SelectedText = "Cover Price" Then
            WriteToFile("Comics.txt", CStr(txtComicAdder.Text), "Cover Price")
        End If

        If cbComicAdder.SelectedText = "Added Date" Then
            WriteToFile("Comics.txt", CStr(txtComicAdder.Text), "Added Date")
        End If
    End Sub

    Private Sub WriteToFile(ByVal filename As String, ByVal Item As String, ByVal Instance As String)
        Dim sw As IO.StreamWriter = IO.File.AppendText(filename)
        While CBool(Instance)
            If Instance = "Title" Then
                sw.Write(Item & ",")
            ElseIf Instance = "Issue" Then
                sw.Write(Item & ",")
            ElseIf Instance = "Publisher" Then
                sw.Write(Item & ",")
            ElseIf Instance = "Cover Date" Then
                sw.Write(Item & ",")
            ElseIf Instance = "Cover Price" Then
                sw.Write(Item & ",")
            ElseIf Instance = "Added Date" Then
                sw.Write(Item)
            End If
        End While
        sw.Close()
    End Sub

'how can i fix this function so that it will work?

    Private Sub btnRefreshGrid_Click(sender As Object, e As EventArgs) Handles btnRefreshGrid.Click
        dgvComics.ClearSelection()
        dgvComics.Refresh()
        dgvComics.Update()
    End Sub
End Class

1 个答案:

答案 0 :(得分:0)

示例类版本

Public Class Comic
    Public Property Title As String
    Public Property Issue As Integer

    ' etc

    Public Sub SaveData
        Using sw As New StreamWriter(yourFileNameHere)

            sr.WriteLine("{0}, {1}, {2}, ...{n}", 
                  Title, Issue, Publisher, IssueDate, SoForth, SoOn)

        End Using
    End Sub

字符串描述的格式或布局主要是,,然后是要写入的数据。

如果您的应用依赖于用户一次填写并保存一个字段,则可能会出现问题。在保存之前,课程可以检查是否所有信息都在那里以防止写入部分记录。