且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

我如何使用索引号将数据从列表框传输到文本框?

更新时间:2023-02-10 16:59:23

我不知道为什么你的教授指示你使用任意数组,但这是另一个问题.以下代码反映了所需的行为:

I´m not sure why your professor instructs you to use an arbitrary array but that´s another question. The following code reflects the desired behavior:

Public Class Form1

    Dim ArrayofNames() As String

    Private Sub Retrievebtn_Click(sender As Object, e As EventArgs) Handles Retrievebtn.Click
        Dim idxRetreive = Integer.Parse(TextBox2.Text)
        TextBox3.Text = ArrayofNames(idxRetreive)
    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        syncItemArray()
    End Sub

    Private Sub Addbtn_Click(sender As Object, e As EventArgs) Handles Addbtn.Click
        Dim newItem = TextBox1.Text
        ListBox1.Items.Add(newItem)
        syncItemArray()
    End Sub

    Private Sub syncItemArray()
        ReDim ArrayofNames(ListBox1.Items.Count - 1)
        ListBox1.Items.CopyTo(ArrayofNames, 0)
    End Sub
End Class