c#winForm常用控制項提示

來源:互聯網
上載者:User
今天突然要寫一些代碼,發現其中的一些一些控制項的使用很不靈活,現在我要把他們給積累起來,給我和需要的朋友一個方便!
不過呢,我現在還是一學生,如有高見請指教!
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;

   }

 

 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.