2016-02-04 85 views
1

我目前正在爲我的計算課程做一個項目,在那裏我讓用戶輸入鍛鍊名稱,對於每次鍛鍊,他們都可以進行一些練習,並且每次練習都可以跟蹤距離,時間,體重,套數或代表。Visual Basic數組和對象

Public Class Form1 
    Dim workoutValue(14) As String 
    Dim exerciseValue(14, 9) As String 
    Dim workoutCounter As Integer 
    Dim exerciseCounter As Integer 
    Dim progressValue(14, 9, 4) As String 
    Dim reps, sets, weight, distance, time As Integer 



    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load 
     For i = 1 To 15 
      workoutValue(i - 1) = CStr(i) 
      workoutcomboBox1.Items.Add(i) 
      workoutcomboBox2.Items.Add((workoutValue(i - 1))) 
      workoutcomboBox3.Items.Add((workoutValue(i - 1))) 
     Next 
     workoutcomboBox1.SelectedIndex = 0 
     workoutcomboBox2.SelectedIndex = 0 

     For i = 1 To 10 
      For x = 0 To 14 
       exerciseValue(x, i - 1) = CStr(i) 
      Next 
      exercisecombobox1.Items.Add(i) 
      exercisecomboBox2.Items.Add(i) 
     Next 
     exercisecombobox1.SelectedIndex = 0 
     workoutcomboBox3.SelectedIndex = 0 
     reps = 0 
     sets = 1 
     weight = 2 
     distance = 3 
     time = 4 

     For i = 0 To 14 
      For x = 0 To 9 
       For y = 0 To 4 
        progressValue(i, x, y) = "" 
       Next 
      Next 
     Next 
    End Sub 


    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles workoutdataButton.Click 
     workoutCounter = workoutcomboBox1.SelectedIndex 
     workoutValue(workoutCounter) = txtworkoutvalueInput.Text 
     workoutcomboBox2.Items.RemoveAt(workoutCounter) 
     workoutcomboBox2.Items.Insert(workoutCounter, workoutValue(workoutCounter)) 
     workoutcomboBox3.Items.RemoveAt(workoutCounter) 
     workoutcomboBox3.Items.Insert(workoutCounter, workoutValue(workoutCounter)) 

    End Sub 

    Private Sub exercisedataButton_Click(sender As Object, e As EventArgs) Handles exercisedataButton.Click 
     exerciseCounter = exercisecombobox1.SelectedIndex 
     workoutCounter = workoutcomboBox2.SelectedIndex 
     exerciseValue(workoutCounter, exerciseCounter) = txtexercisevalueInput.Text 

    End Sub 


    Private Sub workoutcomboBox3_SelectedIndexChanged(sender As Object, e As EventArgs) Handles workoutcomboBox3.SelectedIndexChanged 
     exercisecomboBox2.Items.Clear() 
     For i = 0 To 9 
      exercisecomboBox2.Items.Insert(i, exerciseValue(workoutcomboBox3.SelectedIndex, CStr(i))) 
     Next 
    End Sub 
    Private Sub progressButton_Click(sender As Object, e As EventArgs) Handles progressButton.Click 
     exerciseCounter = exercisecomboBox2.SelectedIndex 
     workoutCounter = workoutcomboBox3.SelectedIndex 
     If repscheckBox.Checked = True Then 
      progressValue(workoutCounter, exerciseCounter, reps) = repstxtBox.Text 
     End If 
     If setscheckBox.Checked = True Then 
      progressValue(workoutCounter, exerciseCounter, sets) = setstxtBox.Text 
     End If 
     If weightcheckBox.Checked = True Then 
      progressValue(workoutCounter, exerciseCounter, weight) = weighttxtBox.Text 
     End If 
     If distancecheckBox.Checked = True Then 
      progressValue(workoutCounter, exerciseCounter, distance) = distancetxtBox.Text 
     End If 
     If timecheckBox.Checked = True Then 
      progressValue(workoutCounter, exerciseCounter, time) = timetxtBox.Text 
End If 
     Label1.Text = progressValue(1, 1, 0) 
    End Sub 
End Class 

這是我到目前爲止做出(原諒我的語言可怕的使用和理解)。程序可以工作,但我希望用戶輸入的數據在關閉程序時保持不變!我考慮使用對象,因爲您可以將對象的數據保存到串行文件中。但是我找不到像我的數組一樣使用對象的方法。任何幫助或建議將不勝感激!

+0

VB6或vb.net選擇一。他們是不一樣的 –

+0

修好了,對不起! – DJHolmes97

+0

這不是VB6,但看起來VB.Net –

回答

0

您可能需要使用每個循環和陣列的每個值導出到文本文件中的行...

事情是這樣的:

Try 

Using sw As StreamWriter = File.AppendText("C:\MyFile.txt") 
For each value In myArray 
sw.WriteLine(value.tostring) 
Next 
    Catch ex As Exception 
MsgBox("Something went wrong:" & ex.tostring) 
     End Try 
+0

雖然這可能是我將不得不使用的,如果我可以解決這個問題,必須使用這個列表來完成這個工作,它可能會清理我的代碼。 – DJHolmes97