Từ 1 tới 2 trên tổng số 2 kết quả

Đề tài: Tạo Combobox có nhiều màu trên C#

  1. #1
    Ngày gia nhập
    09 2006
    Nơi ở
    /usr/share/.hack@
    Bài viết
    1,433

    Smile Tạo Combobox có nhiều màu trên C#

    Hình mẫu :
    Click vào hình ảnh để lấy hình ảnh lớn

Tên:		cbb1.png
Lần xem:	6
Size:		10.9 KB
ID:		5209

    Source

    Visual C# Code:
    1. using System;
    2. using System.Drawing;
    3. using System.Collections;
    4. using System.ComponentModel;
    5. using System.Windows.Forms;
    6. using System.Data;
    7.  
    8. namespace MyComboBox
    9. {
    10.   /// <summary>
    11.   /// Summary description for MyComboBox.
    12.   /// </summary>
    13.   public class MyComboBox : System.Windows.Forms.Form
    14.   {
    15.     private System.Windows.Forms.ComboBox comboBox1;
    16.     private System.Windows.Forms.Label label1;
    17.     private System.Windows.Forms.ComboBox comboBox2;
    18.     private System.Windows.Forms.Label label2;
    19.     ArrayList colorArray = new ArrayList() ;
    20.     ArrayList fontArray  = new ArrayList() ;
    21.     /// <summary>
    22.     /// Required designer variable.
    23.     /// </summary>
    24.     private System.ComponentModel.Container components = null;
    25.  
    26.     public MyComboBox()
    27.     {
    28.       //
    29.       // Required for Windows Form Designer support
    30.       //
    31.       InitializeComponent();
    32.  
    33.       //
    34.       // TODO: Add any constructor code after InitializeComponent call
    35.       //
    36.     }
    37.  
    38.     /// <summary>
    39.     /// Clean up any resources being used.
    40.     /// </summary>
    41.     protected override void Dispose( bool disposing )
    42.     {
    43.       if( disposing )
    44.       {
    45.         if (components != null)
    46.         {
    47.           components.Dispose();
    48.         }
    49.       }
    50.       base.Dispose( disposing );
    51.     }
    52.  
    53.     #region Windows Form Designer generated code
    54.     /// <summary>
    55.     /// Required method for Designer support - do not modify
    56.     /// the contents of this method with the code editor.
    57.     /// </summary>
    58.     private void InitializeComponent()
    59.     {
    60.       this.comboBox1 = new System.Windows.Forms.ComboBox();
    61.       this.label1 = new System.Windows.Forms.Label();
    62.       this.comboBox2 = new System.Windows.Forms.ComboBox();
    63.       this.label2 = new System.Windows.Forms.Label();
    64.       this.SuspendLayout();
    65.       //
    66.       // comboBox1
    67.       //
    68.       this.comboBox1.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawVariable;
    69.       this.comboBox1.ItemHeight = 25;
    70.       this.comboBox1.Location = new System.Drawing.Point(16, 40);
    71.       this.comboBox1.Name = "comboBox1";
    72.       this.comboBox1.Size = new System.Drawing.Size(264, 31);
    73.       this.comboBox1.TabIndex = 0;
    74.       this.comboBox1.SelectedIndexChanged += new System.EventHandler(this.MyItemSelected);
    75.       this.comboBox1.MeasureItem += new System.Windows.Forms.MeasureItemEventHandler(this.comboBox1_MeasureItem);
    76.       this.comboBox1.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.comboBox1_DrawItem);
    77.       //
    78.       // label1
    79.       //
    80.       this.label1.Location = new System.Drawing.Point(16, 16);
    81.       this.label1.Name = "label1";
    82.       this.label1.Size = new System.Drawing.Size(100, 16);
    83.       this.label1.TabIndex = 1;
    84.       this.label1.Text = "Font Combo Box";
    85.       //
    86.       // comboBox2
    87.       //
    88.       this.comboBox2.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawFixed;
    89.       this.comboBox2.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
    90.       this.comboBox2.ItemHeight = 20;
    91.       this.comboBox2.Location = new System.Drawing.Point(16, 104);
    92.       this.comboBox2.Name = "comboBox2";
    93.       this.comboBox2.Size = new System.Drawing.Size(264, 26);
    94.       this.comboBox2.TabIndex = 0;
    95.       this.comboBox2.SelectedIndexChanged += new System.EventHandler(this.MyItemSelected);
    96.       this.comboBox2.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.comboBox2_DrawItem);
    97.       //
    98.       // label2
    99.       //
    100.       this.label2.Location = new System.Drawing.Point(24, 80);
    101.       this.label2.Name = "label2";
    102.       this.label2.TabIndex = 2;
    103.       this.label2.Text = "Color Combo Box";
    104.       //
    105.       // MyComboBox
    106.       //
    107.       this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
    108.       this.ClientSize = new System.Drawing.Size(312, 157);
    109.       this.Controls.AddRange(new System.Windows.Forms.Control[] {
    110.                                       this.label2,
    111.                                       this.label1,
    112.                                       this.comboBox1,
    113.                                       this.comboBox2});
    114.       this.Name = "MyComboBox";
    115.       this.Text = "OwnerDraw ComboBox";
    116.       this.Load += new System.EventHandler(this.MyComboBox_Load);
    117.       this.ResumeLayout(false);
    118.  
    119.     }
    120.     #endregion
    121.  
    122.     /// <summary>
    123.     /// The main entry point for the application.
    124.     /// </summary>
    125.     [STAThread]
    126.     static void Main()
    127.     {
    128.       Application.Run(new MyComboBox());
    129.     }
    130.  
    131.     private void MyComboBox_Load(object sender, System.EventArgs e)
    132.     {  
    133.       // Fill an array with the different color you wish
    134.       // to display in the ComboBox
    135.  
    136.       colorArray.Add(new SolidBrush(Color.Yellow));
    137.       colorArray.Add(new SolidBrush(Color.Black));
    138.       colorArray.Add(new SolidBrush(Color.Azure));
    139.       colorArray.Add(new SolidBrush(Color.Firebrick));
    140.       colorArray.Add(new SolidBrush(Color.DarkMagenta));
    141.  
    142.       // Add blank items to the list, since the text we will display
    143.       // will be the name of the Color we are painting
    144.       comboBox2.Items.Add("");
    145.       comboBox2.Items.Add("");
    146.       comboBox2.Items.Add("");
    147.       comboBox2.Items.Add("");
    148.       comboBox2.Items.Add("");
    149.  
    150.    
    151.       // Fill an array with the different fonts that you will use to display
    152.       // items in the other comboBox
    153.  
    154.       fontArray .Add(new Font("Ariel" , 15 , FontStyle.Bold ));
    155.       fontArray .Add(new Font("Courier" , 12 , FontStyle.Italic));
    156.       fontArray .Add(new Font("Veranda" , 14 , FontStyle.Bold));
    157.       fontArray .Add(new Font("System" , 10 , FontStyle.Strikeout));
    158.       fontArray .Add(new Font("Century SchoolBook" , 15 , FontStyle.Underline));
    159.  
    160.       // Add the items that in the listBox
    161.  
    162.       comboBox1.Items.Add("Washington");
    163.       comboBox1.Items.Add("Houston");
    164.       comboBox1.Items.Add("Phoenix");
    165.       comboBox1.Items.Add("Dallas");
    166.       comboBox1.Items.Add("London");
    167.  
    168.     }
    169.  
    170.     private void comboBox1_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)
    171.     {
    172.       // Override this function to draw items in the Font comboBox
    173.  
    174.       // Get the Graphics Object (aka. CDC or Device Context Object )
    175.       // passed via the DrawItemEventArgs parameter
    176.       Graphics g = e.Graphics ;
    177.  
    178.       // Get the bounding rectangle of the item currently being painted
    179.       Rectangle r = e.Bounds ;
    180.       Font fn = null ;
    181.  
    182.       if ( e.Index >= 0 )
    183.       {
    184.         // Get the Font object, at the specifid index in the fontArray
    185.         fn = (Font)fontArray[e.Index];
    186.  
    187.         // Get the text that we wish to display
    188.         string s = (string)comboBox1.Items[e.Index];
    189.  
    190.         // Set the string format options
    191.         StringFormat sf = new StringFormat();
    192.         sf.Alignment = StringAlignment.Near;
    193.  
    194.         Console.WriteLine(e.State.ToString());
    195.         // Draw the rectangle
    196.         e.Graphics.DrawRectangle(new Pen(new SolidBrush(Color.Black), 2 ), r );
    197.  
    198.         if ( e.State == ( DrawItemState.NoAccelerator | DrawItemState.NoFocusRect))
    199.         {
    200.           // if the item is not selected draw it with a different color
    201.           e.Graphics.FillRectangle(new SolidBrush(Color.White) , r);
    202.           e.Graphics.DrawString( s , fn , new SolidBrush(Color.Black), r ,sf);
    203.           e.DrawFocusRectangle();
    204.         }
    205.         else
    206.         {
    207.           // if the item is selected draw it with a different color
    208.           e.Graphics.FillRectangle(new SolidBrush(Color.LightBlue) , r);
    209.           e.Graphics.DrawString( s , fn , new SolidBrush(Color.Red), r ,sf);
    210.           e.DrawFocusRectangle();
    211.         }
    212.       }
    213.     }
    214.  
    215.     private void comboBox2_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)
    216.     {
    217.       // Override this function to draw items in the Color comboBox
    218.  
    219.       // Get the Graphics Object (aka. CDC or Device Context Object )
    220.       // passed via the DrawItemEventArgs parameter
    221.       Graphics g = e.Graphics ;
    222.  
    223.       // Get the bounding rectangle of the item currently being painted
    224.       Rectangle r = e.Bounds ;
    225.  
    226.       if ( e.Index >= 0 )
    227.       {
    228.         Rectangle rd = r ;
    229.         rd.Width = 100 ;
    230.        
    231.         Rectangle rt = r ;
    232.         r.Left = rd.Right ;
    233.  
    234.         // Get the brush object, at the specifid index in the colorArray
    235.         SolidBrush b = (SolidBrush)colorArray[e.Index];
    236.         // Fill a portion of the rectangle with the selected brush
    237.         g.FillRectangle(b  , rd);
    238.  
    239.         // Set the string format options
    240.         StringFormat sf = new StringFormat();
    241.         sf.Alignment = StringAlignment.Near;
    242.  
    243.         Console.WriteLine(e.State.ToString());
    244.        
    245.         // Draw the rectangle
    246.         e.Graphics.DrawRectangle(new Pen(new SolidBrush(Color.Black), 2 ), r );
    247.  
    248.         if ( e.State == ( DrawItemState.NoAccelerator | DrawItemState.NoFocusRect))
    249.         {
    250.           // if the item is not selected draw it with a different color
    251.           e.Graphics.FillRectangle(new SolidBrush(Color.White) , r);
    252.           e.Graphics.DrawString( b.Color.Name, new Font("Ariel" , 8 , FontStyle.Bold  ) , new SolidBrush(Color.Black), r ,sf);
    253.           e.DrawFocusRectangle();
    254.         }
    255.         else
    256.         {
    257.           // if the item is selected draw it with a different color
    258.           e.Graphics.FillRectangle(new SolidBrush(Color.LightBlue) , r);
    259.           e.Graphics.DrawString( b.Color.Name, new Font("Veranda" , 12 , FontStyle.Bold  ) , new SolidBrush(Color.Red), r ,sf);
    260.           e.DrawFocusRectangle();
    261.         }
    262.       }
    263.     }
    264.  
    265.     private void MyItemSelected(object sender, System.EventArgs e)
    266.     {
    267.       // UnBox the sender. Since both the ComboBox controls use the same
    268.       // events to handle the selection of item from the list
    269.  
    270.       ComboBox cb = null ;
    271.       if ( sender.Equals(comboBox1))
    272.         cb = comboBox1;
    273.       else
    274.         cb = comboBox2;
    275.      
    276.       // Get the index of the Item Selected
    277.             int x = cb.SelectedIndex ;
    278.  
    279.       string str = "";
    280.       // Get the Text of the item selected
    281.       if ( sender.Equals(comboBox1))
    282.       {
    283.         str = "Item Selected is = " + (string)cb.Items[x];
    284.       }
    285.       else
    286.       {
    287.         SolidBrush br = (SolidBrush)colorArray[x];
    288.         str = "Color Selected is = " + br.Color.Name;
    289.       }
    290.      
    291.       MessageBox.Show(str,"ComboBox Item");
    292.     }
    293.  
    294.     private void comboBox1_MeasureItem(object sender, System.Windows.Forms.MeasureItemEventArgs e)
    295.     {
    296.       // For the comboBox with OwnerDrawVariable property
    297.       // Display every second item with a height of 45
    298.       if ( e.Index%2 == 0  )
    299.       {
    300.         e.ItemHeight = 45 ;
    301.         e.ItemWidth = 20 ;
    302.       }
    303.       else
    304.       {
    305.         // Display all other items with a height of 25
    306.         e.ItemHeight = 25 ;
    307.         e.ItemWidth = 10 ;
    308.       }
    309.     }
    310.   }
    311. }
    None!

  2. #2
    Ngày gia nhập
    06 2008
    Bài viết
    6

    Cái này có thể thêm 1 cột nữa để thể hiện mã màu được không bạn

Các đề tài tương tự

  1. làm sao tạo nhiểu cột cho combobox ???
    Gửi bởi vuonghnit90 trong diễn đàn Thắc mắc lập trình C#
    Trả lời: 5
    Bài viết cuối: 13-07-2012, 03:46 PM
  2. Tạo Combobox nhiều cột trên C# như thế nào?
    Gửi bởi lyngocho trong diễn đàn Thắc mắc lập trình C#
    Trả lời: 7
    Bài viết cuối: 19-11-2011, 08:31 PM
  3. Combobox trên C# | Khóa không cho nhập vào combobox trên C# như thế nào?
    Gửi bởi vanan3103 trong diễn đàn Thắc mắc lập trình C#
    Trả lời: 4
    Bài viết cuối: 15-08-2011, 04:22 PM
  4. Database bị lỗi gì khi load giá trị từ một bảng vào nhiều combobox?
    Gửi bởi nguyendongbb trong diễn đàn Thắc mắc lập trình C#
    Trả lời: 5
    Bài viết cuối: 05-08-2011, 12:33 PM
  5. Tạo Combobox trên Datagirdview | Làm thế nào add combobox vào datagrid với C#?
    Gửi bởi hvcuongit trong diễn đàn Thắc mắc lập trình C#
    Trả lời: 4
    Bài viết cuối: 10-04-2009, 05:12 PM

Quyền hạn của bạn

  • Bạn không thể gửi đề tài mới
  • Bạn không thể gửi bài trả lời
  • Bạn không thể gửi các đính kèm
  • Bạn không thể chỉnh sửa bài viết của bạn