c#winForm常用控件使用技巧

今天突然要写一些代码,发现其中的一些一些控件的使用很不灵活,现在我要把他们给积累起来,给我和需要的朋友一个方便!
不过呢,我现在还是一学生,如有高见请指教!
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;

   }

 

 

 

 

转载于:https://www.cnblogs.com/guozhiming2003/archive/2008/07/18/1246250.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值