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?

4
  • 1
    Just like @KlausByskovHoffmann asked, is it windows or web?
    – MaxDataSol
    Aug 23, 2012 at 11:50
  • sorry, forgot to mention that... its WinForm app
    – bakua
    Aug 23, 2012 at 11:57
  • Have you ever drawn text before using GDI? Because that's what you're going to have to do. Aug 23, 2012 at 12:01
  • See answer to this question stackoverflow.com/questions/10191254/…
    – Grammarian
    Aug 23, 2012 at 12:25

2 Answers 2

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.

5
  • 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, 2012 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. Aug 24, 2012 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) img607.imageshack.us/img607/7375/screenuoa.png
    – bakua
    Aug 24, 2012 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, 2012 at 1: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, 2012 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.

3
  • 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, 2012 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. Aug 23, 2012 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 5, 2014 at 13:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.