程序很简单,它要求用户的名字和姓氏以及其他三个交互式用户输入元素。当用户键入名字和姓氏时,它应使用数组并通过单击按钮将名称发送到ComboBox。目前我还不知道如何使用数组将用户输入的值发送到Combox。
以下是我现在的代码:
Public Class Form1
Dim people(20) As String
Dim peopleIndex As Integer
Dim firstName As String
Dim lastName As String
Dim blnGroup As Boolean
Dim numberOfAdult As Integer
Dim numberOfChild As Integer
Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
peopleIndex = -1
End Sub
Private Sub btnAddCustomer_Click(sender As System.Object, e As System.EventArgs) Handles btnAddCustomer.Click
peopleIndex += 1
If peopleIndex > UBound(people) Then
ReDim Preserve people(peopleIndex + 10)
End If
End Sub
End Class
答案 0 :(得分:1)
不太确定这是否是您想要的,但它至少可以让您了解如何使用数组。
首先,声明一个像这样的全局字符串列表:
Dim people As New List(Of String)
然后在表单中添加一个组合框,一个文本框和另一个按钮。
现在试试这个:
Private Sub btnAddCustomer_Click(sender As Object, e As EventArgs) Handles btnAddCustomer.Click
'' Add the textbox1's text to the list of strings and to the combobox
people.Add(TextBox1.Text)
ComboBox1.Items.Add(TextBox1.Text)
End Sub
Private Sub btnDelCustomer_Click(sender As Object, e As EventArgs) Handles btnDelCustomer.Click
'' Remove the selected item from the list of strings
people.Remove(ComboBox1.SelectedItem)
'' After delete, repopulate the combobox
ComboBox1.Items.Clear()
For Each person In people
ComboBox1.Items.Add(person)
Next
End Sub
确保将第二个按钮命名为“btnDelCustomer”'或者只是更改事件处理程序。同样,我不清楚你想要什么,所以如果你需要更多信息,请问。
答案 1 :(得分:1)
好的,这个怎么样:
首先声明全局数组列表。
Dim people, age, sex, phoneNo As New ArrayList
现在添加按钮,请使用:
'' check to see If all text fields have a user input
If Not ((TextBox1.Text = "") Or (TextBox2.Text = "") Or (TextBox3.Text = "") Or (TextBox4.Text = "")) Then
people.Add(TextBox1.Text)
ComboBox1.Items.Add(TextBox1.Text)
TextBox1.Text = Nothing
age.Add(TextBox2.Text)
TextBox2.Text = Nothing
sex.Add(TextBox3.Text)
TextBox3.Text = Nothing
phoneNo.Add(TextBox4.Text)
TextBox4.Text = Nothing
Else
MessageBox.Show("Please fill in all fields.")
End If
对于您的组合框 - 选择的索引已更改,请使用:
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
'' When the combobox has changed its value and is not -1, update the labels
If Not ComboBox1.SelectedIndex = -1 Then
Label1.Text = String.Format("Age: {0}", age(ComboBox1.SelectedIndex))
Label2.Text = String.Format("Sex: {0}", sex(ComboBox1.SelectedIndex))
Label3.Text = String.Format("Phone Number: {0}", phoneNo(ComboBox1.SelectedIndex))
End If
End Sub
最后是你的删除按钮,请使用:
If Not ComboBox1.SelectedIndex = -1 Then
'' Remove the selected values from the array list
people.RemoveAt(ComboBox1.SelectedIndex)
age.RemoveAt(ComboBox1.SelectedIndex)
sex.RemoveAt(ComboBox1.SelectedIndex)
phoneNo.RemoveAt(ComboBox1.SelectedIndex)
'' Repopulate the combobox
ComboBox1.Items.Clear()
For Each person In people
ComboBox1.Items.Add(person)
Next
'' Update the labels
Label1.Text = "Age: None Selected!"
Label2.Text = "Sex: None Selected!"
Label3.Text = "Phone Number: None Selected!"
Else
MessageBox.Show("None Selected!")
End If
显然,这只是一个模拟,你会采取这种方式并以自己的方式实现它。希望这有帮助