Quantcast
Channel: Windows Forms General forum
Viewing all articles
Browse latest Browse all 12583

How can i color in Red also another text from the string in the listBox?

$
0
0

This is the code now:

publicstaticvoidColorListBox(List<string> data,DrawItemEventArgs e){string url = data[e.Index].Substring(0,5);string keywords = data[e.Index].IndexOf

            using (Font f =newFont(FontFamily.GenericSansSerif,8,FontStyle.Regular)){if((e.State&DrawItemState.Selected)==DrawItemState.Selected)
                    e.Graphics.FillRectangle(Brushes.LightBlue, e.Bounds);else{
                    using (SolidBrush sb =newSolidBrush(SystemColors.Window))
                        e.Graphics.FillRectangle(sb, e.Bounds);}SizeF size = e.Graphics.MeasureString(url, f);
                using (SolidBrush sb =newSolidBrush(Color.Red))
                    e.Graphics.DrawString(url, f, sb,newPointF(e.Bounds.X, e.Bounds.Y));string token = data[e.Index].Substring(url.Length, data[e.Index].LastIndexOf(" --- ")-(url.Length));
                e.Graphics.DrawString(token, f,Brushes.Black,newPointF(e.Bounds.X + size.Width, e.Bounds.Y));

                size = e.Graphics.MeasureString(url + token, f);
                e.Graphics.DrawString(data[e.Index].Substring(data[e.Index].IndexOf(token)+ token.Length, data[e.Index].LastIndexOf(": ")- token.Length- data[e.Index].IndexOf(token)+1), f,Brushes.Black,newPointF(size.Width, e.Bounds.Y));

                token = data[e.Index].Substring(data[e.Index].LastIndexOf(": ")+2);
                size = e.Graphics.MeasureString(data[e.Index].Substring(0, data[e.Index].LastIndexOf(token)), f);
                using (SolidBrush sb =newSolidBrush(Color.Green))
                    e.Graphics.DrawString(token, f, sb,newPointF(e.Bounds.X + size.Width+4, e.Bounds.Y));

                e.DrawFocusRectangle();}}

I want to use now indexof and substring and not regex. The variable data contain fro example in index 0:

Url: http://www.walla.co.il --- Localy KeyWord: walla

In index 1:

Url: http://www.google.com --- Localy KeyWord: google

What the code does is coloring all the text: Url: in Red Url: Url:

This in Red the rest of each line is in black and green in the end. I want now to color in Red also the words: Localy Keword in each line. So next time I will run the application I will see Url: in Red and also Localy KeyWord in Red.

How can I do it ?

I added to the code this line:

string keywords = data[e.Index].IndexOf

But not sure what should I do.

The variable url contain the: Url: from the data But how can I find get the Localy KeyWord ?

This is what I did so far now:

Added this:

int keywords = data[e.Index].IndexOf("Localy KeyWord:");string keyword = data[e.Index].Substring(keywords,15);

So now keyword contain the string: Localy KeyWord Then I tried to color it in Red and added/changed this:

using (SolidBrush sb =newSolidBrush(Color.Red)){
                    e.Graphics.DrawString(url, f, sb,newPointF(e.Bounds.X, e.Bounds.Y));
                    e.Graphics.DrawString(keyword, f, sb,newPointF(e.Bounds.X, e.Bounds.Y));}

But it's not coloring it right it's moving the text ot the beginning and color it on the other text the Url: and make a mess.

What should I do to solve it ?


danieli


Viewing all articles
Browse latest Browse all 12583

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>