Click here to Skip to main content
15,878,959 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I have a listbox with many items like these:

test_test_0.0.0.0_000
test5_test5_1.1.1.1_000_0
testing_testing_0.1.2.3_000
blabla_blabla_0.5.4.3_000


I need to rename the listbox items into:

rtsp://test:test@0.0.0.0:000
rtsp://test5:test5@1.1.1.1:000/0
rtsp://testing:testing@0.1.2.3:000
rtsp://blabla:blabla@0.5.4.3:000


How can I rename and display all these items in the same listbox at once? maybe in a for each loop?
Thanks in advance

What I have tried:

For Each item As String In ListBox.Items


            Dim result As String
            Dim splitarray() As String 'Çredits to Tony Hill for this code'

            splitarray = item.Split("_")
            If splitarray.Length = 3 Then
                result = "rtsp://" + splitarray(0) + ":" + splitarray(1) + "@" + splitarray(2)
                MsgBox(result) 'This is only to see what is happening

            End If
        Next
Posted
Updated 5-Apr-22 7:53am
v4

As far as I know, the ListBox item has to be selected before you can change the text. You will find that difficult in a For Each loop, especially as you have Strings rather than Objects.

Try changing the loop to a For iterator = range loop. E.g.
VB
For i As Integer = 0 To ListBox.Items.Count - 1 

	'Select the item
    ListBox.SetSelected(i, True)
	
	'Derive the new text
	Dim result As String
	Dim splitarray() As String 'Çredits to Tony Hill for this code'

	splitarray = ListBox.SelectedItem.ToString().Split("_")

	'Assign the new text to the selected item
	If splitarray.Length = 3 Then
		result = "rtsp://" + splitarray(0) + ":" + splitarray(1) + "@" + splitarray(2)
		ListBox.SelectedItem = result
	End If	
	
Next
Caveat: I have not been able to test this, but the principle is sound
 
Share this answer
 
Comments
Maciej Los 5-Apr-22 12:48pm    
ListBox item has to be selected before you can change the text - Are you sure, Caroline?
CHill60 5-Apr-22 14:01pm    
Not 100% to be sure, and was unable to test my understanding, it didn't feel right to be honest. Clearly from your solution I was mistaken
If ListBox's items are not bound with data source, you can change the text of items in single loop.
See C# code (LinqPad):

C#
void Main()
{
	MyForm mf = new MyForm();
	//mf.ShowDialog();


}

public class MyForm: Form
{
	public MyForm()
	{
		this.Size = new Size(600, 340);
		this.Text = "MyForm";
		InitializeControls();
		this.Show();
	}
	
	private void InitializeControls()
	{
		//ListBox1
		ListBox1 = new ListBox(){Location = new Point(10,10), Size = new Size(150, 280), Anchor = (AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Bottom)};
		for(int i =1; i<=20; i++)
			ListBox1.Items.Add($"Item_{i}");
		
		//Button1
		Button1 = new Button(){Location = new Point(180,10), Size = new Size(120, 24), Anchor = (AnchorStyles.Left | AnchorStyles.Top), Text = "Change text!"};
		Button1.Click += Button1_Click;
		
		//add controls
		this.Controls.Add(ListBox1);
		this.Controls.Add(Button1);
	}

	private void Button1_Click(object sender, EventArgs e)
	{
		Button btn = sender as Button;
		string btnText = btn.Text == "Change text!" ? "Change again ;)" : "Change text!";
		char splitOn = btn.Text == "Change text!" ? '_' : '@';
		string replacement = btn.Text == "Change text!" ? "@" : "_";
		
		for(int i=0; i<ListBox1.Items.Count; i++)
		{
			object item = ListBox1.Items[i];
			string[] parts = item.ToString().Split(splitOn);
			string newText = parts[0] + replacement + parts[1];
			ListBox1.Items[i] = newText;
			btn.Text = btnText;
		}
	}
	
	
	private ListBox ListBox1;
	private Button Button1;
	
}
 
Share this answer
 
v2

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