17

Ehm, umm, this means some lines should be two-lined in size. My boss think this is more simple solution, than limit displayed text to fit width and don't like horizontal scroll bar >_<

4 Answers 4

41
lst.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawVariable;
lst.MeasureItem += lst_MeasureItem;
lst.DrawItem += lst_DrawItem;

private void lst_MeasureItem(object sender, MeasureItemEventArgs e)
{
    e.ItemHeight = (int)e.Graphics.MeasureString(lst.Items[e.Index].ToString(), lst.Font, lst.Width).Height;
}

private void lst_DrawItem(object sender, DrawItemEventArgs e)
{
    e.DrawBackground();
    e.DrawFocusRectangle();
    e.Graphics.DrawString(lst.Items[e.Index].ToString(), e.Font, new SolidBrush(e.ForeColor), e.Bounds);
}
Sign up to request clarification or add additional context in comments.

13 Comments

Love it. One minor thing though. When I databind a custom class, the DisplayMember gets wiped and I can't seem to reset it. Any tips?
@BillSambrone - Sorry, but I don't use databind and never meet same problem and don't know how to solve.
@AycanYaşıt : totaly agree
@Kosmos Seems that the ListBox caches the items height, what is good. The problem is that if the ListBox is resized it don't recalculates the height! I'm trying someway to clean the cache of items height without removing and adding then, in the resize, but I don't find any method for that. At least documented.
@DiegoCNascimento - I used ListBox.Refresh method in resize handler for that.
|
2
private void lst_MeasureItem(object sender, MeasureItemEventArgs e)
{
    e.ItemHeight = (int)e.Graphics.MeasureString(lst.Items[e.Index].ToString(), lst.Font, lst.Width).Height;
}

private void lst_DrawItem(object sender, DrawItemEventArgs e)
{
    e.DrawBackground();
    e.DrawFocusRectangle();
    e.Graphics.DrawString(lst.Items[e.Index].ToString(), e.Font, new SolidBrush(e.ForeColor), e.Bounds);
}

To get the right display member to show up when data binding, replace

lst.Items[e.Index].ToString()

with a casted version of the property. So if your binding source is class object Car it would look like

((Car)lst.Items[e.Index]).YourDisplayProperty

Then the above functions can appropriately measure the string and draw it. :)

Comments

0

Helpful link

Check out this answer. It overrides the template of the listbox with a textblock which wraps the text. Hope it's useful. To solve your problem I think you shold add : ScrollViewer.HorizontalScrollBarVisibility="Disabled" . Found it here

1 Comment

Thanks, but I use win forms :(
0

To make binding correct, be sure to add check "lst.Items.Count > 0" to lst_MeasureItem function. Here is my example:

 if (lst.Items.Count > 0)
 {
    e.ItemHeight = (int)e.Graphics.MeasureString(lst.Items[e.Index].ToString(), lst.Font, lst.Width).Height;
 }

Everything else seems to work nicely after that.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.