1

さて、インターンシップ プロジェクトのために、ストリームライターとストリームリーダーと一緒にジャーナルを作成しています。

名前、ユーザー名、およびパスワードを使用してアカウントを作成できる場所に移動する必要があります。アカウントを作成すると、その人の名前でtxtファイルが作成される場所にもあります。今、彼らはログインし、ジャーナルページに移動します. ほとんどのジャーナル ページには、ジャーナル エントリの日付、ジャーナルのタイトル、およびジャーナル エントリのテキスト自体があります。

私が抱えている問題は、日誌エントリを作成/編集するボタンをクリックすると、その日誌が存在するかどうかを確認するサブルーチンを通過することです (つまり、その日付に既に 1 つ存在することを意味します)。存在しない場合は、テキスト ファイルの末尾に新しいファイルを作成する必要があります。存在する場合は、そのジャーナルがテキスト ファイルに配置されている行を編集する必要があります。

コード:

Private Sub CreateBtn_Click(sender As System.Object, e As System.EventArgs) Handles CreateBtn.Click

    Errors = ""

    Dim TempCounter As Integer = 0

    If TitleTxt.Text = "" Then

        Errors = "You must enter a title." & vbCrLf

    End If


    If JournalTextRtxt.Text = "" Then

        Errors &= "You must enter an entry for the journal."

    End If

    If Errors <> "" Then

        MessageBox.Show("There's an error in creating/editing your journal." & vbCrLf & "Error(s):" & vbCrLf & Errors, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)

    Else

        JournalDate = DateTimePicker1.Value
        JournalTitle = TitleTxt.Text
        JournalText = JournalTextRtxt.Text

        arrJournalEntries(TempCounter).TheDate = JournalDate
        arrJournalEntries(TempCounter).Title = JournalTitle
        arrJournalEntries(TempCounter).JournalEntry = JournalText



        CheckAndWrite()

    End If

End Sub

Private Sub CheckAndWrite()

    Dim Reader As New StreamReader(MyName & ".txt", False)
    Dim Sline As String = Reader.ReadLine

    Counter = 0

    Do Until (Sline Is Nothing) 'Perform the code until the line in the text file is blank

        If Not Sline Is Nothing Then 'If the line in the text file is NOT blank then

            For i As Integer = 1 To 3

                Select Case i

                    Case 1

                        arrJournalEntries(Counter).TheDate = Sline
                        Sline = Reader.ReadLine


                    Case 2

                        arrJournalEntries(Counter).Title = Sline
                        Sline = Reader.ReadLine

                    Case 3

                        arrJournalEntries(Counter).JournalEntry = Sline
                        Sline = Reader.ReadLine

                End Select

            Next
        End If

        JournalDate = arrJournalEntries(Counter).TheDate

        Time = DateTimePicker1.Value

        MsgBox("Journal Date = " & JournalDate & vbCrLf & "Today's Date = " & Time)


        If Time = JournalDate Then

            JournalFound = True

        Else

            Counter += 1

            JournalFound = False

        End If

    Loop


    Reader.Close()

    Try

        If Sline Is Nothing Or JournalFound = False Then

            MsgBox("Your journal is now going to be created.")

            JournalDate = DateTimePicker1.Value
            JournalTitle = TitleTxt.Text
            JournalText = JournalTextRtxt.Text

            arrJournalEntries(Counter).TheDate = JournalDate
            arrJournalEntries(Counter).Title = JournalTitle
            arrJournalEntries(Counter).JournalEntry = JournalText

            Dim Writer As New StreamWriter(MyName & ".txt", True)

            Do Until (arrJournalEntries(Counter).TheDate = Nothing)

                Writer.WriteLine(arrJournalEntries(Counter).TheDate)
                Writer.WriteLine(arrJournalEntries(Counter).Title)
                Writer.WriteLine(arrJournalEntries(Counter).JournalEntry)


                Counter += 1

            Loop

            Writer.Close()


        End If

        If JournalFound = True Then

            MsgBox("Your journal is now going to be edited.")

            JournalDate = DateTimePicker1.Value
            JournalTitle = TitleTxt.Text
            JournalText = JournalTextRtxt.Text


            arrJournalEntries(Counter).TheDate = JournalDate
            arrJournalEntries(Counter).Title = JournalTitle
            arrJournalEntries(Counter).JournalEntry = JournalText


            Dim Writer As New StreamWriter(MyName & ".txt", True)

            Do Until (arrJournalEntries(Counter).TheDate = Nothing)

                Writer.WriteLine(arrJournalEntries(Counter).TheDate)
                Writer.WriteLine(arrJournalEntries(Counter).Title)
                Writer.WriteLine(arrJournalEntries(Counter).JournalEntry)


                Counter += 1

            Loop

            Writer.Close()


        End If

    Catch ex As Exception

        MessageBox.Show("An error has occured" & vbCrLf & vbCrLf & "Original Error:" & vbCrLf & ex.ToString)

    End Try



End Sub`

発生している問題は、最初の書き込みが間違っているだけではないということです。編集すると言っているはずなのに、そうではなく、ただ作成していると言っているだけです。しかし、それはファイルに追加するだけです。現在の日付でボタンを3回押した後。タイトルは「テスト タイトル」、日誌エントリ テキストは「テスト テキスト」です。これが起こったことです。

仕訳が正しくありません

それはちょうどあるはずです

2012 年 7 月 10 日 15:52:08

テストタイトル

テストテキスト

2012 年 7 月 10 日 15:52:08

テストタイトル

テストテキスト

ずっと。もちろん、同じ日付の場合は上書きされます。誰でも私を助けてもらえますか?

4

1 に答える 1

1

配列を日付でフィルタリングしているだけなので、日付はあるがタイトルやテキストがないオブジェクトがあるように見えます。

Do Until (arrJournalEntries(Counter).TheDate = Nothing)

「クイック」修正:

Do Until (arrJournalEntries(Counter).TheDate = Nothing)
  If arrJournalEntries(Counter).Title <> String.Empty Then
    Writer.WriteLine(arrJournalEntries(Counter).TheDate)
    Writer.WriteLine(arrJournalEntries(Counter).Title)
    Writer.WriteLine(arrJournalEntries(Counter).JournalEntry)
  End If
  Counter += 1
Loop

配列を取り除き、List(of JournalEntry)代わりに a を使用することを検討してください。あなたのコードは、現在の状態では維持するのが難しいようです。

于 2012-07-10T20:37:34.793 に答える