因为需要使用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. (转)Python 3 collections.defaultdict() 与 dict的使用和区别

    原文:https://www.cnblogs.com/herbert/archive/2013/01/09/2852843.html 在Python里面有一个模块collections,解释是数据类型 ...

  2. 使用mysqldump备份单表数据,并使用navicat导出单表中部分字段到excel

    今天工作上遇到一个问题,客户需要将生产环境上数据库中用户表中的用户名.登录名.邮箱三个字段导出到excel中,查了一下,分两个步骤完成了任务 1. 使用mysqldump命令将生产环境的user表备份 ...

  3. python 代码的缩进位置决定执行部分

    通过近期的代码训练,我发现同样的一个语句,在同一行缩进的位置不同,决定的执行部分也不同.先给一个例子(我想打出1-100中的前20个偶数) 比较第一张图和第二张图.第一张截图实现了我想要的功能.可是当 ...

  4. 【树】Binary Tree Right Side View

    题目: Given a binary tree, imagine yourself standing on the right side of it, return the values of the ...

  5. Android Studio打开非本机项目比较慢的问题。

    使用Android Studio打开其他项目的时候,如果使用的AS版本.gradle不同的话,会在打开项目的时候下载gradle版本,网速不好的情况下回非常的慢. 解决方案: 1.将本机创建的AS项目 ...

  6. Android Studio打开项目提示找不到sdk路径的问题。

    问题如图: 这是由于所打开的项目不是本机创建的,所使用的sdk路径不一致所导致. 解决方案: 打开项目所在目录,找到local.properties文件并打开,发现sdk.dir=D\:\\Andro ...

  7. 工具类APP

    应用名称 工具S 英文名称 未填写 应用描述 工具类APP 英文描述 未填写 应用官网 this 应用图标  

  8. centos6 free 和 centos 7的free 的差异与对比

    目录 一 centos6 free 常用参数和含义 centos6 free 命令示例 free 值讲解 计算公式 二 centos7 free 常用的参数 centos7 free 命令示例 计算公 ...

  9. mongodb-mongotemplate进行地理坐标操作

    因为项目中使用的springboot + mongotemplate, 所以还是需要mongotemplate的操作方式 首先建立一个bean: package com.iwhere.easy.tra ...

  10. Linux C 遍历指定目录

    #include<stdio.h> //标准输入输出 #include<unistd.h> //各种系统调用 #include<dirent.h> //与目录有关的 ...