ListBox: font and image : ListBox « GUI Windows Form « C# / C Sharp

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Design Patterns
8.Development Class
9.Event
10.File Stream
11.Generics
12.GUI Windows Form
13.Language Basics
14.LINQ
15.Network
16.Office
17.Reflection
18.Regular Expressions
19.Security
20.Services Event
21.Thread
22.Web Services
23.Windows
24.Windows Presentation Foundation
25.XML
26.XML LINQ
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source
C# / C Sharp » GUI Windows Form » ListBoxScreenshots 
ListBox: font and image
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 = ;
    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 Disposebool disposing )
    {
      ifdisposing )
      {
        if (components != null
        {
          components.Dispose();
        }
      }
      base.Disposedisposing );
    }

    #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(20824);
      this.label1.Name = "label1";
      this.label1.Size = new System.Drawing.Size(19223);
      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(2424);
      this.label2.Name = "label2";
      this.label2.Size = new System.Drawing.Size(16823);
      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(42424);
      this.label3.Name = "label3";
      this.label3.Size = new System.Drawing.Size(20816);
      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(42456);
      this.VARIABLE.Name = "VARIABLE";
      this.VARIABLE.Size = new System.Drawing.Size(200240);
      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(20856);
      this.FIXED.Name = "FIXED";
      this.FIXED.Size = new System.Drawing.Size(208238);
      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(2456);
      this.SIMPLE.Name = "SIMPLE";
      this.SIMPLE.Size = new System.Drawing.Size(176238);
      this.SIMPLE.TabIndex = 3;
      this.SIMPLE.SelectedIndexChanged += new System.EventHandler(this.SIMPLE_SelectedIndexChanged);
      // 
      // MULTI_COLUMN
      // 
      this.MULTI_COLUMN.Location = new System.Drawing.Point(24344);
      this.MULTI_COLUMN.MultiColumn = true;
      this.MULTI_COLUMN.Name = "MULTI_COLUMN";
      this.MULTI_COLUMN.Size = new System.Drawing.Size(22469);
      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(24320);
      this.label4.Name = "label4";
      this.label4.Size = new System.Drawing.Size(16824);
      this.label4.TabIndex = 7;
      this.label4.Text = "Multi Column ListBox";
      // 
      // MULTI_SELECTION
      // 
      this.MULTI_SELECTION.Location = new System.Drawing.Point(296344);
      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(200147);
      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(296320);
      this.label5.Name = "label5";
      this.label5.Size = new System.Drawing.Size(20824);
      this.label5.TabIndex = 7;
      this.label5.Text = "Multi Selection ListBox";
      // 
      // MyListBox
      // 
      this.AutoScaleBaseSize = new System.Drawing.Size(513);
      this.ClientSize = new System.Drawing.Size(640493);
      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.Black2, 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.DrawStringstr , new Font("Ariel" 12new 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.DrawStringstr , new Font("Ariel" 12new 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.Black2, 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.DrawStringstr , new Font("Ariel" 12new 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.DrawStringstr , new Font("Ariel" 12new 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 %== 
      {
        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 = (stringVARIABLE.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 = (stringMULTI_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");
    }
  }
}


           
       
MyListBox.zip( 49 k)
Related examples in the same category
1.Add new item to ListBox (text from TextBox) Add new item to ListBox (text from TextBox)
2.Remove item if one is selected from ListBoxRemove item if one is selected from ListBox
3.Clear all items in a ListBoxClear all items in a ListBox
4.ListBox selected Item changed eventListBox selected Item changed event
5.Add Object to ListBoxAdd Object to ListBox
6.List Box click eventList Box click event
7.Set TopIndex to auto scroll ListBoxSet TopIndex to auto scroll ListBox
8.Form with list, buttonForm with list, button
9.CheckedListBox Demo 2CheckedListBox Demo 2
10.ListBox Demo 2ListBox Demo 2
11.ListBox and Metafile EnumListBox and Metafile Enum
12.ListBox ObjectsListBox Objects
13.Fill XML data to ListBox
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.