2008-11-29 35 views
13

Trong Visual C# Express Edition, có thể làm cho một số (nhưng không phải tất cả) các mục trong một ListBox đậm? Tôi không thể tìm thấy bất kỳ loại tùy chọn nào cho điều này trong API.Làm cách nào để tôi có thể tạo một số mục trong một ListBox đậm?

+0

Bạn có trên WPF? Trong trường hợp đó, nó dễ dàng hơn nhiều – Gishu

+0

Tôi xin lỗi tôi là một chút của một newbie NET., Tôi thậm chí không chắc chắn những gì WPF được. –

Trả lời

30

Bạn cần thay đổi DrawMode của ListBox thành DrawMode.OwnerDrawFixed. Hãy kiểm tra những bài viết này trên MSDN:
DrawMode Enumeration
ListBox.DrawItem Event
Graphics.DrawString Method

Ngoài ra nhìn vào câu hỏi này trên các diễn đàn MSDN:
Question on ListBox items

Một ví dụ đơn giản (cả mặt hàng - Black-Arial-10-Bold):

public partial class Form1 : Form 
{ 
    public Form1() 
    { 
     InitializeComponent(); 

     ListBox1.Items.AddRange(new Object[] { "First Item", "Second Item"}); 
     ListBox1.DrawMode = DrawMode.OwnerDrawFixed; 
    } 

    private void ListBox1_DrawItem(object sender, DrawItemEventArgs e) 
    { 
     e.DrawBackground(); 
     e.Graphics.DrawString(ListBox1.Items[e.Index].ToString(), new Font("Arial", 10, FontStyle.Bold), Brushes.Black, e.Bounds); 
     e.DrawFocusRectangle(); 
    } 
} 
+0

Ví dụ và liên kết tuyệt vời. Ước gì tôi có thể bỏ phiếu này hai lần! –

0

Sau đây là mã chứng minh tương tự.

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 

namespace WindowsFormsApplication2 
{ 
    public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent(); 
     } 

     private void Form1_Load(object sender, EventArgs e) 
     { 
      foreach (FontFamily fam in FontFamily.Families) 
      { 
       listBox1.Items.Add(fam.Name); 
      } 
      listBox1.DrawMode = DrawMode.OwnerDrawFixed; // 属性里设置 

     } 

     private void listBox1_DrawItem(object sender, DrawItemEventArgs e) 
     { 
      e.DrawBackground(); 
      e.Graphics.DrawString(listBox1.Items[e.Index].ToString(), new Font(listBox1.Items[e.Index].ToString(), listBox1.Font.Size), Brushes.Black, e.Bounds); 
      //e.DrawFocusRectangle(); 
     } 
    } 
} 

Sample Output

1

Bổ sung vào giải pháp Mindaugas Mozūras, tôi có một vấn đề mà e.Bounds của tôi không đủ lớn và văn bản đã bị cắt đứt. Để giải quyết vấn đề này (nhờ một bài đăng here), bạn ghi đè sự kiện OnMeasureItem và thay đổi DrawMode thành DrawMode.OwnerDrawVariable.

Trong nhà thiết kế:

listBox.DrawMode = DrawMode.OwnerDrawVariable; 

Trong handler:

void listBox_MeasureItem(object sender, MeasureItemEventArgs e) 
{ 
    e.ItemHeight = 18; 
} 

Giải Quyết vấn đề của tôi có chiều cao cắt đứt văn bản.

+1

Chỉ cần thêm vào để nói rằng tôi đã không có vấn đề này trên máy tính phát triển của tôi, nhưng chúng tôi đã nhận được nó khi vận chuyển đến một máy chủ đầu cuối. Vì vậy, tôi khuyên bạn nên áp dụng mã này cho mã của bạn để đảm bảo an toàn. – Jonas

0

Ví dụ chung hơn sử dụng người gửi và thực sự tôn trọng màu nền trước (nếu mục được chọn, ví dụ hoặc người dùng sử dụng một số bộ màu khác, nơi màu nền trước màu đen không thực sự đọc được) và phông chữ ListBox hiện tại:

private void listBoxDrawItem (object sender, DrawItemEventArgs e) 
    { 
     Font f = e.Font; 
     if (e.Index == 1) //TODO: Your condition to make text bold 
      f = new Font(e.Font, FontStyle.Bold); 
     e.DrawBackground(); 
     e.Graphics.DrawString(((ListBox)(sender)).Items[e.Index].ToString(), f, new SolidBrush(e.ForeColor), e.Bounds); 
     e.DrawFocusRectangle(); 
    } 

Bạn cần phải có DrawMode được đặt thành OwnerDrawFixed (ví dụ: trong trình thiết kế).

0

Make mục đã chọn đậm

public partial class Form1 : Form 
{ 
    public Form1() 
    { 
     InitializeComponent(); 

     ListBox1.Items.AddRange(new Object[] { "me", "myself", "bob"}); 

     // set the draw mode to fixed 
     ListBox1.DrawMode = DrawMode.OwnerDrawFixed; 
    } 

    private void ListBox1_DrawItem(object sender, DrawItemEventArgs e) 
    { 
     // draw the background 
     e.DrawBackground(); 

     // get the font 
     Font font = new Font(e.Font, (e.State & DrawItemState.Selected) == DrawItemState.Selected ? FontStyle.Bold : FontStyle.Regular); 

     // draw the text 
     e.Graphics.DrawString(ListBox1.Items[e.Index].ToString(), font, new SolidBrush(ListBox1.ForeColor), e.Bounds); 

     e.DrawFocusRectangle(); 
    } 
} 
Các vấn đề liên quan