Click here to Skip to main content
15,885,757 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Kindly review my code below, in my code, once change index value there are text changes happen in the richtextBox, but my problem is once change index values, final value only display in the richtextbox, I need to kept all the changes in every index. Please help asap. Thanks!

C#
for (int i = 0; i < listBox1.Items.Count; i++)
            {

                listBox1.SelectedIndex = i;
                textBox2.Text = Regex.Replace((listBox1.Text), @"<a([^>]*)>&sect;([\d]+)", "Section $2").ToString();
                textBox3.Text = Regex.Match((listBox1.Text), @"id=""([^\s]*)""").ToString();
                richTextBox2.Text = Regex.Replace((richTextBox1.Text), @" " + textBox2.Text + " ", " <a " + textBox3.Text + ">" + textBox2.Text + "</a> ").ToString();
           
 }
Posted
Updated 6-Dec-12 7:31am
v2
Comments
TRK3 6-Dec-12 14:57pm    
Not clear from your explanation what exactly you are trying to do and what exactly is happening.

Can you give us a clear concrete example of the input and the actual result vs. the expected result?
TRK3 6-Dec-12 15:00pm    
Actually, I just noticed that your code is looping through all the items in ListBox1 and setting the other controls.

Each time through the loop you are trashing whatever you set the previous time throught the loop. The only thing that you'll end up with is whatever you did very last time throught the loop.

You might as well just set i = listBox1.Items.Count - 1 and run the code once. The final result is the same.

I'm pretty sure that's not what you intended.
abdulsafran 6-Dec-12 22:33pm    
Hi TRK3, thanks for your help, from your code it goes to the first listbox item and then only first list box item regular expression is working, I exactly required is once search the first index it is regular expression should need to work in the richTextBox and once search the next index, first index output don't need to change in the richTextBox and second index regular expression should only change. These should continue like above. See below example.

Index[0] = Index[0] Work
Index[1] = Index[0] + Index[1]
Continue....
TRK3 7-Dec-12 13:13pm    
I don't understand what you are trying to do at all.

I can understand your code, but I don't understand your explanation of what you are trying to do, or what the undesired result is.

Maybe it's just a problem with the language, but if you can't clearly explain what it is you are trying to accomplish, then maybe you don't totally understand it (which is the first thing you have to do in order to be able to code anything).

It's probably easier to explain if you give actual examples.

Can you give us an example of what's in the list box, and then show us what you expect to have happen and what actually happens?
Jibesh 6-Dec-12 17:20pm    
You better the copy the method name where you added the above lines. You must note that whenever you changed the SelectedIndex i.e 'listbox.SelectedIndex = i;' ListBox_SelectedIndex changed event will be triggered. check whether you subscribed for this event.

1 solution

Hi,

According to your problem description you want all changes to be listed in the richTextBox2, right? If so, your code modifies the Text of richTextBox2 in each iteration and only the final value will be left there. I think what you should do is, concatenate the changes in to the richTextBox2.
richTextBox2.Text += (Regex.Replace((richTextBox1.Text), @" " + textBox2.Text + " ", " <a " + textBox3.Text + ">" + textBox2.Text + "</a> ") + "\n");

Regards.
 
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