随笔 - 64  文章 - 0 评论 - 31 阅读 - 98355
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

今天突然要写一些代码,发现其中的一些一些控件的使用很不灵活,现在我要把他们给积累起来,给我和需要的朋友一个方便!
不过呢,我现在还是一学生,如有高见请指教!
listbox
     一.根据需要自己定义item的颜色
       1.首先要设置其DrawMode属性,设置DrawMode.OwnerDrawFixed 或 DrawMode.OwnerDrawVariable (有大小可变的项时使用)
       2.实现其DrawItem事件响应
我写的代码如下:
        private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
        {
            listBox1.DrawMode = DrawMode.OwnerDrawFixed;
            e.DrawBackground();  

            Brush myBrush = Brushes.Red;
            Brush otherBrush=Brushes.Black;
            if (e.Index == 2)
                e.Graphics.DrawString(listBox1.Items[e.Index].ToString(), e.Font, myBrush, e.Bounds, StringFormat.GenericDefault);
            else
                e.Graphics.DrawString(listBox1.Items[e.Index].ToString(), e.Font, otherBrush, e.Bounds, StringFormat.GenericDefault);
        }

注:e.DrawBackground();  一定要写哦,否则你都不知道自己选择了哪个的!

dataGridView

一.设置行颜色

1、首先设置selectionMode为FullRowSelect

2、设置AllowUserToAddRows属性为false(否则会发生索引错误的)

下面就是我写的代码了(省略了具体的应用设置,通过对.rows[e.RowIndex]的判断就可以了):

 DataGridViewCellStyle style = new DataGridViewCellStyle();

private void dataGridView1_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)
        {

          style.BackColor = Color.Red;
                dataGridView1.Rows[e.RowIndex].DefaultCellStyle = style;

   }

 

 

 

 

posted on   朝阳  阅读(1210)  评论(2编辑  收藏  举报
编辑推荐:
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
点击右上角即可分享
微信分享提示