ListBox中如何实现两行显示一个颜色,可能需要自己写一个类

liwei84516 2009-06-10 12:00:24
如题,要实现两行显示一个颜色,怎么写这个类? 最好能给个完整的事例,谢谢 谢谢啊!急
...全文
56 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
龙宜坡 2009-06-10
  • 打赏
  • 举报
回复
1楼的可以!
outou 2009-06-10
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 feiyun0112 的回复:]
http://www.codeproject.com/KB/combobox/colorlistbox.aspx

[/Quote]
方法不错,支持
h_w_king 2009-06-10
  • 打赏
  • 举报
回复

this.listBox1.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawFixed;

private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
{
Color bg;


else
{
if (e.Index % 2 == 0)
bg = Color.Red;
else
bg = Color.White;
}

e.Graphics.FillRectangle(new SolidBrush(bg), e.Bounds);
if (e.State == DrawItemState.Selected)
e.Graphics.DrawString(this.listBox1.Items[e.Index].ToString(), e.Font, new SolidBrush(Color.White), e.Bounds);
else
e.Graphics.DrawString(this.listBox1.Items[e.Index].ToString(), e.Font, new SolidBrush(Color.Black), e.Bounds);

}
麻子Mozart 2009-06-10
  • 打赏
  • 举报
回复
up
lovvver 2009-06-10
  • 打赏
  • 举报
回复
其实你也可以自己实现的,写一个control,继承自ListBox,然后在它的OnPaint的方法里,对每一行进行自定义的处理。
qq904492758 2009-06-10
  • 打赏
  • 举报
回复
顶回帖是一种美德!每天回帖即可获得 10 分可用分!
满衣兄 2009-06-10
  • 打赏
  • 举报
回复
楼上的可以.我接.
feiyun0112 2009-06-10
  • 打赏
  • 举报
回复
http://www.codeproject.com/KB/combobox/colorlistbox.aspx

*****************************************************************************
欢迎使用CSDN论坛专用阅读器 : CSDN Reader(附全部源代码)

http://feiyun0112.cnblogs.com/
wzuomin 2009-06-10
  • 打赏
  • 举报
回复
哦,明白啦,少写了一句,呵呵。
lz试试看吧,是不是这个效果


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 WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
for (int i = 0; i < 10;i++ )
{
this.myListBox1.Items.Add("item" + i.ToString ());
}
}
}

public class MyListBox : System.Windows.Forms.ListBox
{

public MyListBox()
{
this.DrawMode = DrawMode.OwnerDrawVariable;
this.DrawItem += new DrawItemEventHandler(MyListBox_DrawItem);
}

private void MyListBox_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)
{
if (e.Index == -1) return;

Font txtFnt = new Font(this.Font.Name, this.Font.Size);

if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
{
txtFnt = new Font(this.Font.Name, this.Font.Size, FontStyle.Italic | FontStyle.Bold);
}

if (e.Index % 4 == 0 | e.Index % 4 == 1) {
e.Graphics.FillRectangle(Brushes.White, e.Bounds);
}
else {
e.Graphics.FillRectangle(Brushes.LightCyan, e.Bounds);
}

StringFormat SF = new StringFormat();
SF.LineAlignment = StringAlignment.Center;
e.Graphics.DrawString(" " + this.Items[e.Index].ToString(), txtFnt, Brushes.Blue, e.Bounds, SF);
e.DrawFocusRectangle();
}
}

}


wzuomin 2009-06-10
  • 打赏
  • 举报
回复

Public Class MyListBox
Inherits System.Windows.Forms.ListBox

Public Sub New()
Me.DrawMode = Windows.Forms.DrawMode.OwnerDrawVariable
End Sub

Private Sub MyListBox_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles Me.DrawItem
If e.Index = -1 Then Exit Sub
Dim txtFnt As Font = New Font(Me.Font.Name, Me.Font.Size)

If e.State And System.Windows.Forms.DrawItemState.Selected Then
txtFnt = New Font(Me.Font.Name, Me.Font.Size, FontStyle.Italic Or FontStyle.Bold)
End If

If e.Index Mod 4 = 0 Or e.Index Mod 4 = 1 Then
e.Graphics.FillRectangle(Brushes.White, e.Bounds)
Else
e.Graphics.FillRectangle(Brushes.LightCyan, e.Bounds)
End If

Dim SF As New StringFormat : SF.LineAlignment = StringAlignment.Center
e.Graphics.DrawString(" " + Items(e.Index).ToString, txtFnt, Brushes.Blue, e.Bounds, SF)
e.DrawFocusRectangle()
End Sub

Private Sub MyListBox_MeasureItem(ByVal sender As Object, ByVal e As System.Windows.Forms.MeasureItemEventArgs) Handles Me.MeasureItem
Dim itmSize As SizeF = e.Graphics.MeasureString(Items(e.Index).ToString, Me.Font, New Size(Me.Width, 400))
e.ItemHeight = itmSize.Height * 2 : e.ItemWidth = itmSize.Width
End Sub

End Class



以上代码在vb中可以实现lz的要求,转成c#代码后好像不正确,不知道怎么回事儿。



public class MyListBox : System.Windows.Forms.ListBox
{

public MyListBox()
{
this.DrawMode = DrawMode.OwnerDrawVariable;
}

private void MyListBox_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)
{
if (e.Index == -1) return;

Font txtFnt = new Font(this.Font.Name, this.Font.Size);

if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
{
txtFnt = new Font(this.Font.Name, this.Font.Size, FontStyle.Italic | FontStyle.Bold);
}

if (e.Index % 4 == 0 | e.Index % 4 == 1) {
e.Graphics.FillRectangle(Brushes.White, e.Bounds);
}
else {
e.Graphics.FillRectangle(Brushes.LightCyan, e.Bounds);
}

StringFormat SF = new StringFormat();
SF.LineAlignment = StringAlignment.Center;
e.Graphics.DrawString(" " + this.Items[e.Index].ToString(), txtFnt, Brushes.Blue, e.Bounds, SF);
e.DrawFocusRectangle();
}

private void MyListBox_MeasureItem(object sender, System.Windows.Forms.MeasureItemEventArgs e)
{
SizeF itmSize = e.Graphics.MeasureString(this.Items[e.Index].ToString(), this.Font, new Size(this.Width, 400));
e.ItemHeight =(int) itmSize.Height * 2;
e.ItemWidth = (int)itmSize.Width;
}
}
zenowolf 2009-06-10
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 h_w_king 的回复:]

this.listBox1.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawFixed;

private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
{
Color bg;


else
{
if (e.Index % 2 == 0)
bg = Color.Red;
else
bg = Color.White;
}


[/Quote]
这个可以参考。。推荐下
liwei84516 2009-06-10
  • 打赏
  • 举报
回复
问题没解决,我顶起
liwei84516 2009-06-10
  • 打赏
  • 举报
回复
我从来没学过c#,如果是c++,让我写一个继承类,很容易,但是c#的话,我不知道格式是什么样的?哪位大哥能不能发一个继承ListBox的模版给我,然后我自己再填充功能进去?最好能告诉我怎么调用这个新类!谢谢啊
jym2003 2009-06-10
  • 打赏
  • 举报
回复
新手來學習~
  • 打赏
  • 举报
回复
做过gridview的。。没做过listbox的。。

关注好了
liwei84516 2009-06-10
  • 打赏
  • 举报
回复
还是不会。没接触过c#

111,120

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧