Hello Daniweb Community,
I was wondering if there is a way to replace words from a listbox.
So at the moment I've got a listbox with a couple of strings to be replaced in the textbox, however single line listbox items work but when I add a multi-lined item eg.
Hello
There

It'll go into the listbox as HelloThere, so when I go to use the for each to replace them It doesn't work.

I've tried manually replacing it by using TextBox1.Text = TextBox1.Text.Replace("Hello" & Environment.NewLine & "There") it'll work, and if I try

For Each lbItem In .ListBox1.Items
    TextBox1.Text = Replace(TextBox1.Text, lbItem, "{HIDDED}")
Next

it doesn't work.

Any ideas why this is happening?

Recommended Answers

All 3 Replies

It is not very clear what you attempt to do. In your example you replace a single word, in your for each loop you replace all(?) text.
Also you should use something like:

For Each lbItem As string In ListBox1.Items
    TextBox1.Text = Replace(TextBox1.Text, lbItem, "{HIDDED}")
Next

OK, I believe you: it doesn't work now tell me, how can I find out what does not work if you just give me some code and tell me it does not work?
I'd like to know WHAT is not working, that it probably is not working is obvious for everyone. Now my poor mind is strugling over your problem. But you don't seem to care I guess. You just have a problem and want a quick response. For you it just doesn't work, but by just telling that, you are providing us with no information whatsoever. Can you understand a simple fact like that, or do you need more explanation? Normally I'm not that verbose and I can go on and on if you like, but I'm gonna leave it by this. Hope you understand :)

No matter I figured it out, it was because it went from a RichTextBox into a ListBox then tried looking through a TextBox for what I wanted replaced so I solved it using the code below

For Each lbItem In .ListBox1.Items
    TextBox1.Text = Replace(TextBox1.Text, lbItem.Replace(ControlChars.Lf, Environment.NewLine), "{HIDDED}")
Next
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.