现在的位置: 首页 > 综合 > 正文

c#winForm常用控件使用技巧

2012年08月25日 ⁄ 综合 ⁄ 共 1215字 ⁄ 字号 评论关闭
今天突然要写一些代码,发现其中的一些一些控件的使用很不灵活,现在我要把他们给积累起来,给我和需要的朋友一个方便!
不过呢,我现在还是一学生,如有高见请指教!
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;

   }

 

 

 

 

抱歉!评论已关闭.