我需要帮助尝试学习如何使结构与arraylists一起工作。我正在尝试制作一个允许用户输入帐户的程序。这是一个家庭作业,教授想要使用arraylists而不是list。
该帐户包括名字,姓氏,帐号和余额。用户可以使用previoous和next按钮来浏览帐户,并使用新的,保存和添加按钮来创建或更改帐户。我很好奇我如何在整个arraylist中循环,以及我如何在某个索引处调用结构中的某个变量。例如,我想在firstnametextbox中显示第一个名称。当我按下“下一步”按钮时,如何使FirstNameTextbox.Text ='帐户结构的名字,在第二个arraylist点'
这是我的代码
Public Class AccountInformationForm
Dim objaccount As account
Dim accounts As New ArrayList
Public Structure account
Public RecordNum As Integer
Public FirstName As String
Public LastName As String
Public balance As Double
Public account As Integer
End Structure
Private Sub AccountInformationForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'Example account to start off with
objaccount.RecordNum = 0
objaccount.FirstName = "Jesus"
objaccount.LastName = "Christ"
objaccount.balance = 100
objaccount.account = 1
accounts.Add(objaccount)
'Display First Record
firstTextBox.Text = objaccount.FirstName
lastTextBox.Text = objaccount.LastName
balanceTextBox.Text = objaccount.balance
accountTextBox.Text = objaccount.account
End Sub
Private Sub nextButton_Click(sender As Object, e As EventArgs) Handles nextButton.Click
objaccount.RecordNum += 1
If objaccount.RecordNum > accounts.Count Then
objaccount.RecordNum = 0
End If
'Display Current Record
firstTextBox.Text = objaccount.FirstName
lastTextBox.Text = objaccount.LastName
balanceTextBox.Text = objaccount.balance
accountTextBox.Text = objaccount.account
End Sub
Private Sub previousButton_Click(sender As Object, e As EventArgs) Handles previousButton.Click
'Before paging away, save the current form data in the current record
objaccount.RecordNum -= 1
'Check for wrap around
If objaccount.RecordNum < 0 Then 'the first, zeroth, element = index of 0
objaccount.RecordNum = accounts.Count
End If
firstTextBox.Text = objaccount.FirstName
lastTextBox.Text = objaccount.LastName
balanceTextBox.Text = objaccount.balance
accountTextBox.Text = objaccount.account
End Sub
Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
Dim upper As Integer = accounts.Count
objaccount.RecordNum = (upper + 1)
If firstTextBox.Text = "" Then
MsgBox("Please enter a name")
Else
objaccount.FirstName = firstTextBox.Text
End If
If lastTextBox.Text = "" Then
MsgBox("Please enter a name")
Else
objaccount.LastName = lastTextBox.Text
End If
If balanceTextBox.Text = "" Then
MsgBox("Please enter a name")
Else
objaccount.balance = balanceTextBox.Text
End If
If accountTextBox.Text = "" Then
MsgBox("Please enter a name")
Else
objaccount.account = accountTextBox.Text
End If
accounts.Add(objaccount)
firstTextBox.Clear()
lastTextBox.Clear()
balanceTextBox.Clear()
accountTextBox.Clear()
End Sub
Private Sub Save_Click(sender As Object, e As EventArgs) Handles Save.Click
Dim upper As Integer = accounts.Count
objaccount.RecordNum = (upper + 1)
If firstTextBox.Text = "" Then
MsgBox("Please enter a name")
Else
objaccount.FirstName = firstTextBox.Text
End If
If lastTextBox.Text = "" Then
MsgBox("Please enter a name")
Else
objaccount.LastName = lastTextBox.Text
End If
If balanceTextBox.Text = "" Then
MsgBox("Please enter a name")
Else
objaccount.balance = balanceTextBox.Text
End If
If accountTextBox.Text = "" Then
MsgBox("Please enter a name")
Else
objaccount.account = accountTextBox.Text
End If
accounts.Add(objaccount)
End Sub
Private Sub btnNew_Click(sender As Object, e As EventArgs) Handles btnNew.Click
firstTextBox.Clear()
lastTextBox.Clear()
balanceTextBox.Clear()
accountTextBox.Clear()
Dim upper As Integer = accounts.Count
objaccount.RecordNum = (upper + 2)
accountTextBox.Text = objaccount.RecordNum
End Sub
结束班