2012-01-12 15 views
6

मैं उन वस्तुओं को आकर्षित करने की कोशिश कर रहा हूं जो उनके अंत में * लाल रंग में वर्ण हैं (और * वर्ण हटाएं) और अन्य वस्तुओं को काले रंग में खींचें।लिस्टबॉक्स मैनुअल DrawItem बड़ा फ़ॉन्ट आकार

private void listBox1_DrawItem(object sender, DrawItemEventArgs e) 
    { 
     e.DrawBackground() ; //Draw our regular background 
     if (Microsoft.VisualBasic.Strings.Right(listBox1.Items[e.Index].ToString(), 1) == "*") 
     { 
      e.Graphics.DrawString(Microsoft.VisualBasic.Strings.Mid(listBox1.Items[e.Index].ToString(),1,listBox1.Items[e.Index].ToString().Length - 1), e.Font, Brushes.Red, e.Bounds); //Draw the item text in red! 
     } 
     else 
     { 
      e.Graphics.DrawString(listBox1.Items[e.Index].ToString(), e.Font, Brushes.Black, e.Bounds); //Draw the item text in its regular color 
     } 
    } 

इसके अलावा लिस्टबॉक्स की DrawMode संपत्ति OwnerDrawVariable पर सेट है:

इस

मेरी कोड है।

मेरा कोड ठीक काम कर रहा है जब सूचीबॉक्स का फ़ॉन्ट डिफ़ॉल्ट फ़ॉन्ट है।

लेकिन जब मैं फ़ॉन्ट आकार को 8.25 (डिफ़ॉल्ट आकार) से 14 तक बदलता हूं, तो पाठ का आधा सूचीबॉक्स पर खींचता है। इस तरह: My listbox when size is 14!

लेकिन डिफ़ॉल्ट फ़ॉन्ट आकार के साथ, सब कुछ सही है।

समस्या क्या है?

उत्तर

6

आप MeasureItem घटना को संभालने और वहाँ मदों की ऊंचाई सेट करने के लिए:

private void listBox1_MeasureItem(object sender, MeasureItemEventArgs e) 
{ 
    e.ItemHeight = listBox1.Font.Height; 
} 
+2

मैं का इस्तेमाल किया है 'e.ItemHeight = listBox1.Font.Height;' और यह अच्छी तरह से काम करता है। धन्यवाद! –

+2

बढ़िया, मैं आपकी टिप्पणी के साथ अपना उत्तर अपडेट करूंगा, इसलिए यह अतिरिक्त कस्टम 'ListBoxFontItem' क्लास पर निर्भर नहीं है। –

संबंधित मुद्दे