2014-01-14 157 views
0

自從我與VB混淆後,這已經快一年了,但我在第一次分配該學期時遇到了問題,該學期應該是一個複習。我應該做的是製作一個應用程序,我可以通過電子郵件輸入學生姓名,地址,GPA,年齡,以及哪個年級(新生,大二,其他)以及複選框。將陣列ID添加到標籤文本和列表框VB.NET

一旦填寫完成,我需要將這些信息預覽到label.text中。如果它看起來不錯,我需要將textbox.text信息連接到一個列表框中。無論我嘗試做什麼,或者在預覽中顯示String.Array(),或者第57行的程序崩潰,都將不勝感激。

Public Class Form1 
'CIS259 Spring 2014 Matthew McQuarrie 

'Declare Student Data as a String of 9 arrays 
Dim StudentData(8) As String 

Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click 
    'Close application 
    Me.Close() 
End Sub 

Private Sub btnReset_Click(sender As Object, e As EventArgs) Handles btnReset.Click 
    'Clear entire form 
    txtName.Clear() 
    txtAddress.Clear() 
    txtGPA.Clear() 
    txtAge.Clear() 
    radFreshman.Checked = False 
    radSophomore.Checked = False 
    radOther.Checked = False 
    chkCIS.Checked = False 
    chkMath.Checked = False 
    chkScience.Checked = False 
    chkHistory.Checked = False 
    lblPreview.Text = "" 
End Sub 
Public Sub btnPreview_Click(sender As Object, e As EventArgs) Handles btnPreview.Click 

    StudentData(0) = txtName.Text 
    StudentData(1) = txtAddress.Text 
    StudentData(2) = txtGPA.Text 
    StudentData(3) = txtAge.Text 

    'Find which radio button is checked to add to StudentData 
    If radFreshman.Checked = True Then 
     StudentData(4) = "Freshman" 
    ElseIf radSophomore.Checked = True Then 
     StudentData(4) = "Sophomore" 
    ElseIf radOther.Checked = True Then 
     StudentData(4) = "Other" 
    End If 

    'Find which check boxes are checked to add to StudentData 
    If chkCIS.Checked = True Then 
     StudentData(5) = "CIS" 
     If chkMath.Checked = True Then 
      StudentData(6) = "Math" 
      If chkScience.Checked = True Then 
       StudentData(7) = "Science" 
       If chkHistory.Checked = True Then 
        StudentData(8) = "History" 
       End If 
      End If 
     End If 
    End If 
    'Show StudentData ino the text of lblPreview 
    lblPreview.Text = StudentData(0 & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8) 
End Sub 

Private Sub btnStudent_Click(sender As Object, e As EventArgs) Handles btnStudent.Click 

    'Add StudentData elements to Student list 
    lstStudents.Items.Add(StudentData(0 & 1 & 2 & 3)) 

End Sub 
End Class 
+0

定義「程序崩潰」並定義「第57行」。什麼是例外?什麼行引發異常?發生這種情況時的價值是什麼?作爲開發環境的介紹,這是一個與調試器一起工作的非常好的機會。在您的IDE中設置一個調試斷點並逐步瀏覽代碼以觀察其運行時行爲。這應該給你很多關於發生了什麼的線索。 – David

+0

'lblPreview.Text = StudentData(0&1&2&3&4&5&6&7&8)'這不符合你的想法。你必須單獨連接每個元素'StudenData(0)&StudentData(1)...'當它在該行(以及其他人喜歡它)拋出一個exeption時,這是一個線索,那裏有一個錯誤。 'OPTION STRICT ON'也會有所幫助 – Plutonix

+0

感謝Plutonix你是一個拯救生命的人。現在我遇到一個問題,無論選中哪個複選框,即使未檢查,所有信息都會轉到lblPreview.Text。 –

回答

-1

也許這個方法,我這裏使用可以提供幫助,但也有一些差別..

我的源是數據庫..我在串聯數據庫中的字段(源) ,我只顯示文本,而不是列表框上的id。你可以將id連接到源文本字段並顯示接觸到的fullstring。

'create the adapter 
    Dim adapterU As New OleDb.OleDbDataAdapter("Select id,nombre+' '+apellido as fullname from users", con) 

    'create a datatable 
    Dim datatableU As New DataTable 

    'bring the info from the adapter (database) into the datatable. 
    adapterU.Fill(datatableU) 

    'relate the datasource (datable) to the listbox lsUsers 
    lsUsers.DataSource = datatableU 
    lsUsers.ValueMember = "id" 
    lsUsers.DisplayMember = "fullname"