C# ListBox控件中item换行 C#重绘ListBox项

WindowsForm项目开发中,Listbox控件item数据项,只能一条数据显示在一行,有的时候内容很长,体验就非常之差。简直要歇菜了。哈哈。。。不开玩笑了。下面讲下怎么实现吧!

新建一个Winform项目,命名为ListAutoline,拖一个按钮(用来填充数据用的,触发吧),一个ListBox控件 . 如图;

弄完之后呢  将listbox属性项:  把这两个属性设置成如图所示;Enabled可设置可不设置;看个人了; DramMode主要是控制列表框绘制,系统用户绘制每项;可以理解为启用人工绘制的意思啦;

弄玩上面之后,咱们来看一下怎么绘制这个玩意吧。

先手动做几条数据吧 搞个Datatable去装数据;代码如下

 Font nfont;Color colorn;int leftMargines;int topMargines;int distance;Font tfont;DataTable ListData;/// <summary>/// 初始化数据/// </summary>private void FillDataTable(){ListData = new DataTable("List");//ListData.Columns.Add("Caption", System.Type.GetType("System.String"));ListData.Columns.Add("Text", System.Type.GetType("System.String"));ListData.Rows.Add(new object[] { //"datagram ", "工站:过谁丢发货未付哈尔发我份那份辣口味发文分蘖UI违法哈维佛阿文hi服务。" });ListData.Rows.Add(new object[] { //"digital signature ","过谁丢发货未付哈尔发我份那份辣口味发文分蘖UI违法哈维佛阿文hi服务请检查连接P上传"});ListData.Rows.Add(new object[]{ //"Digital Signature Standard ","过谁丢发货未付哈尔发我份那份辣口味发文分蘖UI违法哈维佛阿文hi服务 "});ListData.Rows.Add(new object[] { // "Distinguished Encoding Rules  ","过谁丢发货未付哈尔发我份那份辣口味发文分蘖UI违法哈维佛阿文hi服务28;CellId[3]:FXP0290DGWFE90228;"});ListData.Rows.Add(new object[] { //"electronic codebook  ", "P过谁丢发货未付哈尔发我份那份辣口味发文分蘖UI违法哈维佛阿文hi服务,请检查 " });}

然后在Button事件下面这样写; 就是调用那个FillDataTable();再加个ID啥的;

  private void button1_Click(object sender, EventArgs e){this.FillDataTable();this.listBox1.Enabled = false;this.listBox1.Items.Clear();for (int i = 0; i <= ListData.Rows.Count - 1; i++){this.listBox1.Items.Add(i);//this.listBox1.Items.Add(i + ListData.Rows[i]["Text"].ToString());}this.listBox1.Enabled = true;}

做完以上操作之后,选中Listbox  查看事件;

新增两个事件; 还有

新建这两个事件;开始重新绘制Listbox;

代码如下:

  private void listBox1_MeasureItem(object sender, MeasureItemEventArgs e){Graphics gfx = e.Graphics;int number = e.Index + 1;//string xcaption = ListData.Rows[e.Index]["Caption"].ToString();string dtext = ListData.Rows[e.Index]["Text"].ToString();SizeF f1 = gfx.MeasureString(number.ToString() + ".", nfont);// SizeF f11 = gfx.MeasureString(xcaption, nfont);Rectangle rect = new Rectangle(leftMargines + (int)f1.Width + 8, topMargines + (int)f1.Height + distance, this.listBox1.ClientSize.Width - leftMargines - (int)f1.Width - 8, e.ItemHeight);StringFormat stf = new StringFormat();stf.FormatFlags = StringFormatFlags.FitBlackBox;SizeF f2 = gfx.MeasureString(dtext, tfont, rect.Width, stf);int Temp = e.ItemWidth;//if (f2.Width < (leftMargines + f1.Width + 8 + f11.Width)) //    Temp = leftMargines + (int)f1.Width + 8 + (int)f11.Width + 4;if (f2.Width < (leftMargines + f1.Width + 8))Temp = leftMargines + (int)f1.Width + 8 + 4;//行间距e.ItemHeight = topMargines + (int)f1.Height + distance + (int)f2.Height + 8 + 8;//上下高度}private void listBox1_DrawItem(object sender, DrawItemEventArgs e){int number = e.Index + 1;//string xcaption = ListData.Rows[e.Index]["Caption"].ToString();string dtext = ListData.Rows[e.Index]["Text"].ToString();if ((e.State & DrawItemState.Selected) == DrawItemState.Selected){Graphics gfx = e.Graphics;Pen pen = new Pen(Brushes.SteelBlue, 0.4f);gfx.DrawRectangle(pen, e.Bounds.X + 4, e.Bounds.Y + 4, e.Bounds.Width - 8, e.Bounds.Height - 8);gfx.FillRectangle(Brushes.LightSteelBlue, e.Bounds.X + 5, e.Bounds.Y + 5, e.Bounds.Width - 9, e.Bounds.Height - 9);gfx.DrawString(number.ToString() + ".", nfont, Brushes.Black, e.Bounds.X + leftMargines, e.Bounds.Y + topMargines);SizeF f1 = gfx.MeasureString(number.ToString() + ".", nfont);//gfx.DrawString(xcaption, nfont, Brushes.SteelBlue, e.Bounds.X + leftMargines + f1.Width + 8, e.Bounds.Y + topMargines);//SizeF f11 = gfx.MeasureString(xcaption, nfont);Rectangle rect = new Rectangle(e.Bounds.X + leftMargines + (int)f1.Width + 8, e.Bounds.Y + topMargines + (int)f1.Height + distance, this.listBox1.ClientSize.Width - leftMargines - (int)f1.Width - 8, this.ClientSize.Height);StringFormat stf = new StringFormat();stf.FormatFlags = StringFormatFlags.FitBlackBox;gfx.DrawString(dtext, tfont, Brushes.Black, rect, stf);}else{Graphics gfx = e.Graphics;gfx.FillRectangle(Brushes.White, e.Bounds.X + 4, e.Bounds.Y + 4, e.Bounds.Width - 7, e.Bounds.Height - 7);gfx.FillRectangle(Brushes.SteelBlue, e.Bounds.X + 4, e.Bounds.Y + e.Bounds.Height - 8, e.Bounds.Width - 8, 1);Pen pen = new Pen(Brushes.SteelBlue, 0.4f);gfx.DrawString(number.ToString() + ".", nfont, Brushes.Black, e.Bounds.X + leftMargines, e.Bounds.Y + topMargines);SizeF f1 = gfx.MeasureString(number.ToString() + ".", nfont);//gfx.DrawString(xcaption, nfont, Brushes.SteelBlue, e.Bounds.X + leftMargines + f1.Width + 8, e.Bounds.Y + topMargines);//SizeF f11 = gfx.MeasureString(xcaption, nfont);Rectangle rect = new Rectangle(e.Bounds.X + leftMargines + (int)f1.Width + 8, e.Bounds.Y + topMargines + (int)f1.Height + distance, this.listBox1.ClientSize.Width - leftMargines - (int)f1.Width - 8, this.ClientSize.Height);StringFormat stf = new StringFormat();stf.FormatFlags = StringFormatFlags.FitBlackBox;gfx.DrawString(dtext, tfont, Brushes.Black, rect, stf);}e.DrawFocusRectangle();}

然后在窗体Load事件中写入以下代码:

 private void ListAutoLine_Load(object sender, EventArgs e){nfont = new Font("Microsoft Sans Serif", 10, FontStyle.Bold);tfont = new Font("Microsoft Sans Serif", 10, FontStyle.Regular);colorn = Color.Black;leftMargines = 8;topMargines = 10;distance = 12;}

新建窗体SizeChange事件:

 private void ListAutoLine_SizeChanged(object sender, EventArgs e){listBox1.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawFixed;listBox1.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawVariable;}

好了,以上代码就实现了ListBox自动换了;写个事件测试测试;

  private void listBox1_Click(object sender, EventArgs e){MessageBox.Show("当前选中-[" + listBox1.SelectedItem.ToString().Trim() + "]-项");MessageBox.Show(getListText(listBox1.SelectedItem.ToString().Trim()));}

基本上是没有什么问题的啦;

效果图为:

就这样子了;

以上就是本期全部内容了;如有不当之处,还请指正,感谢;欢迎留言讨论;

C# ListBox控件中item换行 C#重绘ListBox项相关推荐

  1. vb.net将excel表格数据填加到Listbox控件中并去重复选项

    Dim xlApp As Object = NothingDim xlBooks As Excel.Workbooks = NothingDim xlBook As Excel.Workbook = ...

  2. python listbox控件_Python GUI编程(Tkinter)10、Listbox控件

    import tkinter win = tkinter.Tk() win.title("sunck") win.geometry("400x400+200+0" ...

  3. matlab listbox选中输出,vba中ListBox控件的使用

    给ListBox添加内容 If CheckBox8 = True Then---------------------------checkbox控件被选中 For i = 0 To ListBox1. ...

  4. ListBox 控件

    ListBox 控件又称列表框,它在工具箱中的图标为,它显示一个项目列表供用户选择.在列表框中,用户一次可以选择一项,也可以选择多项. 1.常用属性: (1) Items属性: 用于存放列表框中的列表 ...

  5. C#ListBox控件

    ListBox 控件又称列表框,它显示一个项目列表供用户选择.在列表框中,用户一次可以选择一项,也可以选择多项. 1.常用属性: (1) Items属性: 用于存放列表框中的列表项,是一个集合.通过该 ...

  6. C# ListBox 控件

    ListBox 控件又称列表框,它显示一个项目列表供用户选择.在列表框中,用户 一次可以选择一项,也可以选择多项. 1.常用属性: (1)Items属性: 用于存放列表框中的列表项, 是一个集合.通过 ...

  7. ListBox 控件

    ListBox 控件 ListBox 控件又称列表框,它显示一个项目列表供用户选择.在列表框中,用户 一次可以选择一项,也可以选择多项. 1.常用属性: (1) Items属性: 用于存放列表框中的列 ...

  8. 说说wps jsa的ListBox控件的数组写入方法

    目前wps jsa的控件API只能说吐槽!再吐槽!目前很多WPS JS宏教程都不太愿意谈WPS的控件,也是因为WPS控件的功能太差,简直差到极点了. 目前小编很想用WPS的ListBox控件来做一些应 ...

  9. asp.net Listbox控件用法

    2008-02-18 19:56 来源: 作者: ListBox(列表框)控件可以显示一组项目的列表,用户可以根据需要从中选择一个或多个选项.列表框可以为用户提供所有选项的列表.虽然也可设置列表框为多 ...

最新文章

  1. 宗成庆:如何撰写毕业论文?
  2. 配置PIM auto-rp
  3. (*长期更新)软考网络工程师学习笔记——Section 9 应用层
  4. Trident State译文
  5. linux下运行python_在Linux命令行终端中使用python的简单方法(推荐)
  6. tp5写的系统比php源码写的慢多少,基于TP5框架开发的极速企业网站开发框架PHP源码...
  7. python自定义包的发布与安装
  8. 会装64位solaris jdk1.6吗?
  9. 实用ExtJS教程100例-001:开天辟地的Hello World
  10. 【编程软件】keli烧录代码点击Download或者Debug界面卡死
  11. 乒乓球单循环赛_乒乓球循环赛制比赛规则
  12. 洛谷P1640 [SCOI2010]连续攻击游戏 (二分图) HQG_AC 的博客
  13. HTML5期末大作业:旅游网站设计——北京旅游(1页) 北京旅游网页设计制作 简单静态HTML网页作品 我的旅游网页作业成品 学生旅游网站模板
  14. vue3中瀑布流插件 vue-masonry 的使用
  15. 系统的延时任务和定时任务
  16. 262-我的大学竞选的事迹材料
  17. Eolink: 一站式 API 生产力工具
  18. 无法从服务器中获取信息吗,无法从服务器获取信息
  19. Android最全的屏幕适配
  20. 一号店首页代码需其他的联系我

热门文章

  1. 如何用android赚钱以及几个发布android原生app的是市场地址
  2. 加密pdf在不知道密码的情况下如何解密
  3. “中国风色彩域名“.ren溢价词销售大盘点,更多域名等你来淘
  4. 【第86题】JAVA高级技术-网络编程5(断点续传)
  5. 绿色屏幕键盘监控专家【安全相关】
  6. 个人简历英文词汇大全
  7. 个人总结-----疯狂猜成语小组成员赵静娜
  8. 教师节:你有三行情书,我有三行代码!
  9. php损坏的图像,php – 复制后图像损坏
  10. P1177 【模板】快速排序