ListBox: font and image
/* Professional Windows GUI Programming Using C# by Jay Glynn, Csaba Torok, Richard Conway, Wahid Choudhury, Zach Greenvoss, Shripad Kulkarni, Neil Whitlow Publisher: Peer Information ISBN: 1861007663 */ using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; namespace MyListBox { /// <summary> /// Summary description for MyListBox. /// </summary> public class MyListBox : System.Windows.Forms.Form { /// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.Container components = null; private System.Windows.Forms.Label label1; private System.Windows.Forms.Label label2; private System.Windows.Forms.Label label3; private System.Windows.Forms.ListBox VARIABLE; private System.Windows.Forms.ListBox FIXED; private System.Windows.Forms.ListBox SIMPLE; private System.Windows.Forms.ListBox MULTI_COLUMN; private System.Windows.Forms.Label label4; private System.Windows.Forms.ListBox MULTI_SELECTION; private System.Windows.Forms.Label label5; static int delta = 5 ; string[] ListItems = new string[7]; public MyListBox() { ListItems[0] = "Apples"; ListItems[1] = "Oranges"; ListItems[2] = "Mangoes"; ListItems[3] = "PineApple"; ListItems[4] = "Strawbverries"; ListItems[5] = "Bananas"; ListItems[6] = "GrapeFruit"; // // Required for Windows Form Designer support // InitializeComponent(); // // TODO: Add any constructor code after InitializeComponent call // } /// <summary> /// Clean up any resources being used. /// </summary> protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows Form Designer generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.label1 = new System.Windows.Forms.Label(); this.label2 = new System.Windows.Forms.Label(); this.label3 = new System.Windows.Forms.Label(); this.VARIABLE = new System.Windows.Forms.ListBox(); this.FIXED = new System.Windows.Forms.ListBox(); this.SIMPLE = new System.Windows.Forms.ListBox(); this.MULTI_COLUMN = new System.Windows.Forms.ListBox(); this.label4 = new System.Windows.Forms.Label(); this.MULTI_SELECTION = new System.Windows.Forms.ListBox(); this.label5 = new System.Windows.Forms.Label(); this.SuspendLayout(); // // label1 // this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0))); this.label1.Location = new System.Drawing.Point(208, 24); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(192, 23); this.label1.TabIndex = 1; this.label1.Text = "Fixed Owner Draw List Box"; // // label2 // this.label2.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0))); this.label2.Location = new System.Drawing.Point(24, 24); this.label2.Name = "label2"; this.label2.Size = new System.Drawing.Size(168, 23); this.label2.TabIndex = 2; this.label2.Text = "Simple List Box"; // // label3 // this.label3.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0))); this.label3.Location = new System.Drawing.Point(424, 24); this.label3.Name = "label3"; this.label3.Size = new System.Drawing.Size(208, 16); this.label3.TabIndex = 1; this.label3.Text = "Variable Owner Draw List Box"; // // VARIABLE // this.VARIABLE.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawVariable; this.VARIABLE.Location = new System.Drawing.Point(424, 56); this.VARIABLE.Name = "VARIABLE"; this.VARIABLE.Size = new System.Drawing.Size(200, 240); this.VARIABLE.TabIndex = 5; this.VARIABLE.MeasureItem += new System.Windows.Forms.MeasureItemEventHandler(this.VARIABLE_MeasureItem); this.VARIABLE.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.VARIABLE_DrawItem); this.VARIABLE.SelectedIndexChanged += new System.EventHandler(this.VARIABLE_SelectedIndexChanged); // // FIXED // this.FIXED.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawFixed; this.FIXED.Location = new System.Drawing.Point(208, 56); this.FIXED.Name = "FIXED"; this.FIXED.Size = new System.Drawing.Size(208, 238); this.FIXED.TabIndex = 4; this.FIXED.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.FIXED_DrawItem); this.FIXED.SelectedIndexChanged += new System.EventHandler(this.FIXED_SelectedIndexChanged); // // SIMPLE // this.SIMPLE.Location = new System.Drawing.Point(24, 56); this.SIMPLE.Name = "SIMPLE"; this.SIMPLE.Size = new System.Drawing.Size(176, 238); this.SIMPLE.TabIndex = 3; this.SIMPLE.SelectedIndexChanged += new System.EventHandler(this.SIMPLE_SelectedIndexChanged); // // MULTI_COLUMN // this.MULTI_COLUMN.Location = new System.Drawing.Point(24, 344); this.MULTI_COLUMN.MultiColumn = true; this.MULTI_COLUMN.Name = "MULTI_COLUMN"; this.MULTI_COLUMN.Size = new System.Drawing.Size(224, 69); this.MULTI_COLUMN.TabIndex = 6; this.MULTI_COLUMN.SelectedIndexChanged += new System.EventHandler(this.MULTI_COLUMN_SelectedIndexChanged); // // label4 // this.label4.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0))); this.label4.Location = new System.Drawing.Point(24, 320); this.label4.Name = "label4"; this.label4.Size = new System.Drawing.Size(168, 24); this.label4.TabIndex = 7; this.label4.Text = "Multi Column ListBox"; // // MULTI_SELECTION // this.MULTI_SELECTION.Location = new System.Drawing.Point(296, 344); this.MULTI_SELECTION.MultiColumn = true; this.MULTI_SELECTION.Name = "MULTI_SELECTION"; this.MULTI_SELECTION.SelectionMode = System.Windows.Forms.SelectionMode.MultiSimple; this.MULTI_SELECTION.Size = new System.Drawing.Size(200, 147); this.MULTI_SELECTION.TabIndex = 6; this.MULTI_SELECTION.SelectedIndexChanged += new System.EventHandler(this.MULTI_SELECTION_SelectedIndexChanged); // // label5 // this.label5.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0))); this.label5.Location = new System.Drawing.Point(296, 320); this.label5.Name = "label5"; this.label5.Size = new System.Drawing.Size(208, 24); this.label5.TabIndex = 7; this.label5.Text = "Multi Selection ListBox"; // // MyListBox // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(640, 493); this.Controls.AddRange(new System.Windows.Forms.Control[] { this.label4, this.MULTI_COLUMN, this.VARIABLE, this.FIXED, this.SIMPLE, this.label2, this.label1, this.label3, this.MULTI_SELECTION, this.label5}); this.Name = "MyListBox"; this.Text = "ListBox Style"; this.Load += new System.EventHandler(this.MyListBox_Load); this.ResumeLayout(false); } #endregion /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { Application.Run(new MyListBox()); } private void MyListBox_Load(object sender, System.EventArgs e) { // Set the height of the ListBox with OwnerDrawFixed property FIXED.ItemHeight = 35 ; // Populate all the ListBoxes for ( int i=0; i < ListItems.Length; i++) { MULTI_SELECTION.Items.Add(ListItems[i]); MULTI_COLUMN.Items.Add(ListItems[i]); SIMPLE.Items.Add(ListItems[i]); FIXED.Items.Add(ListItems[i]); VARIABLE.Items.Add(ListItems[i]); } } private void FIXED_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e) { //Select the Icon that you want to display Icon i = new Icon("TICK.ico"); // Get the Bounding rectangle Rectangle rc = new Rectangle(e.Bounds.X + delta , e.Bounds.Y + delta , e.Bounds.Width-10, e.Bounds.Height-delta); Console.WriteLine(e.State.ToString()); // Setup the stringformatting object StringFormat sf = new StringFormat(); sf.Alignment = StringAlignment.Center ; // Get the item text FIXED = (ListBox)sender ; string str = (string)FIXED.Items[e.Index]; // Draw the rectangle e.Graphics.DrawRectangle(new Pen(new SolidBrush(Color.Black) , 2) , rc); e.Graphics.FillRectangle(new SolidBrush(Color.White) , rc); // Check if the item is selected if ( e.State == ( DrawItemState.NoAccelerator | DrawItemState.NoFocusRect)) { // Paint the item that if not selected e.Graphics.FillRectangle(new SolidBrush(Color.White) , rc); e.Graphics.DrawString( str , new Font("Ariel" , 12) , new SolidBrush(Color.Black), rc ,sf); e.DrawFocusRectangle(); } else { // Paint the item accordingly if it is selected e.DrawFocusRectangle(); e.Graphics.FillRectangle(new SolidBrush(Color.LightYellow) , rc); e.Graphics.DrawIcon(i, e.Bounds.X , e.Bounds.Y+5); e.Graphics.DrawString( str , new Font("Ariel" , 12) , new SolidBrush(Color.Black), rc ,sf); } } private void VARIABLE_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e) { //Select the Icon that you want to display Icon i = new Icon("TICK.ico"); // Get the Bounding rectangle Rectangle rc = new Rectangle(e.Bounds.X + delta , e.Bounds.Y + delta , e.Bounds.Width-10, e.Bounds.Height-delta); Console.WriteLine(e.State.ToString()); // Setup the stringformatting object StringFormat sf = new StringFormat(); sf.Alignment = StringAlignment.Center ; // UnBox the sender VARIABLE = (ListBox)sender ; // Get the item text string str = (string)VARIABLE.Items[e.Index]; // Draw the rectangle e.Graphics.DrawRectangle(new Pen(new SolidBrush(Color.Black) , 2) , rc); // Fill the rectangle with white background. Default Item not selected e.Graphics.FillRectangle(new SolidBrush(Color.White) , rc); // Check if the item is selected if ( e.State == ( DrawItemState.NoAccelerator | DrawItemState.NoFocusRect)) { // Paint the item that if not selected e.Graphics.FillRectangle(new SolidBrush(Color.White) , rc); e.Graphics.DrawString( str , new Font("Ariel" , 12) , new SolidBrush(Color.Black), rc ,sf); e.DrawFocusRectangle(); } else { // Paint the item accordingly if it is selected e.DrawFocusRectangle(); e.Graphics.FillRectangle(new SolidBrush(Color.LightCyan) , rc); e.Graphics.DrawIcon(i, e.Bounds.X , e.Bounds.Y+5); e.Graphics.DrawString( str , new Font("Ariel" , 12) , new SolidBrush(Color.Black), rc ,sf); } } private void VARIABLE_MeasureItem(object sender, System.Windows.Forms.MeasureItemEventArgs e) { // For every second item in the VariableListBox, set the itemheight to 55 if ( e.Index %2 == 0 ) { e.ItemHeight = 55 ; } else { // For all other items set the items to 33 e.ItemHeight = 33 ; } } private void SIMPLE_SelectedIndexChanged(object sender, System.EventArgs e) { // Unbox the sender SIMPLE = (ListBox)sender ; // Get the position of the item selected int posSelected = SIMPLE.SelectedIndex ; // Get the item text string str = (string)SIMPLE.Items[posSelected]; MessageBox.Show("ITEM SELECTED FROM SIMPLE LIST BOX = " + str , "Simple ListBox"); } private void FIXED_SelectedIndexChanged(object sender, System.EventArgs e) { // Unbox the sender FIXED = (ListBox)sender ; // Get the position of the item selected int posSelected = FIXED.SelectedIndex ; // Get the item text string str = (string)FIXED.Items[posSelected]; MessageBox.Show("ITEM SELECTED FROM FIXED LIST BOX = " + str , "Fixed Height ListBox"); } private void VARIABLE_SelectedIndexChanged(object sender, System.EventArgs e) { // Unbox the sender VARIABLE = (ListBox)sender ; // Get the position of the item selected int posSelected = VARIABLE.SelectedIndex ; // Get the item text string str = (string) VARIABLE.Items[posSelected]; MessageBox.Show("ITEM SELECTED FROM VARIABLE LIST BOX = " + str , "Variable Height ListBox"); } private void MULTI_COLUMN_SelectedIndexChanged(object sender, System.EventArgs e) { // Unbox the sender MULTI_COLUMN = (ListBox)sender ; // Get the position of the item selected int posSelected = MULTI_COLUMN.SelectedIndex ; // Get the item text string str = (string) MULTI_COLUMN.Items[posSelected]; MessageBox.Show("ITEM SELECTED FROM MULTI_COLUM LIST BOX = " + str , "Multi Column"); } private void MULTI_SELECTION_SelectedIndexChanged(object sender, System.EventArgs e) { // Unbox the sender MULTI_SELECTION = (ListBox)sender ; string str = "" ; // Get the list of selected index's for ( int i=0; i < MULTI_SELECTION.SelectedIndices.Count; i++) { // Get the index int posSelected = MULTI_SELECTION.SelectedIndices[i]; // Get the item text at the index str = str + (string)MULTI_SELECTION.Items[posSelected] + "," ; } MessageBox.Show("ITEM SELECTED FROM MULTI_SELECTION LIST BOX = " + str , "Multi Selection"); } } }