2015-04-29 84 views
-1

這裏是我的代碼: -我想知道如何將項目添加到一個ArrayList

If FirstNameText.Text = "" Then 
     MessageBox.Show("Please enter your first name please") 
    End If 
    If SurnameText.Text = "" Then 
     MessageBox.Show("Please enter your surname please") 
    End If 
    If (Not RB_Male.Checked) AndAlso (Not RB_Female.Checked) Then 
     MessageBox.Show("Please select your gender") 
    End If 
    If ComboBox1.SelectedValue = False Then 
     MessageBox.Show("Please select your year group") 
    End If 
    If TextStudentID.Text = "" Then 
     MessageBox.Show("Please select the generate button to give you a unique student ID") 
    End If 
    "in this section I want to add all the submitted items to an arraylist so a user's name pops up in the list box" 
    Try 

    Catch ex As Exception 

    End Try 

這是我寫了,我到處尋找答案提交項目到一個ArrayList的代碼的唯一位。該區域是我計劃將這些項目提交給數組列表的地方。任何幫助或鏈接到一個很好的指導我們做。

+2

不要使用ArrayList。它仍然存在與舊代碼的兼容性,但沒有充分的理由在新項目中使用它。 –

+0

代碼應該做什麼?目前還不清楚你在嘗試什麼,爲什麼它不起作用。要回答你的標題:'arraylist.Add(yourObject)' –

+0

堅持我會更新整篇文章並解釋我想要做什麼。 – GiordySays

回答

-1

也許我編輯的代碼會對你有所幫助:)另外我添加的//<Text>可以幫助你以及如何編輯ArrayList。

+0

希望這可以解決您的問題,如何將arraylist放入列表框,所以首先您需要將Array {ArrayName}聲明爲arraylist = new arraylist,然後將您的文本框轉換爲剛好是{ArrayName}的ArrayList。添加(TextboxName),然後將它們顯示在列表框中它只是{ListboxName} .Items.AddRange(ArrayName.ToArray()) – Arkanos

0

首先,我會做一個類爲您的學生信息

Class Student 
    Public FirstName As String 
    Public Surname As String 
    Public Gender As String 
    Public YearGroup As String 
    Public StudentId As String 
End Class 

其次,我會用一個List不是ArrayList,並確保這是在類級別,因此它可以在你的代碼中使用

Private StudentList As New List(Of Student) 

第三,你從你的控件驗證表單上的數據後,你會實例化一個Student對象,然後將其添加到您的StudentList。一旦你添加完所有新Student數據,您可以訪問StudentList其他方法做任何你想做

' Validate all fields have data for a new student 
If String.IsNullOrEmpty(FirstNameText.Text) Then 
    MessageBox.Show("Please enter your first name please") 
    FirstNameText.Focus() 
    Return 
ElseIf String.IsNullOrEmpty(SurnameText.Text) Then 
    MessageBox.Show("Please enter your surname please") 
    SurnameText.Focus() 
    Return 
ElseIf Not RB_Male.Checked AndAlso Not RB_Female.Checked Then 
    MessageBox.Show("Please select your gender") 
    Return 
ElseIf ComboBox1.SelectedIndex = -1 Then 
    MessageBox.Show("Please select your year group") 
    Return 
ElseIf String.IsNullOrEmpty(TextStudentID.Text) Then 
    MessageBox.Show("Please select the generate button to give you a unique student ID") 
    GenerateButton.Focus() 
    Return 
End If 

' Create your new Student object 
Dim NewStudent As New Student() 
NewStudent.FirstName = FirstNameText.Text 
NewStudent.Surname = SurnameText.Text 
If RB_Male.Checked Then 
    NewStudent.Gender = "M" 
Else 
    NewStudent.Gender = "F" 
End If 
NewStudent.YearGroup = ComboBox1.SelectedItem 
NewStudent.StudentId = TextStudentID.Text 

' Add your new student object to your List 
StudentList.Add(NewStudent) 
相關問題