///
/// A simple ListView example.
/// This diet list contains Calorie items and Fat Calorie and Description
/// subItems.
///
/// Note:
///
/// - The use of subscripts to get to the item and subitem collections
///
- The use of mouse click position to determine the selected item
///
- The get and set methods for dialog itemDialog should be rewritten
/// to use properties
///
- The use of XML
tags for comments. Generate html docs.
/// Under the Tools menu select "Build comment web pages".
/// Be sure to set the correct directory, use browse.
///
/// - I use the subdirectory name docs.
///
- Necessary files will be stored in docs.
///
- A subdirectory docs/"projectName" will have the actual project
/// htm pages.
///
///
/// Mike Barnes 11/30/02
///
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace ListView {
///
/// The Form has:
///
/// - ListView in the center. The ListView has columns for
/// Calories, Fat Calories, and an Item Descritpion field
/// - ToolBar on the right. The toolBar has 3 buttons to
///
/// - add an item to the list
/// - edit the selected item in the list
/// - delete the selected item from the list
///
/// - StatusBar to display usage and provide feedback
///
///
public class Form1 : System.Windows.Forms.Form
{
public System.Windows.Forms.ListView listView1;
private System.Windows.Forms.ColumnHeader calorieHeader;
private System.Windows.Forms.ColumnHeader fatCalorieHeader;
private System.Windows.Forms.ColumnHeader descriptHeader;
private System.Windows.Forms.StatusBar statusBar;
private System.Windows.Forms.ImageList imageList1;
private System.Windows.Forms.ToolBarButton addButton;
private System.Windows.Forms.ToolBarButton editButton;
private System.Windows.Forms.ToolBarButton deleteButton;
private System.Windows.Forms.ToolBar toolBar;
private System.ComponentModel.IContainer components;
private int count, selectedIndex;
private ListViewItem selectedItem;
private ItemDialog itemDialog;
public Form1() {
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
count = 0;
selectedIndex = 0;
itemDialog = new ItemDialog();
}
///
/// Clean up any resources being used.
///
protected override void Dispose( bool disposing ) {
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
System.Resources.ResourceManager resources =
new System.Resources.ResourceManager(typeof(Form1));
this.listView1 = new System.Windows.Forms.ListView();
this.calorieHeader = new System.Windows.Forms.ColumnHeader();
this.fatCalorieHeader = new System.Windows.Forms.ColumnHeader();
this.descriptHeader = new System.Windows.Forms.ColumnHeader();
this.statusBar = new System.Windows.Forms.StatusBar();
this.imageList1 = new System.Windows.Forms.ImageList(this.components);
this.addButton = new System.Windows.Forms.ToolBarButton();
this.editButton = new System.Windows.Forms.ToolBarButton();
this.deleteButton = new System.Windows.Forms.ToolBarButton();
this.toolBar = new System.Windows.Forms.ToolBar();
this.SuspendLayout();
//
// listView1
//
this.listView1.Activation = System.Windows.Forms.ItemActivation.OneClick;
this.listView1.AutoArrange = false;
this.listView1.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
this.calorieHeader,
this.fatCalorieHeader,
this.descriptHeader});
this.listView1.Dock = System.Windows.Forms.DockStyle.Fill;
this.listView1.FullRowSelect = true;
this.listView1.GridLines = true;
this.listView1.HeaderStyle =
System.Windows.Forms.ColumnHeaderStyle.Nonclickable;
this.listView1.ImeMode = System.Windows.Forms.ImeMode.AlphaFull;
this.listView1.LabelEdit = true;
this.listView1.MultiSelect = false;
this.listView1.Name = "listView1";
this.listView1.Size = new System.Drawing.Size(344, 273);
this.listView1.TabIndex = 0;
this.listView1.View = System.Windows.Forms.View.Details;
this.listView1.MouseUp +=
new System.Windows.Forms.MouseEventHandler(this.setSelectedIndex);
//
// calorieHeader
//
this.calorieHeader.Text = "Calories";
//
// fatCalorieHeader
//
this.fatCalorieHeader.Text = "Fat Cals";
//
// descriptHeader
//
this.descriptHeader.Text = "Description";
this.descriptHeader.Width = 200;
//
// statusBar
//
this.statusBar.Location = new System.Drawing.Point(0, 251);
this.statusBar.Name = "statusBar";
this.statusBar.Size = new System.Drawing.Size(344, 22);
this.statusBar.TabIndex = 1;
this.statusBar.Text = "show selection item";
//
// imageList1
//
this.imageList1.ColorDepth = System.Windows.Forms.ColorDepth.Depth24Bit;
this.imageList1.ImageSize = new System.Drawing.Size(16, 16);
this.imageList1.ImageStream =
((System.Windows.Forms.ImageListStreamer)
(resources.GetObject("imageList1.ImageStream")));
this.imageList1.TransparentColor = System.Drawing.Color.Transparent;
//
// addButton
//
this.addButton.ImageIndex = 0;
this.addButton.ToolTipText = "Insert an item into list";
//
// editButton
//
this.editButton.ImageIndex = 1;
this.editButton.ToolTipText = "edit the selected item";
//
// deleteButton
//
this.deleteButton.ImageIndex = 2;
this.deleteButton.ToolTipText = "delete the current item";
//
// toolBar
//
this.toolBar.Buttons.AddRange(new System.Windows.Forms.ToolBarButton[] {
this.addButton,
this.editButton,
this.deleteButton});
this.toolBar.Dock = System.Windows.Forms.DockStyle.Right;
this.toolBar.DropDownArrows = true;
this.toolBar.ImageList = this.imageList1;
this.toolBar.Location = new System.Drawing.Point(320, 0);
this.toolBar.Name = "toolBar";
this.toolBar.ShowToolTips = true;
this.toolBar.Size = new System.Drawing.Size(24, 251);
this.toolBar.TabIndex = 2;
this.toolBar.ButtonClick +=
new System.Windows.Forms.ToolBarButtonClickEventHandler
(this.toolBar_ButtonClick);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(344, 273);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.toolBar,
this.statusBar,
this.listView1});
this.Name = "Form1";
this.Text = "Simple List View Example";
this.ResumeLayout(false);
}
#endregion
///
/// The main entry point for the application.
///
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
///
/// Respond to button clicks from the toolbar.
/// Use e.Button property of the event arg to determine which toolbar button
/// was clicked.
/// This delegate will display the itemDialog when adding or editing items.
/// Error conditions are reported in the statusBar.
///
/// ToolBarButton
/// ToolBarButtonClickEventArgs
private void toolBar_ButtonClick(object sender,
System.Windows.Forms.ToolBarButtonClickEventArgs e)
{
if(e.Button == addButton){
itemDialog.clear();
if (DialogResult.OK == itemDialog.ShowDialog(this)) {
listView1.Items.Insert(count, itemDialog.Calories);
listView1.Items[count].SubItems.Add(itemDialog.FatCals);
listView1.Items[count].SubItems.Add(itemDialog.Description);
count++;
statusBar.Text = "insert. count = " + count.ToString(); }
}
else if (e.Button == editButton){
if(selectedItem == null) {
statusBar.Text = "Select an item before trying to edit";
return; }
itemDialog.Calories = listView1.Items[selectedIndex].Text;
itemDialog.FatCals = selectedItem.SubItems[1].Text;
itemDialog.Description = selectedItem.SubItems[2].Text;
if(DialogResult.OK == itemDialog.ShowDialog(this)) {
listView1.Items[selectedIndex].Text = itemDialog.Calories;
listView1.Items[selectedIndex].SubItems[1].Text =
itemDialog.FatCals;
listView1.Items[selectedIndex].SubItems[2].Text =
itemDialog.Description;
}
statusBar.Text = "edit. selected index = " +
selectedItem.ToString();}
else if (e.Button == deleteButton)
if(selectedItem == null)
statusBar.Text = "Select an item before trying to delete";
else {
count--;
listView1.Items.RemoveAt(count);
statusBar.Text = "delete. count = " + count.ToString();}
else statusBar.Text = "toolbar pushed";
}
///
/// Mouse clicks in the listView are used to set:
///
/// - selectedItem
///
- selectedIndex
///
///
/// ListView
/// MouseEventArgs
private void setSelectedIndex(object sender,
System.Windows.Forms.MouseEventArgs e)
{
selectedItem = listView1.GetItemAt(e.X, e.Y);
if (selectedItem != null) {
selectedIndex = selectedItem.Index;
statusBar.Text = "item at " + selectedIndex.ToString() +
" is " + selectedItem.ToString();
}
else
statusBar.Text = "no item selected";
}
}
}