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()); } }
Author: coder
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]
Editable Binding
/* 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 ADO.NET_Binding { /// <summary> /// Summary description for EditableBinding. /// </summary> public class EditableBinding : System.Windows.Forms.Form { internal System.Windows.Forms.Label Label4; internal System.Windows.Forms.GroupBox GroupBox1; internal System.Windows.Forms.Label Label3; internal System.Windows.Forms.Label Label2; internal System.Windows.Forms.Label Label1; internal System.Windows.Forms.TextBox txtModelName; internal System.Windows.Forms.TextBox txtDescription; internal System.Windows.Forms.TextBox txtUnitCost; internal System.Windows.Forms.TextBox txtModelNumber; internal System.Windows.Forms.ComboBox cboModelName; /// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.Container components = null; public EditableBinding() { // // 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.Label4 = new System.Windows.Forms.Label(); this.GroupBox1 = new System.Windows.Forms.GroupBox(); this.Label3 = new System.Windows.Forms.Label(); this.Label2 = new System.Windows.Forms.Label(); this.Label1 = new System.Windows.Forms.Label(); this.txtModelName = new System.Windows.Forms.TextBox(); this.txtDescription = new System.Windows.Forms.TextBox(); this.txtUnitCost = new System.Windows.Forms.TextBox(); this.txtModelNumber = new System.Windows.Forms.TextBox(); this.cboModelName = new System.Windows.Forms.ComboBox(); this.GroupBox1.SuspendLayout(); this.SuspendLayout(); // // Label4 // this.Label4.Location = new System.Drawing.Point(16, 17); this.Label4.Name = "Label4"; this.Label4.Size = new System.Drawing.Size(88, 16); this.Label4.TabIndex = 20; this.Label4.Text = "Select a Record:"; // // GroupBox1 // this.GroupBox1.Anchor = (((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right); this.GroupBox1.Controls.AddRange(new System.Windows.Forms.Control[] { this.Label3, this.Label2, this.Label1, this.txtModelName, this.txtDescription, this.txtUnitCost, this.txtModelNumber}); this.GroupBox1.Location = new System.Drawing.Point(16, 53); this.GroupBox1.Name = "GroupBox1"; this.GroupBox1.Size = new System.Drawing.Size(400, 252); this.GroupBox1.TabIndex = 19; this.GroupBox1.TabStop = false; // // Label3 // this.Label3.Location = new System.Drawing.Point(220, 56); this.Label3.Name = "Label3"; this.Label3.Size = new System.Drawing.Size(36, 16); this.Label3.TabIndex = 18; this.Label3.Text = "Cost:"; // // Label2 // this.Label2.Location = new System.Drawing.Point(16, 56); this.Label2.Name = "Label2"; this.Label2.Size = new System.Drawing.Size(52, 16); this.Label2.TabIndex = 17; this.Label2.Text = "Model:"; // // Label1 // this.Label1.Location = new System.Drawing.Point(16, 28); this.Label1.Name = "Label1"; this.Label1.Size = new System.Drawing.Size(52, 16); this.Label1.TabIndex = 16; this.Label1.Text = "Name:"; // // txtModelName // this.txtModelName.Location = new System.Drawing.Point(68, 24); this.txtModelName.Name = "txtModelName"; this.txtModelName.Size = new System.Drawing.Size(316, 21); this.txtModelName.TabIndex = 15; this.txtModelName.Text = ""; // // txtDescription // this.txtDescription.Location = new System.Drawing.Point(12, 92); this.txtDescription.Multiline = true; this.txtDescription.Name = "txtDescription"; this.txtDescription.Size = new System.Drawing.Size(372, 116); this.txtDescription.TabIndex = 14; this.txtDescription.Text = ""; // // txtUnitCost // this.txtUnitCost.Location = new System.Drawing.Point(256, 52); this.txtUnitCost.Name = "txtUnitCost"; this.txtUnitCost.Size = new System.Drawing.Size(128, 21); this.txtUnitCost.TabIndex = 13; this.txtUnitCost.Text = ""; // // txtModelNumber // this.txtModelNumber.Location = new System.Drawing.Point(68, 52); this.txtModelNumber.Name = "txtModelNumber"; this.txtModelNumber.Size = new System.Drawing.Size(136, 21); this.txtModelNumber.TabIndex = 12; this.txtModelNumber.Text = ""; // // cboModelName // this.cboModelName.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; this.cboModelName.Location = new System.Drawing.Point(108, 13); this.cboModelName.Name = "cboModelName"; this.cboModelName.Size = new System.Drawing.Size(308, 21); this.cboModelName.TabIndex = 18; // // EditableBinding // this.AutoScaleBaseSize = new System.Drawing.Size(5, 14); this.ClientSize = new System.Drawing.Size(432, 318); this.Controls.AddRange(new System.Windows.Forms.Control[] { this.Label4, this.GroupBox1, this.cboModelName}); this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0))); this.Name = "EditableBinding"; this.Text = "EditableBinding"; this.Load += new System.EventHandler(this.EditableBinding_Load); this.GroupBox1.ResumeLayout(false); this.ResumeLayout(false); } #endregion private void EditableBinding_Load(object sender, System.EventArgs e) { DataSet dsStore = new DataSet(); dsStore.ReadXmlSchema(Application.StartupPath + "store.xsd"); dsStore.ReadXml(Application.StartupPath + "store.xml"); cboModelName.DataSource = dsStore.Tables["Products"]; cboModelName.DisplayMember = "ModelName"; txtModelName.DataBindings.Add("Text", dsStore.Tables["Products"], "ModelName"); txtModelNumber.DataBindings.Add("Text", dsStore.Tables["Products"], "ModelNumber"); txtUnitCost.DataBindings.Add("Text", dsStore.Tables["Products"], "UnitCost"); txtDescription.DataBindings.Add("Text", dsStore.Tables["Products"], "Description"); } [STAThread] static void Main() { Application.Run(new EditableBinding()); } } } ADO.NETBinding.zip( 76 k)
Add new item to ListBox (text from TextBox)
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; public class Form1 : Form { private System.Windows.Forms.ListBox displayListBox; private System.Windows.Forms.TextBox inputTextBox; private System.Windows.Forms.Button addButton; private System.Windows.Forms.Button removeButton; private System.Windows.Forms.Button clearButton; public Form1() { InitializeComponent(); } private void addButton_Click( object sender, EventArgs e ) { displayListBox.Items.Add( inputTextBox.Text ); inputTextBox.Clear(); } private void removeButton_Click( object sender, EventArgs e ) { if ( displayListBox.SelectedIndex != -1 ) displayListBox.Items.RemoveAt( displayListBox.SelectedIndex ); } private void clearButton_Click( object sender, EventArgs e ) { displayListBox.Items.Clear(); } private void InitializeComponent() { this.displayListBox = new System.Windows.Forms.ListBox(); this.inputTextBox = new System.Windows.Forms.TextBox(); this.addButton = new System.Windows.Forms.Button(); this.removeButton = new System.Windows.Forms.Button(); this.clearButton = new System.Windows.Forms.Button(); this.SuspendLayout(); // // displayListBox // this.displayListBox.FormattingEnabled = true; this.displayListBox.Location = new System.Drawing.Point(13, 12); this.displayListBox.Name = "displayListBox"; this.displayListBox.Size = new System.Drawing.Size(119, 238); this.displayListBox.TabIndex = 0; // // inputTextBox // this.inputTextBox.Location = new System.Drawing.Point(149, 12); this.inputTextBox.Name = "inputTextBox"; this.inputTextBox.Size = new System.Drawing.Size(100, 20); this.inputTextBox.TabIndex = 1; // // addButton // this.addButton.Location = new System.Drawing.Point(149, 56); this.addButton.Name = "addButton"; this.addButton.Size = new System.Drawing.Size(100, 36); this.addButton.TabIndex = 2; this.addButton.Text = "Add"; this.addButton.Click += new System.EventHandler(this.addButton_Click); // // removeButton // this.removeButton.Location = new System.Drawing.Point(149, 109); this.removeButton.Name = "removeButton"; this.removeButton.Size = new System.Drawing.Size(100, 36); this.removeButton.TabIndex = 3; this.removeButton.Text = "Remove"; this.removeButton.Click += new System.EventHandler(this.removeButton_Click); // // clearButton // this.clearButton.Location = new System.Drawing.Point(149, 165); this.clearButton.Name = "clearButton"; this.clearButton.Size = new System.Drawing.Size(100, 36); this.clearButton.TabIndex = 4; this.clearButton.Text = "Clear"; this.clearButton.Click += new System.EventHandler(this.clearButton_Click); // ListBoxTestForm // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(263, 268); this.Controls.Add(this.clearButton); this.Controls.Add(this.removeButton); this.Controls.Add(this.addButton); this.Controls.Add(this.inputTextBox); this.Controls.Add(this.displayListBox); this.Name = "ListBoxTestForm"; this.Text = "ListBoxTest"; this.ResumeLayout(false); this.PerformLayout(); } [STAThread] static void Main() { Application.EnableVisualStyles(); Application.Run(new Form1()); } }
Remove item if one is selected from ListBox
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; public class Form1 : Form { private System.Windows.Forms.ListBox displayListBox; private System.Windows.Forms.TextBox inputTextBox; private System.Windows.Forms.Button addButton; private System.Windows.Forms.Button removeButton; private System.Windows.Forms.Button clearButton; public Form1() { InitializeComponent(); } private void addButton_Click( object sender, EventArgs e ) { displayListBox.Items.Add( inputTextBox.Text ); inputTextBox.Clear(); } private void removeButton_Click( object sender, EventArgs e ) { if ( displayListBox.SelectedIndex != -1 ) displayListBox.Items.RemoveAt( displayListBox.SelectedIndex ); } private void clearButton_Click( object sender, EventArgs e ) { displayListBox.Items.Clear(); } private void InitializeComponent() { this.displayListBox = new System.Windows.Forms.ListBox(); this.inputTextBox = new System.Windows.Forms.TextBox(); this.addButton = new System.Windows.Forms.Button(); this.removeButton = new System.Windows.Forms.Button(); this.clearButton = new System.Windows.Forms.Button(); this.SuspendLayout(); // // displayListBox // this.displayListBox.FormattingEnabled = true; this.displayListBox.Location = new System.Drawing.Point(13, 12); this.displayListBox.Name = "displayListBox"; this.displayListBox.Size = new System.Drawing.Size(119, 238); this.displayListBox.TabIndex = 0; // // inputTextBox // this.inputTextBox.Location = new System.Drawing.Point(149, 12); this.inputTextBox.Name = "inputTextBox"; this.inputTextBox.Size = new System.Drawing.Size(100, 20); this.inputTextBox.TabIndex = 1; // // addButton // this.addButton.Location = new System.Drawing.Point(149, 56); this.addButton.Name = "addButton"; this.addButton.Size = new System.Drawing.Size(100, 36); this.addButton.TabIndex = 2; this.addButton.Text = "Add"; this.addButton.Click += new System.EventHandler(this.addButton_Click); // // removeButton // this.removeButton.Location = new System.Drawing.Point(149, 109); this.removeButton.Name = "removeButton"; this.removeButton.Size = new System.Drawing.Size(100, 36); this.removeButton.TabIndex = 3; this.removeButton.Text = "Remove"; this.removeButton.Click += new System.EventHandler(this.removeButton_Click); // // clearButton // this.clearButton.Location = new System.Drawing.Point(149, 165); this.clearButton.Name = "clearButton"; this.clearButton.Size = new System.Drawing.Size(100, 36); this.clearButton.TabIndex = 4; this.clearButton.Text = "Clear"; this.clearButton.Click += new System.EventHandler(this.clearButton_Click); // ListBoxTestForm // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(263, 268); this.Controls.Add(this.clearButton); this.Controls.Add(this.removeButton); this.Controls.Add(this.addButton); this.Controls.Add(this.inputTextBox); this.Controls.Add(this.displayListBox); this.Name = "ListBoxTestForm"; this.Text = "ListBoxTest"; this.ResumeLayout(false); this.PerformLayout(); } [STAThread] static void Main() { Application.EnableVisualStyles(); Application.Run(new Form1()); } }
Clear all items in a ListBox
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; public class Form1 : Form { private System.Windows.Forms.ListBox displayListBox; private System.Windows.Forms.TextBox inputTextBox; private System.Windows.Forms.Button addButton; private System.Windows.Forms.Button removeButton; private System.Windows.Forms.Button clearButton; public Form1() { InitializeComponent(); } private void addButton_Click( object sender, EventArgs e ) { displayListBox.Items.Add( inputTextBox.Text ); inputTextBox.Clear(); } private void removeButton_Click( object sender, EventArgs e ) { if ( displayListBox.SelectedIndex != -1 ) displayListBox.Items.RemoveAt( displayListBox.SelectedIndex ); } private void clearButton_Click( object sender, EventArgs e ) { displayListBox.Items.Clear(); } private void InitializeComponent() { this.displayListBox = new System.Windows.Forms.ListBox(); this.inputTextBox = new System.Windows.Forms.TextBox(); this.addButton = new System.Windows.Forms.Button(); this.removeButton = new System.Windows.Forms.Button(); this.clearButton = new System.Windows.Forms.Button(); this.SuspendLayout(); // // displayListBox // this.displayListBox.FormattingEnabled = true; this.displayListBox.Location = new System.Drawing.Point(13, 12); this.displayListBox.Name = "displayListBox"; this.displayListBox.Size = new System.Drawing.Size(119, 238); this.displayListBox.TabIndex = 0; // // inputTextBox // this.inputTextBox.Location = new System.Drawing.Point(149, 12); this.inputTextBox.Name = "inputTextBox"; this.inputTextBox.Size = new System.Drawing.Size(100, 20); this.inputTextBox.TabIndex = 1; // // addButton // this.addButton.Location = new System.Drawing.Point(149, 56); this.addButton.Name = "addButton"; this.addButton.Size = new System.Drawing.Size(100, 36); this.addButton.TabIndex = 2; this.addButton.Text = "Add"; this.addButton.Click += new System.EventHandler(this.addButton_Click); // // removeButton // this.removeButton.Location = new System.Drawing.Point(149, 109); this.removeButton.Name = "removeButton"; this.removeButton.Size = new System.Drawing.Size(100, 36); this.removeButton.TabIndex = 3; this.removeButton.Text = "Remove"; this.removeButton.Click += new System.EventHandler(this.removeButton_Click); // // clearButton // this.clearButton.Location = new System.Drawing.Point(149, 165); this.clearButton.Name = "clearButton"; this.clearButton.Size = new System.Drawing.Size(100, 36); this.clearButton.TabIndex = 4; this.clearButton.Text = "Clear"; this.clearButton.Click += new System.EventHandler(this.clearButton_Click); // ListBoxTestForm // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(263, 268); this.Controls.Add(this.clearButton); this.Controls.Add(this.removeButton); this.Controls.Add(this.addButton); this.Controls.Add(this.inputTextBox); this.Controls.Add(this.displayListBox); this.Name = "ListBoxTestForm"; this.Text = "ListBoxTest"; this.ResumeLayout(false); this.PerformLayout(); } [STAThread] static void Main() { Application.EnableVisualStyles(); Application.Run(new Form1()); } }
ListBox selected Item changed event
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; public class Form1 : Form { private System.Windows.Forms.ListBox lstCustomers; public Form1() { InitializeComponent(); lstCustomers.Items.Add(new Customer("A", "B", DateTime.Now.AddDays(-10))); lstCustomers.Items.Add(new Customer("C", "D", DateTime.Now.AddDays(-100))); lstCustomers.Items.Add(new Customer("F", "G", DateTime.Now.AddDays(-500))); } private void lstCustomers_SelectedIndexChanged(object sender, EventArgs e) { Customer cust = (Customer)lstCustomers.SelectedItem; MessageBox.Show("Birth Date: " + cust.BirthDate.ToShortDateString()); } private void InitializeComponent() { this.lstCustomers = new System.Windows.Forms.ListBox(); this.SuspendLayout(); // // lstCustomers // this.lstCustomers.FormattingEnabled = true; this.lstCustomers.Location = new System.Drawing.Point(12, 12); this.lstCustomers.Name = "lstCustomers"; this.lstCustomers.Size = new System.Drawing.Size(120, 95); this.lstCustomers.TabIndex = 0; this.lstCustomers.SelectedIndexChanged += new System.EventHandler(this.lstCustomers_SelectedIndexChanged); // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(308, 230); this.Controls.Add(this.lstCustomers); this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); this.Name = "Form1"; this.Text = "Form1"; this.ResumeLayout(false); } [STAThread] static void Main() { Application.EnableVisualStyles(); Application.Run(new Form1()); } } public class Customer { public string FirstName; public string LastName; public DateTime BirthDate; public Customer() { } public Customer(string firstName, string lastName, DateTime birthDate) { FirstName = firstName; LastName = lastName; BirthDate = birthDate; } public override string ToString() { return FirstName + " " + LastName; } }