因为需要使用DrawMode自行DrawItem,所以需要将DrawMode设置为OwnerDrawVarialbe或OwnerDrawFixed模式,代码如下:

 private void listBox_DrawItem(object sender, DrawItemEventArgs e)
{
if (e.Index >= 0)
{
e.DrawBackground();
Brush mybsh = Brushes.Black;
if (listBox4.Items[e.Index].ToString().IndexOf("[ERROR]") != -1)
{
mybsh = Brushes.Red;
}
// 焦点框
e.DrawFocusRectangle();
//文本
//e.Graphics.DrawString(listBox4.Items[e.Index].ToString(), e.Font, mybsh, e.Bounds, StringFormat.GenericDefault);
e.Graphics.DrawString(listBox4.Items[e.Index].ToString(), e.Font, mybsh, e.Bounds, null);
}
}

  

并对listbox增加事件:DrawItem事件设置为listBox_DrawItem

但是这样就无法使用默认方式出现水平滚动条,那么需要对listbox增加事件listbox_MeasureItem,代码如下:

private void listBox_MeasureItem(object sender, MeasureItemEventArgs e)
{
String lvsDisp = listBox.Items[e.Index].ToString();
SizeF lvSize = e.Graphics.MeasureString(lvsDisp, listBox.Font);
if (listBox.HorizontalExtent < lvSize.Width)
{
listBox.HorizontalExtent = (int)lvSize.Width + 10;
}
e.ItemHeight = (int)lvSize.Height;
e.ItemWidth = (int)lvSize.Width;
}

  

这样就可以自动出现水平滚动条了。

winform listbox 使用DrawMode使用OwnerDrawVarialbe或OwnerDrawFixed无水平滚动条的更多相关文章

  1. C# winform listBox中的项上下移动(转)

    C# winform listBox中的项上下移动 分类: C# winform2009-06-24 12:37 876人阅读 评论(0) 收藏 举报 winformc#object //上移节点   ...

  2. 发掘ListBox的潜力(一):自动调整横向滚动条宽度

    <自绘ListBox的两种效果>一文帖出之后,从反馈信息来看,大家对这种小技巧还是很认同.接下来我将继续围绕ListBox写一系列的文章,进一步发掘ListBox的潜力,其中包括:自动调整 ...

  3. 为MFC中的ListBox添加水平滚动条

    我们知道,MFC中的水平滚动条并不像垂直滚动条那样的智能.当文字超出ListBox的宽度时,水平滚动条并不会自己出现,我们需要手动的调用CListBox中的函数SetHorizontalExtent设 ...

  4. (七)对话框,单选框(radiobox),复选框(checkbox),列表框(ListBox),组合框(CComboBox),水平滚动条(Horizontal scroll bar),微调(旋转)spincontrol,列表视图控件CListCtrl,静态控件static

    1,模态对话框和非模态对话框 // 模态对话框 void CMainFrame::OnDialogExec() { // TODO: 在此添加命令处理程序代码 // 创建对话框对象 CDialog d ...

  5. C# winform Listbox添加和删除items

    两个listbox添加和删除items #region 添加/移除 //添加 private void btnAdd_Click(object sender, EventArgs e) { ) { r ...

  6. C# winform ListBox实现滚动条自动滚动至底部

    效果如图: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data ...

  7. winform listbox增加鼠标双击事件

    在Form.Designer.cs文件中对于listBox处理: listBox.MouseDoubleClick += new system.Windows.Forms.MouseEventHand ...

  8. WinForm ListBox 控件用法

    下面演示如何利用列表控件 ListBox 实现多选与移动选项: using IMS.WinFormClient.UserControls; using System; using System.Col ...

  9. Winform ListBox输出信息并自动滚动至底部

    应用场景:ListBox作为软件信息的输出框. //ListBox输出信息 internal void SetListBoxMessage(string str) { if (this.Message ...

随机推荐

  1. 【转】linux下杀死进程(kill)的N种方法

    转载一篇,最原始的出处已不可考,望见谅! 常规篇: 首先,用ps查看进程,方法如下: $ ps -ef ……smx       1822     1  0 11:38 ?        00:00:4 ...

  2. 【java排序】冒泡排序、快速排序

    冒泡排序 冒泡排序是一种简单的排序算法.它重复地走访过要排序的数列,一次比较两个元素,如果他们的顺序错误就把他们交换过来.走访数列的工作是重复地 进行直到没有再需要交换,也就是说该数列已经排序完成.这 ...

  3. 机器学习--boosting家族之Adaboost算法

    最近在系统研究集成学习,到Adaboost算法这块,一直不能理解,直到看到一篇博文,才有种豁然开朗的感觉,真的讲得特别好,原文地址是(http://blog.csdn.net/guyuealian/a ...

  4. 玩转mongodb(六):索引,速度的引领(普通索引篇)

    数据库索引与书籍的索引类似,有了索引就不需要翻整本书,数据库可以直接在索引中查找,在索引中找到条目后,就可以直接跳到目标文档的位置,这可以让查找的速度提高几个数量级. 一.创建索引 我们在person ...

  5. 小白学习之Code First(五)

    Fluent API:另一种配置实体类的方式,它比DataAnnorations提供了更多的配置 其中EntityTypeConfiguration这个类提供了重要的属性.方法来配置对应的实体类,可以 ...

  6. DotNet Core 2.0部署后外网IP访问

    将DotNet Core2.0项目部署在Ubuntu上并且运行后,可以用localhost:5000来访问. 但是如果这时候用外网来访问就不行了. 这时候就有两种解决方案,第一种是用Nginx做代理实 ...

  7. 构造用于1个方法在多线程环境下重复多次执行测试的通用方法 C#

    static void MultithreadsCycleTestWithSameMethod(int threadNumber, int cycleNumber, ThreadStart actio ...

  8. oracle 合并列的函数wm_concat

    oracle wm_concat(column)函数使我们经常会使用到的,下面就教您如何使用oracle wm_concat(column)函数实现字段合并,如果您对oracle wm_concat( ...

  9. DHU 1788 Chinese remainder theorem again 中国剩余定理

    Chinese remainder theorem again Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 ...

  10. ASP.NET MVC中你必须知道的13个扩展点

         ScottGu在其最新的博文中推荐了Simone Chiaretta的文章13 ASP.NET MVC extensibility points you have to know,该文章为我 ...