Click here to Skip to main content
12,405,700 members (64,845 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C#
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!

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 6-Dec-12 6:15am
Updated 6-Dec-12 7:31am
ridoy58K
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

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160721.1 | Last Updated 14 Dec 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100