1

I have a ListView in Details mode, and I'd like to have some of its items' text in bold (for highlighting where it finds some substring in the item text).

So something like this:

Somefilename/boldText/etc

Is there any way to do that?

Divi
  • 7,621
  • 13
  • 47
  • 63
bakua
  • 13,704
  • 7
  • 43
  • 62

2 Answers2

3

Okay, so this is going to be difficult because you're going to have to OwnerDraw your ListView first by setting that property to true. Now that you've done that you're going to need to implement DrawColumnHeader and place this line in it:

e.DrawDefault = true;

Next you'll need to implement the following code, either in DrawItem or DrawSubItem depending on which area it exists in. Keep in mind the code I'm giving you isn't fully complete because it's not parsing the string or anything and further you still need to implement drawing a selected item now because your drawing the text on your own.

var boldFont = new Font(this.Font, FontStyle.Bold);
var location = new PointF(e.Bounds.Location.X, e.Bounds.Location.Y);

e.Graphics.DrawString("Somefilename/", this.Font, Brushes.Black, location);
var size = e.Graphics.MeasureString("Somefilename/", this.Font);

location.X += size.Width;
e.Graphics.DrawString("boldText", boldFont, Brushes.Black, location);
size = e.Graphics.MeasureString("boldText", boldFont);

location.X += size.Width;
e.Graphics.DrawString("/etc", this.Font, Brushes.Black, location);

Another thing to note is that you'll have to play with the offsets a little because some fonts have margins and bolded fonts take up more room.

If the Item is not a SubItem then just implement the e.DrawDefault = true; for the DrawSubItem event - and vica versa if the item is a SubItem then implement that line for DrawItem.

Mike Perrenoud
  • 66,820
  • 29
  • 157
  • 232
  • Thank you :) this is what i was looking for. But i still have 1 problem. When i draw my text it looks weird in meaning of the letter lines are wider, with kind of green outline. It look blurry if you know what i mean :) – bakua Aug 24 '12 at 11:18
  • Instead of using `Brushes.Black` you could try using `SystemBrushes.WindowText`. But that's a shot in the dark because I wasn't experiencing that. – Mike Perrenoud Aug 24 '12 at 11:50
  • It didn't help :( here is screen of that (top text is obviously wrong drawed, but as soon as I select this item it goes right and since that everything is ok) http://img607.imageshack.us/img607/7375/screenuoa.png – bakua Aug 24 '12 at 13:02
  • 1
    Normal ListView is rendered using GDI which tends to be clearer. DrawString() uses GDI+ which has lots of advantages but looks different. Before doing any text renderering, make sure you tell GDI+ to use ClearType rendering: g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit; – Grammarian Aug 26 '12 at 01:37
  • Thanks Grammarian, totaly worked for me :) but i had to use System.Drawing.Text.TextRenderingHint.AntiAliasGridFit instead of ClearTypeGridFit which doesn't work for me. – bakua Aug 28 '12 at 15:07
2

You'll have to override OnPaint. Try these links:

Make portion of a Label's Text to be styled bold

Custom ListView in Winforms?

But, the listview is not painted by the .net framework and is only supported. So, even if you override the OnPaint, it might not be called. So you might have to override your listview control as well.

Community
  • 1
  • 1
Divi
  • 7,621
  • 13
  • 47
  • 63
  • 2
    OnPaint will definitely not help you. You'll have to a lot of work with a ListView to do something this complicated. Look at ObjectListView for an example. – Grammarian Aug 23 '12 at 12:26
  • That is correct - I placed in a **very** partial implementation of how to draw the text the OP wanted, but there is a lot more work to do. @Grammarian, if you have more to add to my post please feel free. – Mike Perrenoud Aug 23 '12 at 12:27
  • True, the .UseItemStyleForSubItems = false has to be set for each item. This is the most suitable answer in my opinion. – Larry Feb 05 '14 at 13:19