Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB.NET
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 11-Nov-12 4:25am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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
 
The code was converted from C# so hopefully it's almost correct .
 
Alan
  Permalink  
Comments
ProgramFOX at 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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 6

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
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Comments
ProgramFOX at 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.:/
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

I don't know how to separate values.:/
  Permalink  

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

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Guruprasad.K.Basavaraju 350
1 Sergey Alexandrovich Kryukov 226
2 Shai Vashdi 220
3 OriginalGriff 183
4 Tadit Dash 135
0 Sergey Alexandrovich Kryukov 9,255
1 OriginalGriff 5,433
2 Peter Leow 4,100
3 Maciej Los 3,540
4 Abhinav S 3,333


Advertise | Privacy | Mobile
Web01 | 2.8.140415.2 | Last Updated 11 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Use
Layout: fixed | fluid