我如何使用索引号将数据从列表框传输到文本框? [英] How i gonna transfer a data from listbox to textbox using index numbers?
本文介绍了我如何使用索引号将数据从列表框传输到文本框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我在VB.net中所做的布局:
This the layout I made in the VB.net :
如何仅使用数字将一项从我制作的列表框转移到标签框或文本框(可选).我还创建了一个数组,将其添加到列表框中.
How do I transfer one item from the listbox I made to the label box or textbox(optional) using numbers only. I created also a array for it to add to the listbox.
这是整个代码
Public Class Form1
Dim ArrayofNames() As String
Dim x As Integer
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub Retrievebtn_Click(sender As Object, e As EventArgs) Handles Retrievebtn.Click
If ListBox1.SelectedIndex >= 0 Then
ListBox1.Items.Add(Retrievetextbox.Text & " " & ArrayofNames.Length - 1)
End If
End Sub
Private Sub Addbtn_Click(sender As Object, e As EventArgs) Handles Addbtn.Click
Dim x As Integer = 0
ReDim ArrayofNames(x)
For x = 0 To ArrayofNames.Length - 1
ArrayofNames(x) = Addtextbox.Text
ListBox1.Items.Add(ArrayofNames(x))
Next
End Sub
End Class
推荐答案
我不确定您的教授为什么指示您使用任意数组,但这是另一个问题.以下代码反映了所需的行为:
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
这篇关于我如何使用索引号将数据从列表框传输到文本框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文