close

image

加入小圖案目錄夾至專案

image

命名資料夾bmp

image

加入->現有項目

image

複選a~d四張bmp照片

image

宣告全域變數

private ImageList G_ImageList;

滑鼠double click專案中Properties資料夾, 選擇<資源>, <加入資源>下拉點選

image

<加入資源>下拉點選<加入現有檔案>

image

image

加入後如下:

image

========================================================================

加入控制元件ComboxBox和Button

image

   1: private void button1_Click(object sender, EventArgs e)
   2: {
   3:     comboBox1.Items.Add("car");
   4:     comboBox1.Items.Add("trunk");
   5:     comboBox1.Items.Add("tool");
   6:     comboBox1.Items.Add("friend");
   7: }

image

加入繪圖

   1: private void button1_Click(object sender, EventArgs e)
   2:    {
   3:        comboBox1.Items.Add("car");
   4:        comboBox1.Items.Add("trunk");
   5:        comboBox1.Items.Add("tool");
   6:        comboBox1.Items.Add("friend");
   7:        
   8:        //============================================================
   9:        comboBox1.DrawMode = DrawMode.OwnerDrawFixed;//設定繪製元素方式
  10:        comboBox1.DropDownStyle =                    //設定組合框樣式
  11:            ComboBoxStyle.DropDownList;
  12:        //============================================================
  13:        G_ImageList = new ImageList();//建立ImageList物件
  14:  
  15:        G_ImageList.Images.Add(Properties.Resources.a);
  16:        G_ImageList.Images.Add(Properties.Resources.b);
  17:        G_ImageList.Images.Add(Properties.Resources.c);
  18:        G_ImageList.Images.Add(Properties.Resources.d);
  19:    }

加入繪圖ImageList反而ComboBox項目都不見了

image

這是因為DrawMode已經改由OwnerDrawFixed的關係, 點選ComboBox1事件DrawItem加入繪圖功能, 框架如下

   1: private void comboBox1_DrawItem(object sender, DrawItemEventArgs e)
   2: {
   3:     if (G_ImageList != null)
   4:     {
   5:  
   6:     }
   7: }


   1: private void comboBox1_DrawItem(object sender, DrawItemEventArgs e)
   2:         {
   3:             if (G_ImageList != null)
   4:             {
   5:                 Graphics g = e.Graphics;               //得到繪圖物件
   6:                 Rectangle rec = e.Bounds;              //得到繪圖範圍
   7:                 Size imageSize = G_ImageList.ImageSize;//取得圖像大小
   8:                 if (e.Index >= 0)                      //判斷是否有繪製項 
   9:                 {
  10:                     Font font = new Font("微軟正黑體", 10, FontStyle.Bold);//建立字體物件
  11:                     string s = comboBox1.Items[e.Index].ToString();        //得到繪製項的字串
  12:                     DrawItemState dis = e.State;
  13:                     if (e.State == (DrawItemState.NoAccelerator | DrawItemState.NoFocusRect))
  14:                     {
  15:                         g.FillRectangle(new SolidBrush(Color.LightBlue), rec); //畫item背景
  16:                         G_ImageList.Draw(g, rec.Left, rec.Top, e.Index);                //繪製圖像
  17:                         e.Graphics.DrawString(s, font, new SolidBrush(Color.Black),     //顯示字串
  18:                             rec.Left + imageSize.Width, rec.Top);
  19:                         e.DrawFocusRectangle();                                         //顯示取得焦點時的虛線框
  20:                     }
  21:                     else
  22:                     {
  23:                         g.FillRectangle(new SolidBrush(Color.LightGreen), rec);//畫item背景
  24:                         G_ImageList.Draw(e.Graphics, rec.Left, rec.Top, e.Index);       //繪製圖像
  25:                         e.Graphics.DrawString(s, font, new SolidBrush(Color.Black),     //顯示字串 
  26:                             rec.Left + imageSize.Width, rec.Top);
  27:                         e.DrawFocusRectangle();                                         //顯示取得焦點時的虛線框 
  28:                     }
  29:                 }
  30:             }
  31:         }

執行如下

image

====================================================================

private bool State = false;//定義一個全局變數標識

image

   1: private void comboBox1_KeyDown(object sender, KeyEventArgs e)
   2: {
   3:     State = (e.KeyCode != Keys.Back && e.KeyCode != Keys.Delete);//當按鍵既不是Back鍵又不是Delete鍵時
   4:     comboBox1.DroppedDown = true;                                //當有按鍵被按下時顯示下拉列表
   5: }

當使用者輸入字時

   1: private void comboBox1_TextChanged(object sender, EventArgs e)
   2: {
   3:     if (State)
   4:     {
   5:         string inputTxt = comboBox1.Text;          //取得輸入的文字
   6:         int ind = comboBox1.FindString(inputTxt);  //在ComboBox集合中搜尋匹配的文字
   7:         if (ind >= 0)                              //當有匹配時
   8:         {
   9:             State = false;                         //關閉編輯狀態
  10:             comboBox1.SelectedIndex = ind;         //找到對應項
  11:             State = true;                          //打開編輯狀態
  12:             comboBox1.Select(inputTxt.Length, comboBox1.Text.Length);//設定文字的選擇長度
  13:         }
  14:     }
  15: }

如果comboBox無法輸入字, 表示唯讀

記得遮蔽先前定義的DropDownStyle即可輸入

   1: //comboBox1.DropDownStyle =                    //設定組合框樣式(變唯讀)
   2:            //    ComboBoxStyle.DropDownList;
arrow
arrow
    全站熱搜
    創作者介紹
    創作者 me1237guy 的頭像
    me1237guy

    天天向上

    me1237guy 發表在 痞客邦 留言(0) 人氣()