using System; using System.Windows.Forms; using System.IO; public class TagPropertyExample : System.Windows.Forms.Form { ListView listView = new ListView(); public TagPropertyExample () { listView.Left = 10; listView.Top = 10; listView.Size = new System.Drawing.Size(292, 273); DirectoryInfo directory = new DirectoryInfo("C:"); FileInfo[] files = directory.GetFiles(); foreach (FileInfo file in files) { ListViewItem item = listView.Items.Add(file.Name); item.ImageIndex = 0; item.Tag = file; } this.Controls.Add(listView); } public static void Main(){ Application.Run(new TagPropertyExample()); } }
Author: coder
Extends ListViewItem
using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; public class Form1 : System.Windows.Forms.Form { private System.Windows.Forms.ListView lvCountries; private System.Windows.Forms.ImageList imgLarge; private System.Windows.Forms.ImageList imgSmall; private System.Windows.Forms.ComboBox cbView; private System.Windows.Forms.Label lblAbbreviation; private System.ComponentModel.IContainer components; public Form1() { InitializeComponent(); lvCountries.Items.Add(new CountryItem("United States", "US", "Dollar")); lvCountries.Items[0].ImageIndex = 0; lvCountries.Items.Add(new CountryItem("Great Britain", "GB", "Pound")); lvCountries.Items[1].ImageIndex = 1; lvCountries.Items.Add(new CountryItem("Canada", "CA", "Dollar")); lvCountries.Items[2].ImageIndex = 2; lvCountries.Items.Add(new CountryItem("Japan", "JP", "Yen")); lvCountries.Items[3].ImageIndex = 3; lvCountries.Items.Add(new CountryItem("Germany", "GM", "Deutch Mark")); lvCountries.Items[4].ImageIndex = 4; cbView.Items.Add(View.LargeIcon); cbView.Items.Add(View.SmallIcon); cbView.Items.Add(View.List); cbView.Items.Add(View.Details); cbView.SelectedIndex = 0; lvCountries.Columns.Add("Country",100, HorizontalAlignment.Left); lvCountries.Columns.Add("Currency",100, HorizontalAlignment.Left); } private void InitializeComponent() { this.components = new System.ComponentModel.Container(); this.lvCountries = new System.Windows.Forms.ListView(); this.imgLarge = new System.Windows.Forms.ImageList(this.components); this.imgSmall = new System.Windows.Forms.ImageList(this.components); this.cbView = new System.Windows.Forms.ComboBox(); this.lblAbbreviation = new System.Windows.Forms.Label(); this.SuspendLayout(); this.lvCountries.LargeImageList = this.imgLarge; this.lvCountries.Location = new System.Drawing.Point(24, 72); this.lvCountries.Name = "lvCountries"; this.lvCountries.Size = new System.Drawing.Size(256, 248); this.lvCountries.SmallImageList = this.imgSmall; this.lvCountries.TabIndex = 0; this.lvCountries.MouseUp += new System.Windows.Forms.MouseEventHandler(this.lvCountries_MouseUp); this.imgLarge.ImageSize = new System.Drawing.Size(48, 48); this.imgLarge.TransparentColor = System.Drawing.Color.Transparent; this.imgSmall.ColorDepth = System.Windows.Forms.ColorDepth.Depth16Bit; this.imgSmall.ImageSize = new System.Drawing.Size(16, 16); this.imgSmall.TransparentColor = System.Drawing.Color.Transparent; // // cbView // this.cbView.Location = new System.Drawing.Point(32, 32); this.cbView.Name = "cbView"; this.cbView.Size = new System.Drawing.Size(128, 21); this.cbView.TabIndex = 1; this.cbView.SelectedIndexChanged += new System.EventHandler(this.cbView_SelectedIndexChanged); // // lblAbbreviation // this.lblAbbreviation.Location = new System.Drawing.Point(176, 32); this.lblAbbreviation.Name = "lblAbbreviation"; this.lblAbbreviation.Size = new System.Drawing.Size(48, 23); this.lblAbbreviation.TabIndex = 2; // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(312, 339); this.Controls.Add(this.lblAbbreviation); this.Controls.Add(this.cbView); this.Controls.Add(this.lvCountries); this.Name = "Form1"; this.Text = "Form1"; this.ResumeLayout(false); } static void Main() { Application.Run(new Form1()); } private void cbView_SelectedIndexChanged(object sender, System.EventArgs e) { lvCountries.View = (View)cbView.SelectedItem; } private void lvCountries_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e) { if(lvCountries.View != View.Details) lblAbbreviation.Text = ((CountryItem)lvCountries.GetItemAt(e.X, e.Y)).CountryAbbreviation; else lblAbbreviation.Text = ((CountryItem)lvCountries.GetItemAt(5, e.Y)).CountryAbbreviation; } } public class CountryItem : System.Windows.Forms.ListViewItem { string countryName = ""; string countryAbbrev = ""; public CountryItem(string countryName, string countryAbbreviation, string currency) { countryName = countryName; countryAbbrev = countryAbbreviation; base.Text = countryName; base.SubItems.Add(currency); } public string CountryName { get {return countryName;} } public string CountryAbbreviation { get {return countryAbbrev;} } }
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
{
///
///
public class MyListBox : System.Windows.Forms.Form
{
///
///
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
//
}
///
///
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
///
/// the contents of this method with the code editor.
///
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
///
///
[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");
}
}
}
MyListBox.zip( 49 k)[/csharp]
CheckedListBox Demo 2
using System;
using System.Windows.Forms;
using System.Drawing;
using System.ComponentModel;
public class CheckedListBoxDemo:Form{
CheckedListBox FavLangs;
GroupBox grpControls;
Button AddValue;
Button EditValue;
Button DeleteValue;
Button ShowValues;
TextBox OldValue;
TextBox NewValue;
Label OldCaption;
Label NewCaption;
CheckBox chkAll;
public CheckedListBoxDemo(){
grpControls=new GroupBox();
grpControls.Text=”CheckedListBox Demo”;
AddValue=new Button();
AddValue.Text=”&Add”;
AddValue.Click+=new EventHandler(Add_Click);
EditValue=new Button();
EditValue.Text=”&Edit”;
EditValue.Click+=new EventHandler(Edit_Click);
DeleteValue=new Button();
DeleteValue.Text=”&Delete”;
DeleteValue.Click+=new EventHandler(Delete_Click);
ShowValues=new Button();
ShowValues.Text=”&Show”;
//ShowValues.Click+=new EventHandler(ShowValues_Click);
ShowValues.Click+=new EventHandler(Checked_Changed);
OldValue=new TextBox();
OldValue.ReadOnly=true;
NewValue=new TextBox();
OldCaption=new Label();
OldCaption.Text=”Old Value:”;
NewCaption=new Label();
NewCaption.Text=”New Value:”;
chkAll=new CheckBox();
chkAll.Text=”Check/UnCheck All”;
chkAll.CheckedChanged+= new EventHandler(Checked_Changed);
chkAll.Width=175;
OldCaption.Location=new Point(15,15);
PositionControl(OldCaption,OldValue,true);
PositionControl(OldCaption,NewCaption,false);
PositionControl(OldValue,NewValue,false);
PositionControl(NewCaption,AddValue,false);
PositionControl(AddValue,EditValue,true);
PositionControl(EditValue,DeleteValue,true);
PositionControl(DeleteValue,ShowValues,true);
PositionControl(AddValue,chkAll,false);
grpControls.Controls.AddRange(new Control[]{OldCaption,OldValue,NewCaption,NewValue,AddValue,EditValue,DeleteValue,ShowValues,chkAll});
grpControls.Size=new Size(450,200);
FavLangs=new CheckedListBox();
FavLangs.Location=new Point(10,10);
FavLangs.SelectedIndexChanged+=new EventHandler(SelectedIndex_Changed);
grpControls.Location=new Point(FavLangs.Left+FavLangs.Width+20,FavLangs.Top);
this.Controls.AddRange(new Control[]{FavLangs,grpControls});
}
private void PositionControl(Control source,Control destination,bool CanPlaceHorizontal)
{
if(CanPlaceHorizontal){
destination.Location=new Point(source.Left+source.Width+20,source.Top);
}else{
destination.Location=new Point(source.Left,source.Top+source.Height+20);
}
}
private void Add_Click(object sender,EventArgs e){
((Button)sender).Text = “aaa”;
if(NewValue.Text.Trim()!=””){
FavLangs.Items.Add(NewValue.Text);
}else{
MessageBox.Show(“Enter a Value to Add”);
}
}
private void SelectedIndex_Changed(object sender,EventArgs e){
OldValue.Text=FavLangs.Items[FavLangs.SelectedIndex].ToString();
}
private void Edit_Click(object sender,EventArgs e){
if(FavLangs.SelectedIndex==-1){
MessageBox.Show(“Select a Item to Edit”);
} else{
if(NewValue.Text.Trim()!=””){
FavLangs.Items[FavLangs.SelectedIndex]=NewValue.Text;
}
else
{
MessageBox.Show(“Enter a Value to Edit”);
}
}
}
private void Delete_Click(object sender,EventArgs e)
{
if(FavLangs.SelectedIndex!=-1)
{
FavLangs.Items.RemoveAt(FavLangs.SelectedIndex);
}
else
{
MessageBox.Show(“Select a Item to Delete”);
}
}
private void ShowValues_Click(object sender,EventArgs e){
string SelectedValues=”The following value(s) are Selected:
” + new String('-',48) + ”
“;
for(int i=0;i
ListBox Demo 2
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
public class ListBoxDemo : System.Windows.Forms.Form {
private System.ComponentModel.Container container;
private System.Windows.Forms.Button buttonAdd;
private System.Windows.Forms.Button buttonClose;
private System.Windows.Forms.Button buttonModify;
private System.Windows.Forms.Button buttonDelete;
private System.Windows.Forms.Button buttonMoveUp;
private System.Windows.Forms.Button buttonMoveDown;
private System.Windows.Forms.ListBox listbox;
private System.Windows.Forms.TextBox textbox;
private System.Windows.Forms.Label label;
private int nSelectedIndex;
//*********SIZE & LOCATION******************//
// COMPONENT – BUTTON(s) aligned along X-axis.
const int BUTTON_LENGTH = 50;
const int BUTTON_HEIGHT = 20;
const int FIRSTBUTTON_XPOS = 20;
const int FIRSTBUTTON_YPOS =220;
const int XSPACING = 70; // (Note: XSPACING >= BUTTON_LENGTH)
const int YSPACING = 0;
//COMPONENT – MOVE BUTTONS
const int MBUTTON_LENGTH = 20;
const int MBUTTON_HEIGHT = 20;
const int FIRSTMBUTTON_XPOS = 220;
const int FIRSTMBUTTON_YPOS =70;
const int SECONDMBUTTON_XPOS = 220;
const int SECONDMBUTTON_YPOS =100;
// COMPONENT – LISTBOX
const int LISTBOX_LENGTH = 3*BUTTON_LENGTH;
const int LISTBOX_HEIGHT = 6*BUTTON_HEIGHT;
const int LISTBOX_XPOS = 50;
const int LISTBOX_YPOS = 50;
// COMPONENT – LABEL
const int LABEL_LENGTH = 50;
const int LABEL_HEIGHT = 50;
const int LABEL_XPOS = 20; // align it with first button
const int LABEL_YPOS = 173;
// COMPONENT – TEXTBOX
const int TEXTBOX_LENGTH = 120;
const int TEXTBOX_HEIGHT = 50;
const int TEXTBOX_XPOS = 70;
const int TEXTBOX_YPOS = 170;
public ListBoxDemo() : base() {
InitializeComponent();
}
private void InitializeComponent() {
// this
this.container = new System.ComponentModel.Container();
this.Text=”List Box”;
// buttonAdd
this.buttonAdd = new System.Windows.Forms.Button();
buttonAdd.Location = new
System.Drawing.Point(FIRSTBUTTON_XPOS,FIRSTBUTTON_YPOS);
buttonAdd.Text = “&Add”;
buttonAdd.Size = new System.Drawing.Size(BUTTON_LENGTH,BUTTON_HEIGHT);
buttonAdd.Click += new System.EventHandler(this.buttonAdd_Click);
buttonAdd.Enabled = false;
this.Controls.Add(this.buttonAdd);
//buttonModify
this.buttonModify = new System.Windows.Forms.Button();
buttonModify.Location = new
System.Drawing.Point(FIRSTBUTTON_XPOS+XSPACING,FIRSTBUTTON_YPOS+YSPACING);
buttonModify.Text = “&Modify”;
buttonModify.Size = new System.Drawing.Size(BUTTON_LENGTH,BUTTON_HEIGHT);
buttonModify.Click += new System.EventHandler(this.buttonModify_Click);
buttonModify.Enabled = false;
this.Controls.Add(this.buttonModify);
//buttonDelete
this.buttonDelete = new System.Windows.Forms.Button();
buttonDelete.Location = new
System.Drawing.Point(FIRSTBUTTON_XPOS+2*XSPACING,FIRSTBUTTON_YPOS+2*YSPACING);
buttonDelete.Text = “&Delete”;
buttonDelete.Size = new System.Drawing.Size(BUTTON_LENGTH,BUTTON_HEIGHT);
buttonDelete.Enabled = false;
buttonDelete.Click += new System.EventHandler(this.buttonDelete_Click);
this.Controls.Add(this.buttonDelete);
// buttonClose
this.buttonClose = new System.Windows.Forms.Button();
buttonClose.Location = new
System.Drawing.Point(FIRSTBUTTON_XPOS+3*XSPACING,FIRSTBUTTON_YPOS+3*YSPACING);
buttonClose.Text = “&Close”;
buttonClose.Size = new System.Drawing.Size(BUTTON_LENGTH,BUTTON_HEIGHT);
buttonClose.Click += new System.EventHandler(this.buttonClose_Click);
this.Controls.Add(this.buttonClose);
// listbox
this.listbox = new System.Windows.Forms.ListBox();
listbox.Location = new System.Drawing.Point(LISTBOX_XPOS,LISTBOX_YPOS);
listbox.Size = new System.Drawing.Size(LISTBOX_LENGTH,LISTBOX_HEIGHT);
listbox.Click += new
System.EventHandler(this.listbox_SelectedIndexChanged);
listbox.BackColor = (Color)System.Drawing.SystemColors.Desktop;
this.Controls.Add(this.listbox);
// label
this.label = new System.Windows.Forms.Label();
label.Location = new System.Drawing.Point(LABEL_XPOS,LABEL_YPOS);
label.Size = new System.Drawing.Size(LABEL_LENGTH,LABEL_HEIGHT);
label.Text = “Enter:”;
this.Controls.Add(this.label);
// textbox
this.textbox = new System.Windows.Forms.TextBox();
textbox.Location = new System.Drawing.Point(TEXTBOX_XPOS,TEXTBOX_YPOS);
textbox.Click += new System.EventHandler(this.textbox_Click);
textbox.Size = new System.Drawing.Size(TEXTBOX_LENGTH,TEXTBOX_HEIGHT);
this.Controls.Add(this.textbox);
// buttonMoveUp
this.buttonMoveUp = new System.Windows.Forms.Button();
buttonMoveUp.Location = new
System.Drawing.Point(FIRSTMBUTTON_XPOS,FIRSTMBUTTON_YPOS);
buttonMoveUp.Text = “<";
buttonMoveUp.Size = new
System.Drawing.Size(MBUTTON_LENGTH,MBUTTON_HEIGHT);
buttonMoveUp.Click += new System.EventHandler(this.buttonMoveUp_Click);
buttonMoveUp.Enabled = false;
this.Controls.Add(this.buttonMoveUp);
// buttonMoveDown
this.buttonMoveDown = new System.Windows.Forms.Button();
buttonMoveDown.Location = new
System.Drawing.Point(SECONDMBUTTON_XPOS,SECONDMBUTTON_YPOS);
buttonMoveDown.Text = ">“;
buttonMoveDown.Size = new
System.Drawing.Size(MBUTTON_LENGTH,MBUTTON_HEIGHT);
buttonMoveDown.Click += new
System.EventHandler(this.buttonMoveDown_Click);
buttonMoveDown.Enabled = false;
this.Controls.Add(this.buttonMoveDown);
}
protected void textbox_Click(Object sender, System.EventArgs e) {
this.buttonAdd.Enabled = true;
if (this.listbox.Items.Count>0)
EnableAllListBoxButtons();
}
protected void listbox_SelectedIndexChanged(object sender, System.EventArgs e) {
nSelectedIndex = this.listbox.SelectedIndex;
string szSelected = (string)this.listbox.SelectedItem;
this.textbox.Text = szSelected;
}
protected void buttonAdd_Click(Object sender, System.EventArgs e) {
if (this.textbox.Text !=””) {
this.listbox.Items.Add(this.textbox.Text);
this.textbox.Text = “”;
EnableAllListBoxButtons();
}
}
protected void buttonModify_Click(Object sender, System.EventArgs e) {
this.listbox.Items[nSelectedIndex] = this.textbox.Text;
}
protected void buttonDelete_Click(Object sender, System.EventArgs e) {
nSelectedIndex = this.listbox.SelectedIndex;
this.listbox.Items.Remove(nSelectedIndex);
System.Console.WriteLine(“Remove fn does not work…”);
}
protected void buttonClose_Click(Object sender, System.EventArgs e) {
this.Close();
}
protected void buttonMoveUp_Click(Object sender, System.EventArgs e) {
if (this.listbox.SelectedIndex >0)
this.listbox.SelectedIndex–;
}
protected void buttonMoveDown_Click(Object sender, System.EventArgs e) {
if (this.listbox.SelectedIndex < this.listbox.Items.Count-1)
this.listbox.SelectedIndex++;
}
private void EnableAllListBoxButtons() {
this.buttonAdd.Enabled = true;
this.buttonModify.Enabled = true;
this.buttonDelete.Enabled = true;
this.buttonMoveUp.Enabled = true;
this.buttonMoveDown.Enabled = true;
}
[STAThread]
public static void Main(string[] args) {
Application.Run(new ListBoxDemo());
}
} // class
[/csharp]
ListBox and Metafile Enum
/* GDI+ Programming in C# and VB .NET by Nick Symmonds Publisher: Apress ISBN: 159059035X */ using System; using System.Drawing; using System.Drawing.Drawing2D; using System.Drawing.Imaging; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.Runtime.InteropServices; namespace MetaEnum_c { public class MetaEnum : System.Windows.Forms.Form { private System.ComponentModel.Container components = null; private System.Windows.Forms.ListBox LB; Metafile mf = new Metafile("mymeta.emf"); public MetaEnum() { InitializeComponent(); } protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } mf.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.LB = new System.Windows.Forms.ListBox(); this.SuspendLayout(); // // LB // this.LB.Location = new System.Drawing.Point(208, 232); this.LB.Name = "LB"; this.LB.Size = new System.Drawing.Size(168, 121); this.LB.TabIndex = 1; // // MetaEnum // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(392, 373); this.Controls.AddRange(new System.Windows.Forms.Control[] { this.LB}); this.MaximizeBox = false; this.MinimizeBox = false; this.Name = "MetaEnum"; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; this.Text = "MetaEnum"; this.Load += new System.EventHandler(this.MetaEnum_Load); this.ResumeLayout(false); } #endregion [STAThread] static void Main() { Application.Run(new MetaEnum()); } private void MetaEnum_Load(object sender, System.EventArgs e) { } protected override void OnPaint(PaintEventArgs e) { Graphics G = e.Graphics; G.EnumerateMetafile( mf, new Point( 50, 50 ), new Graphics.EnumerateMetafileProc(this.MetafileCallback) ); } // Define callback method. private bool MetafileCallback( EmfPlusRecordType recordType, int flags, int dataSize, IntPtr data, PlayRecordCallback callbackData) { LB.Items.Add(recordType); if ( dataSize > 0 ) { byte[] D = new byte[dataSize]; Marshal.Copy(data, D, 0, dataSize); mf.PlayRecord(recordType, flags, dataSize, D); } return true; } } } MetaEnum-c.zip( 1 k)
ListBox Objects
/* User Interfaces in C#: Windows Forms and Custom Controls by Matthew MacDonald Publisher: Apress ISBN: 1590590457 */ using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; namespace ListBoxObjects { /// <summary> /// Summary description for ListBoxObjects. /// </summary> public class ListBoxObjects : System.Windows.Forms.Form { internal System.Windows.Forms.CheckedListBox lstCustomers; /// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.Container components = null; public ListBoxObjects() { // // 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.lstCustomers = new System.Windows.Forms.CheckedListBox(); this.SuspendLayout(); // // lstCustomers // this.lstCustomers.Location = new System.Drawing.Point(8, 8); this.lstCustomers.Name = "lstCustomers"; this.lstCustomers.Size = new System.Drawing.Size(264, 196); this.lstCustomers.TabIndex = 2; // // ListBoxObjects // this.AutoScaleBaseSize = new System.Drawing.Size(5, 14); this.ClientSize = new System.Drawing.Size(280, 218); this.Controls.AddRange(new System.Windows.Forms.Control[] { this.lstCustomers}); this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0))); this.Name = "ListBoxObjects"; this.Text = "ListBox Objects"; this.Load += new System.EventHandler(this.ListBoxObjects_Load); this.ResumeLayout(false); } #endregion /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { Application.Run(new ListBoxObjects()); } private void ListBoxObjects_Load(object sender, System.EventArgs e) { lstCustomers.Items.Add(new Customer("Maurice", "Respighi", DateTime.Now)); lstCustomers.Items.Add(new Customer("Sam", "Digweed", DateTime.Now)); lstCustomers.Items.Add(new Customer("Faria", "Khan", DateTime.Now)); } } } public class Customer { public string FirstName; public string LastName; public DateTime BirthDate; public Customer() {} public Customer(string firstName, string lastName, DateTime birthDate) { this.FirstName = firstName; this.LastName = lastName; this.BirthDate = birthDate; } public override string ToString() { return FirstName + " " + LastName; } }