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 listView1= new System.Windows.Forms.ListView();
private System.Windows.Forms.ListView listView2= new System.Windows.Forms.ListView();
private System.ComponentModel.Container components = null;
System.Windows.Forms.ListViewItem listViewItem1 = new System.Windows.Forms.ListViewItem("List 2 Item 1");
System.Windows.Forms.ListViewItem listViewItem2 = new System.Windows.Forms.ListViewItem("List 2 Item 2");
System.Windows.Forms.ListViewItem listViewItem3 = new System.Windows.Forms.ListViewItem("List 2 Item 3");
System.Windows.Forms.ListViewItem listViewItem4 = new System.Windows.Forms.ListViewItem("Item 1");
System.Windows.Forms.ListViewItem listViewItem5 = new System.Windows.Forms.ListViewItem("Item 2");
System.Windows.Forms.ListViewItem listViewItem6 = new System.Windows.Forms.ListViewItem("Item 3");
public Form1() {
this.SuspendLayout();
this.listView2.AllowDrop = true;
listViewItem1.UseItemStyleForSubItems = false;
listViewItem2.UseItemStyleForSubItems = false;
listViewItem3.UseItemStyleForSubItems = false;
this.listView2.Items.AddRange(new System.Windows.Forms.ListViewItem[]{
listViewItem1,listViewItem2,listViewItem3});
this.listView2.Location = new System.Drawing.Point(320, 24);
this.listView2.Size = new System.Drawing.Size(200, 216);
this.listView2.View = System.Windows.Forms.View.List;
this.listView2.DragDrop += new System.Windows.Forms.DragEventHandler(this.OnDragDrop);
this.listView2.DragEnter += new System.Windows.Forms.DragEventHandler(this.OnDragEnter);
this.listView1.AllowDrop = true;
listViewItem4.UseItemStyleForSubItems = false;
listViewItem5.UseItemStyleForSubItems = false;
listViewItem6.UseItemStyleForSubItems = false;
this.listView1.Items.AddRange(new System.Windows.Forms.ListViewItem[] { listViewItem4, listViewItem5, listViewItem6 });
this.listView1.Location = new System.Drawing.Point(40, 24);
this.listView1.Size = new System.Drawing.Size(200, 216);
this.listView1.View = System.Windows.Forms.View.List;
this.listView1.ItemDrag += new System.Windows.Forms.ItemDragEventHandler(this.OnItemDrag);
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(576, 273);
this.Controls.AddRange(new System.Windows.Forms.Control[] { this.listView2, this.listView1 });
this.ResumeLayout(false);
}
[STAThread]
static void Main() {
Application.Run(new Form1());
}
private void OnItemDrag(object sender, System.Windows.Forms.ItemDragEventArgs e) {
string s = e.Item.ToString();
DoDragDrop(s, DragDropEffects.Copy | DragDropEffects.Move);
}
private void OnDragEnter(object sender,
System.Windows.Forms.DragEventArgs e) {
if (e.Data.GetDataPresent(DataFormats.Text))
e.Effect = DragDropEffects.Copy;
else
e.Effect = DragDropEffects.None;
}
private void OnDragDrop(object sender, System.Windows.Forms.DragEventArgs e) {
string typestring = "Type";
string s = e.Data.GetData(typestring.GetType()).ToString();
string orig_string = s;
s = s.Substring(s.IndexOf(":") + 1).Trim();
s = s.Substring(1, s.Length - 2);
this.listView2.Items.Add(s);
IEnumerator enumerator = listView1.Items.GetEnumerator();
int whichIdx = -1;
int idx = 0;
while (enumerator.MoveNext()) {
string s2 = enumerator.Current.ToString();
if (s2.Equals(orig_string)) {
whichIdx = idx;
break;
}
idx++;
}
this.listView1.Items.RemoveAt(whichIdx);
}
}
Software Development
software development notes, main category for Cs, Java, and other programming experiences.
Windows Explorer-Like Program: extends ListView
using System;
using System.Diagnostics; // For Process.Start
using System.Drawing;
using System.IO;
using System.Windows.Forms;
class FileListView : ListView {
string strDirectory;
public FileListView() {
View = View.Details;
ImageList imglst = new ImageList();
imglst.Images.Add(new Bitmap(GetType(), “DOC.BMP”));
imglst.Images.Add(new Bitmap(GetType(), “EXE.BMP”));
SmallImageList = imglst;
LargeImageList = imglst;
Columns.Add(“Name”, 100, HorizontalAlignment.Left);
Columns.Add(“Size”, 100, HorizontalAlignment.Right);
Columns.Add(“Modified”, 100, HorizontalAlignment.Left);
Columns.Add(“Attribute”, 100, HorizontalAlignment.Left);
}
public void ShowFiles(string strDirectory) {
this.strDirectory = strDirectory;
Items.Clear();
DirectoryInfo dirinfo = new DirectoryInfo(strDirectory);
FileInfo[] afileinfo;
try {
afileinfo = dirinfo.GetFiles();
} catch {
return;
}
foreach (FileInfo fi in afileinfo) {
ListViewItem lvi = new ListViewItem(fi.Name);
if (Path.GetExtension(fi.Name).ToUpper() == “.EXE”)
lvi.ImageIndex = 1;
else
lvi.ImageIndex = 0;
lvi.SubItems.Add(fi.Length.ToString(“N0”));
lvi.SubItems.Add(fi.LastWriteTime.ToString());
string strAttr = “”;
if ((fi.Attributes & FileAttributes.Archive) != 0)
strAttr += “A”;
if ((fi.Attributes & FileAttributes.Hidden) != 0)
strAttr += “H”;
if ((fi.Attributes & FileAttributes.ReadOnly) != 0)
strAttr += “R”;
if ((fi.Attributes & FileAttributes.System) != 0)
strAttr += “S”;
lvi.SubItems.Add(strAttr);
Items.Add(lvi);
}
}
protected override void OnItemActivate(EventArgs ea) {
base.OnItemActivate(ea);
foreach (ListViewItem lvi in SelectedItems) {
try {
Process.Start(Path.Combine(strDirectory, lvi.Text));
} catch {
continue;
}
}
}
}
class ExplorerLike : Form {
FileListView filelist;
MenuItemView mivChecked;
public static void Main() {
Application.Run(new ExplorerLike());
}
public ExplorerLike() {
BackColor = SystemColors.Window;
ForeColor = SystemColors.WindowText;
filelist = new FileListView();
filelist.Parent = this;
filelist.Dock = DockStyle.Fill;
Splitter split = new Splitter();
split.Parent = this;
split.Dock = DockStyle.Left;
split.BackColor = SystemColors.Control;
Menu = new MainMenu();
Menu.MenuItems.Add(“&View”);
string[] astrView = { “Lar&ge Icons”, “S&mall Icons”,
“&List”, “&Details” };
View[] aview = { View.LargeIcon, View.SmallIcon,
View.List, View.Details };
EventHandler eh = new EventHandler(MenuOnView);
for (int i = 0; i < 4; i++) { MenuItemView miv = new MenuItemView(); miv.Text = astrView[i]; miv.View = aview[i]; miv.RadioCheck = true; miv.Click += eh; if (i == 3) // Default == View.Details { mivChecked = miv; mivChecked.Checked = true; filelist.View = mivChecked.View; } Menu.MenuItems[0].MenuItems.Add(miv); } Menu.MenuItems[0].MenuItems.Add("-"); MenuItem mi = new MenuItem("&Refresh", new EventHandler(MenuOnRefresh), Shortcut.F5); Menu.MenuItems[0].MenuItems.Add(mi); } void DirectoryTreeViewOnAfterSelect(object obj, TreeViewEventArgs tvea) { filelist.ShowFiles(tvea.Node.FullPath); } void MenuOnView(object obj, EventArgs ea) { mivChecked.Checked = false; mivChecked = (MenuItemView)obj; mivChecked.Checked = true; filelist.View = mivChecked.View; } void MenuOnRefresh(object obj, EventArgs ea) { } } class MenuItemView : MenuItem { public View View; } [/csharp]
Fill XML data to ListBox
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using System.Xml;
class Form1 : Form {
public Form1() {
InitializeComponent();
}
private void buttonLoopThroughDocument_Click(object sender, EventArgs e) {
listBoxXmlNodes.Items.Clear();
XmlDocument document = new XmlDocument();
document.Load("Books.xml");
RecurseXmlDocument((XmlNode)document.DocumentElement, 0);
}
private void RecurseXmlDocument(XmlNode root, int indent) {
if (root == null)
return;
if (root is XmlElement){
listBoxXmlNodes.Items.Add(root.Name.PadLeft(root.Name.Length + indent));
if (root.HasChildNodes)
RecurseXmlDocument(root.FirstChild, indent + 2);
if (root.NextSibling != null)
RecurseXmlDocument(root.NextSibling, indent);
} else if (root is XmlText) {
string text = ((XmlText)root).Value;
listBoxXmlNodes.Items.Add(text.PadLeft(text.Length + indent));
} else if (root is XmlComment) {
string text = root.Value;
listBoxXmlNodes.Items.Add(text.PadLeft(text.Length + indent));
if (root.HasChildNodes)
RecurseXmlDocument(root.FirstChild, indent + 2);
if (root.NextSibling != null)
RecurseXmlDocument(root.NextSibling, indent);
}
}
private void buttonCreateNode_Click(object sender, EventArgs e) {
XmlDocument document = new XmlDocument();
document.Load("Books.xml");
XmlElement root = document.DocumentElement;
XmlElement newBook = document.CreateElement("book");
XmlElement newTitle = document.CreateElement("title");
XmlElement newAuthor = document.CreateElement("author");
XmlElement newCode = document.CreateElement("code");
XmlText title = document.CreateTextNode("C#");
XmlText author = document.CreateTextNode("AAA");
XmlText code = document.CreateTextNode("1234567890");
XmlComment comment = document.CreateComment("comment");
newBook.AppendChild(comment);
newBook.AppendChild(newTitle);
newBook.AppendChild(newAuthor);
newBook.AppendChild(newCode);
newTitle.AppendChild(title);
newAuthor.AppendChild(author);
newCode.AppendChild(code);
root.InsertAfter(newBook, root.FirstChild);
document.Save("Books.xml");
}
private void buttonDeleteNode_Click(object sender, EventArgs e) {
XmlDocument document = new XmlDocument();
document.Load("Books.xml");
XmlElement root = document.DocumentElement;
if (root.HasChildNodes) {
XmlNode book = root.LastChild;
root.RemoveChild(book);
document.Save("Books.xml");
}
}
private void buttonSelect_Click(object sender, EventArgs e) {
XmlDocument document = new XmlDocument();
document.Load("Books.xml");
XmlElement root = document.DocumentElement;
XmlNodeList nodeList = root.SelectNodes("//book[@pages='1000']");
foreach (XmlNode n in nodeList) {
MessageBox.Show(n.InnerText);
}
}
private void InitializeComponent() {
this.buttonLoopThroughDocument = new System.Windows.Forms.Button();
this.listBoxXmlNodes = new System.Windows.Forms.ListBox();
this.buttonCreateNode = new System.Windows.Forms.Button();
this.buttonDeleteNode = new System.Windows.Forms.Button();
this.buttonSelect = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// buttonLoopThroughDocument
//
this.buttonLoopThroughDocument.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.buttonLoopThroughDocument.Location = new System.Drawing.Point(444, 13);
this.buttonLoopThroughDocument.Name = "buttonLoopThroughDocument";
this.buttonLoopThroughDocument.TabIndex = 0;
this.buttonLoopThroughDocument.Text = "Loop";
this.buttonLoopThroughDocument.Click += new System.EventHandler(this.buttonLoopThroughDocument_Click);
//
// listBoxXmlNodes
//
this.listBoxXmlNodes.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.listBoxXmlNodes.FormattingEnabled = true;
this.listBoxXmlNodes.Location = new System.Drawing.Point(13, 13);
this.listBoxXmlNodes.Name = "listBoxXmlNodes";
this.listBoxXmlNodes.Size = new System.Drawing.Size(424, 225);
this.listBoxXmlNodes.TabIndex = 1;
//
// buttonCreateNode
//
this.buttonCreateNode.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.buttonCreateNode.Location = new System.Drawing.Point(444, 43);
this.buttonCreateNode.Name = "buttonCreateNode";
this.buttonCreateNode.TabIndex = 2;
this.buttonCreateNode.Text = "Create Node";
this.buttonCreateNode.Click += new System.EventHandler(this.buttonCreateNode_Click);
//
// buttonDeleteNode
//
this.buttonDeleteNode.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.buttonDeleteNode.Location = new System.Drawing.Point(444, 73);
this.buttonDeleteNode.Name = "buttonDeleteNode";
this.buttonDeleteNode.TabIndex = 3;
this.buttonDeleteNode.Text = "Delete Node";
this.buttonDeleteNode.Visible = false;
this.buttonDeleteNode.Click += new System.EventHandler(this.buttonDeleteNode_Click);
//
// buttonSelect
//
this.buttonSelect.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.buttonSelect.Location = new System.Drawing.Point(444, 103);
this.buttonSelect.Name = "buttonSelect";
this.buttonSelect.TabIndex = 4;
this.buttonSelect.Text = "Select";
this.buttonSelect.Visible = false;
this.buttonSelect.Click += new System.EventHandler(this.buttonSelect_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(531, 250);
this.Controls.Add(this.buttonSelect);
this.Controls.Add(this.buttonDeleteNode);
this.Controls.Add(this.buttonCreateNode);
this.Controls.Add(this.listBoxXmlNodes);
this.Controls.Add(this.buttonLoopThroughDocument);
this.Name = "Form1";
this.Text = "Xml Nodes";
this.ResumeLayout(false);
}
private System.Windows.Forms.Button buttonLoopThroughDocument;
private System.Windows.Forms.ListBox listBoxXmlNodes;
private System.Windows.Forms.Button buttonCreateNode;
private System.Windows.Forms.Button buttonDeleteNode;
private System.Windows.Forms.Button buttonSelect;
[STAThread]
static void Main() {
Application.EnableVisualStyles();
Application.Run(new Form1());
}
}
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;
}
}
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 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]
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