如何从列表框返回值并从中分离出来? [英] How to return and separate values from listbox?

查看:76
本文介绍了如何从列表框返回值并从中分离出来?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Public Class Form1

'//Add button
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        ListBox1.Items.Add("a=" & TextBox1.Text & " " & "b=" & TextBox2.Text & " " & "c=" & TextBox3.Text)
        TextBox1.Text = ""
        TextBox2.Text = ""
        TextBox3.Text = ""

    End Sub
'//Remove button
    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        ListBox1.Items.Remove(ListBox1.SelectedItem)
    End Sub

'//Edit button
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        TextBox1.Text = ListBox1.SelectedItem()'//There is a problem because it returns all values into one textbox and I just wanna to separate values just like in input
    End Sub

推荐答案

您必须重写代码以使用SelectedItems,而不是SelectedItem(最后没有"s").

然后,您可以根据需要通过枚举SelectedItems返回的列表来构建字符串.
You have to rewrite your code to use SelectedItems, not SelectedItem (no "s" on the end).

Then you can built your string however you want from enumerating the list returned by SelectedItems.


通常会先将数据转换为单个字符串,然后再添加到列表框中,然后执行一些复杂的操作字符串处理以重新创建原始数据.

有更好的方法:
1)将对象添加到列表框
2)通过Format事件告诉列表框如何显示对象

这样一来,原始数据就不会丢失,并且可以通过从Object还原为实际类型来轻松恢复.

因此,在您的情况下,对象将是包含3个文本框内容的字符串数组,在示例中由数组data1和data2模拟.
It common to see data being converted into a single string before adding to a list box and then doing some complex string processing to recreate the original data.

There is a better way:
1) Add objects to the listbox
2) Tell the listbox how to display the object via the Format event

By doing this the original data is never lost and can be easily recovered by casting back from Object to the actual type.

So in your case the objects will be arrays of strings containing the contents of the 3 textboxes, simulated in the example by the arrays data1 and data2.
Private Sub AddArrayItems()
  Dim data1 As String() = {"One", "Two", "Three"}
  Dim data2 As String() = {"X", "Y", "Z"}
  listBox1.Items.Add(data1)
  listBox1.Items.Add(data2)
End Sub

Private Sub listBox1_Format(sender As Object, e As ListControlConvertEventArgs)
  Dim data As String() = DirectCast(e.Value, String())
  REM Display as e.g. "One-Two-Three"
  e.Value = String.Join("-", data)
End Sub

Private Sub listBox1_SelectedIndexChanged(sender As Object, e As EventArgs)
  REM get the string array back again
  REM In this example just display the number of array items
  Dim data As String() = TryCast(listBox1.SelectedItem, String())
  If data IsNot Nothing Then
    label1.Text = data.Length.ToString() & " selected items"
  End If
End Sub



该代码是从C#转换而来的,因此希望它几乎是正确的.

艾伦(Alan)



The code was converted from C# so hopefully it''s almost correct .

Alan


我用其他方式解决了这个问题.请参阅:
I resolve it on some other way.See this:
'Check for selection
If ListBox1.SelectedIndex = -1 Then
Return
End If
 
'Get the text of the selected item
Dim selectedtext As String = ListBox1.Items(ListBox1.SelectedIndex).ToString()
 
'Split the items one by one
Dim parts As String() = selectedtext.Split("="c, " "c, "="c, " "c, "="c, " "c)
 
If parts.Length > 2 Then
 
'Define a variable for each part
Dim part1 As String = parts(0).Trim()
Dim part2 As String = parts(1).Trim()
Dim part3 As String = parts(2).Trim()
Dim part4 As String = parts(3).Trim()
Dim part5 As String = parts(4).Trim()
Dim part6 As String = parts(5).Trim()
 
TextBox1.Text = part2
TextBox2.Text = part4
TextBox3.Text = part6
End If


这篇关于如何从列表框返回值并从中分离出来?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆