2014-04-03 26 views
0

將測試分數存儲並寫入與其他用戶的分數並存儲在變量中的文本文件。如何在關閉程序後保持價值? VB.net

該計劃內是一個評分表格,顯示該進度表格中該用戶所有測試的分數。它在我完成測試後正常工作,但是當我註銷並關閉程序時,它會在標籤中返回到0。

但是,當我關閉程序然後重新運行該程序時,值會更改回0,而不是寫入文本文件的實際測試分數。

我該怎麼做,以便在我爲該用戶運行程序時保持相同的值?

末子模塊創建存儲公共變量Public Topic1Score As Integer

這是從測試本身的代碼提取

If answers(i) = questions(i, 4) And FileOpenStatusTS = False Then 
    Topic1Score += 1 
    TotalScore += 1 
End If 
Next 

If yearst = "12" And classst = "A" Then 
    FileOpen(1, FileName12A1, OpenMode.Append) 
    FileOpenStatus12A1 = True` 

一旦所有的細節都被輸入並檢查,然後將它們寫入教師帳戶文本文件

WriteLine(1, Username, Topic1Score, TotalScore) 
FileClose(1) 
End If 

這裏是讀取進度表中的文件。

Private Sub StProgress_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 
    Debug.Assert(Not String.IsNullOrWhiteSpace(Topic1Score)) 
    lblTotalScore.Text = TotalScore 

    LblTopic2Score.Text = "You scored " & Topic2Score & " out of 5" 

    lblStName.Text = namest 
    LblStSurname.Text = surnamest 


    If yearst = "12" And classst = "A" Then 
     Dim Filefound As Boolean 
     Filefound = False 
     FileOpen(1, FileName12A1, OpenMode.Input) 
     While Not EOF(1) And Filefound = False 


      Input(1, Username) 'All the details are read from that account from the 12A1 text file' 
      Input(1, Topic1Score) 
      Input(1, TotalScore) 
      Filefound = True 
      lblTopic1Score.Text = "You scored " & Topic1Score & " out of 5" 
     End While 
    End If 

    If yearst = "13" And classst = "A" Then 
     Dim Filefound As Boolean 
     Filefound = False 
     FileOpen(1, FileName13A1, OpenMode.Input) 
     While Not EOF(1) And Filefound = False 


      Input(1, Username) 'All the details are read from that account from the 12A1 text file' 
      Input(1, Topic1Score) 
      Input(1, TotalScore) 
      Filefound = True 
      lblTopic1Score.Text = "You scored " & Topic1Score & " out of 5" 
     End While 
    End If 
    If yearst = "12" And classst = "B" Then 
     Dim Filefound As Boolean 
     Filefound = False 
     FileOpen(1, FileName12B1, OpenMode.Input) 
     While Not EOF(1) And Filefound = False 
      Input(1, Username) 'All the details are read from that account from the 12A1 text file' 
      Input(1, Topic1Score) 
      Input(1, TotalScore) 
      Filefound = True 
      lblTopic1Score.Text = "You scored " & Topic1Score & " out of 5" 
     End While 
    End If 
    If yearst = "13" And classst = "B" Then 
     Dim Filefound As Boolean 
     Filefound = False 
     FileOpen(1, FileName13B1, OpenMode.Input) 
     While Not EOF(1) And Filefound = False 


      Input(1, Username) 'All the details are read from that account from the 12A1 text file' 
      Input(1, Topic1Score) 
      Input(1, TotalScore) 
      Filefound = True 
      lblTopic1Score.Text = "You scored " & Topic1Score & " out of 5" 
     End While 
    End If 
End Sub` 

最新代碼...測試內 代碼... `FileOpenStatusT =假 昏暗的FileFound由於布爾 的FileFound =假 的FileOpen(1,Filenamet,OpenMode.Input) 儘管不EOF(1 )和的FileFound =假 輸入(1名) 輸入(1 '所有的細節都從TeacherAccounts文本文件閱讀該帳戶',密碼) 輸入(1,namet) 輸入(1,surnamet)

 If Username = TxtUsername.Text And Password = TxtPassword.Text Then 'If the username and account entered are valid then the user is navigated to the TeacherMenu form' 
      Filefound = True 
      t = Username 
     End If 
    End While 

    If Filefound = False Then 
     MsgBox("Username and Password were not a match,please try again") 
    Else 
     TeacherMenu.Show() 
     Me.Hide() 
    End If` 

進展形式碼... 如果yearst = 「12」 和classst = 「A」 然後 昏暗的FileFound由於布爾 的FileFound =假 的FileOpen(1,FileName12A1,OpenMode.Input) 儘管不EOF(1 )和Filefound = False

  Input(1, Username) 'All the details are read from that account from the 12A1 text file' 
      Input(1, Topic1Score) 
      Input(1, TotalScore) 
      Filefound = True 
      If Username = t Then 
       lblTopic1Score.Text = "You scored " & Topic1Score & " out of 5" 
      End If 

t是一個字符串,並設置爲模塊中的公共變量。

+0

請向我們顯示您的代碼。沒有辦法猜測發生了什麼。 –

+0

我已重新編輯代碼 – user3167227

+0

內容閱讀的代碼是相同的 - 我建議您更改「case」或「if」中的文件名,但使用相同的閱讀代碼。 – evenro

回答

4

有你3種不同的選擇:

  1. 使用的文件系統,併爲它創建一個文件(需要一些工作)。
    如果我理解正確 - 這就是你所做的,並且你有一個錯誤...獲得調試幫助 - 發佈代碼。
    我認爲你的代碼中的問題是你打開附加模式的文件。
    你沒有發佈文件的閱讀內容,但我想你會在那裏讀到第一個分數,但是不要每次都寫下 - 你在文件末尾寫下。
    所以你的文件可能看起來像這樣:
    ,你總是讀0 ...
    如果這不是問題 - 發佈讀得。

  2. 使用註冊表來保存變量(需要較少的工作)。

  3. 使用用戶區域app.config來存儲這些數據。
    這個區域與應用程序區域相矛盾,可以由應用程序修改,並且會分別保留給每個用戶。

我覺得第三個是最簡單和最快的。
好運


所以我覺得問題是,在該文件中讀取你不「縮小」相關的用戶。

在此代碼:

Dim Filefound As Boolean 
    Filefound = False 
    FileOpen(1, FileName12B1, OpenMode.Input) 
    While Not EOF(1) And Filefound = False 
     Input(1, Username) 'All the details are read from that account from the 12A1 text file' 
     Input(1, Topic1Score) 
     Input(1, TotalScore) 
     Filefound = True 
-------IF THE Username == t here... - the currently logged in user ... run the following line 
     lblTopic1Score.Text = "You scored " & Topic1Score & " out of 5" 
-------IF the Username == the currently logged in user - break the loop 
------- UNLESS - the last student record is necessary, since the grades were written in append mode 

    End While 

這應該解決的問題。

它是一個練習系統還是一個「真實」系統?
我在問,因爲從最終客戶端訪問保存用戶數據的文件是不安全的......因爲練習它是可以的,但對於真正的系統,您需要更改架構。

+0

發表涉及變量Topic1Score – user3167227

+0

的代碼添加了閱讀部分。 – user3167227

+0

你在每個文件中只有一個用戶嗎? – evenro

相關問題