Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
VB
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
Posted

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.
 
Share this answer
 
Comments
Thomas Daniels 11-Nov-12 12:47pm    
Comment from the OP, posted as a non-answer below (Solution 2):
I don't know how to seperate values.:/
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.
VB
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


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

Alan
 
Share this answer
 
Comments
Thomas Daniels 11-Nov-12 12:45pm    
Comment from the OP, posted as a non-answer below (Solution 4):
You really don't understand what I need to do.
As first I input three numbers into textboxes as STRING value and that all right.When I hit add to listbox,all values are send to listbox as ONE STRING.Now I wanna return values to textboxes as at the input step.
If someone wanna help me to change my code above I will be greatful.I am tried to find solution more than two days.
Thanks
I resolve it on some other way.See this:
VB
'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
 
Share this answer
 
I don't know how to separate values.:/
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900