listview的使用一例

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;

namespace ListViewDemo
{
 /**//// <summary>
 /// Form1 的摘要说明。
 /// </summary>
 public class DemoForm : System.Windows.Forms.Form
 {
  private System.Windows.Forms.ListView lvw;
  private System.Windows.Forms.ImageList ilsLargeIcons;
  private System.Windows.Forms.ImageList ilsSmallIcons;
  private System.Windows.Forms.ComboBox cboView;
  private System.Windows.Forms.ColumnHeader columnHeader1;
  private System.Windows.Forms.ColumnHeader columnHeader2;
  private System.Windows.Forms.ColumnHeader columnHeader3;
  private System.Windows.Forms.ColumnHeader columnHeader4;
  private System.Windows.Forms.ColumnHeader columnHeader5;
  private System.Windows.Forms.ColumnHeader columnHeader6;
  private System.ComponentModel.IContainer components;
  private System.Windows.Forms.Button btnUse;
  private System.Windows.Forms.Button btnLeave;

  private ListViewItem itmX;
  private System.Windows.Forms.Button btnStat;
  const int MoneyPerHour=2;

  public DemoForm()
  {
   //
   // Windows 窗体设计器支持所必需的
   //
   InitializeComponent();

   //
   // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
   //
  }

  /**//// <summary>
  /// 清理所有正在使用的资源。
  /// </summary>
  protected override void Dispose( bool disposing )
  {
   if( disposing )
   {
    if (components != null)
    {
     components.Dispose();
    }
   }
   base.Dispose( disposing );
  }


   /**//// <summary>
   /// 设计器支持所需的方法 - 不要使用代码编辑器修改
   /// 此方法的内容。
   /// </summary>
   private void InitializeComponent()
   {
    this.components = new System.ComponentModel.Container();
    this.lvw = new System.Windows.Forms.ListView();
    this.columnHeader1 = new System.Windows.Forms.ColumnHeader();
    this.columnHeader2 = new System.Windows.Forms.ColumnHeader();
    this.columnHeader3 = new System.Windows.Forms.ColumnHeader();
    this.columnHeader4 = new System.Windows.Forms.ColumnHeader();
    this.columnHeader5 = new System.Windows.Forms.ColumnHeader();
    this.columnHeader6 = new System.Windows.Forms.ColumnHeader();
    this.ilsLargeIcons = new System.Windows.Forms.ImageList(this.components);
    this.ilsSmallIcons = new System.Windows.Forms.ImageList(this.components);
    this.cboView = new System.Windows.Forms.ComboBox();
    this.btnUse = new System.Windows.Forms.Button();
    this.btnLeave = new System.Windows.Forms.Button();
    this.btnStat = new System.Windows.Forms.Button();
    this.SuspendLayout();
    //
    // lvw
    //
    this.lvw.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
                       this.columnHeader1,
                       this.columnHeader2,
                       this.columnHeader3,
                       this.columnHeader4,
                       this.columnHeader5,
                       this.columnHeader6});
    this.lvw.FullRowSelect = true;
    this.lvw.GridLines = true;
    this.lvw.HideSelection = false;
    this.lvw.LargeImageList = this.ilsLargeIcons;
    this.lvw.Location = new System.Drawing.Point(0, 0);
    this.lvw.MultiSelect = false;
    this.lvw.Name = "lvw";
    this.lvw.Size = new System.Drawing.Size(512, 232);
    this.lvw.TabIndex = 0;
    this.lvw.View = System.Windows.Forms.View.Details;
    this.lvw.SelectedIndexChanged += new System.EventHandler(this.lvw_SelectedIndexChanged);
    //
    // columnHeader1
    //
    this.columnHeader1.Text = "机号";
    this.columnHeader1.Width = 54;
    //
    // columnHeader2
    //
    this.columnHeader2.Text = "状态";
    this.columnHeader2.Width = 58;
    //
    // columnHeader3
    //
    this.columnHeader3.Text = "上机时间";
    this.columnHeader3.Width = 126;
    //
    // columnHeader4
    //
    this.columnHeader4.Text = "下机时间";
    this.columnHeader4.Width = 118;
    //
    // columnHeader5
    //
    this.columnHeader5.Text = "小计";
    this.columnHeader5.Width = 70;
    //
    // columnHeader6
    //
    this.columnHeader6.Text = "金额";
    //
    // ilsLargeIcons
    //
    this.ilsLargeIcons.ImageSize = new System.Drawing.Size(32, 32);
    this.ilsLargeIcons.TransparentColor = System.Drawing.Color.Transparent;
    //
    // ilsSmallIcons
    //
    this.ilsSmallIcons.ImageSize = new System.Drawing.Size(16, 16);
    this.ilsSmallIcons.TransparentColor = System.Drawing.Color.Transparent;
    //
    // cboView
    //
    this.cboView.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
    this.cboView.Items.AddRange(new object[] {
                "大图标",
                "详细资料",
                "小图标",
                "列表"});
    this.cboView.Location = new System.Drawing.Point(24, 248);
    this.cboView.Name = "cboView";
    this.cboView.Size = new System.Drawing.Size(96, 20);
    this.cboView.TabIndex = 1;
    this.cboView.SelectedIndexChanged += new System.EventHandler(this.cboView_SelectedIndexChanged);
    //
    // btnUse
    //
    this.btnUse.Location = new System.Drawing.Point(168, 248);
    this.btnUse.Name = "btnUse";
    this.btnUse.Size = new System.Drawing.Size(80, 24);
    this.btnUse.TabIndex = 2;
    this.btnUse.Text = "上机登记";
    this.btnUse.Click += new System.EventHandler(this.btnUse_Click);
    //
    // btnLeave
    //
    this.btnLeave.Location = new System.Drawing.Point(264, 248);
    this.btnLeave.Name = "btnLeave";
    this.btnLeave.Size = new System.Drawing.Size(80, 24);
    this.btnLeave.TabIndex = 2;
    this.btnLeave.Text = "下机登记";
    this.btnLeave.Click += new System.EventHandler(this.btnLeave_Click);
    //
    // btnStat
    //
    this.btnStat.Location = new System.Drawing.Point(360, 248);
    this.btnStat.Name = "btnStat";
    this.btnStat.Size = new System.Drawing.Size(64, 24);
    this.btnStat.TabIndex = 3;
    this.btnStat.Text = "统计";
    this.btnStat.Click += new System.EventHandler(this.btnStat_Click);
    //
    // DemoForm
    //
    this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
    this.ClientSize = new System.Drawing.Size(512, 280);
    this.Controls.Add(this.btnStat);
    this.Controls.Add(this.btnUse);
    this.Controls.Add(this.cboView);
    this.Controls.Add(this.lvw);
    this.Controls.Add(this.btnLeave);
    this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
    this.MaximizeBox = false;
    this.Name = "DemoForm";
    this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
    this.Text = "ListView控件的使用";
    this.Load += new System.EventHandler(this.DemoForm_Load);
    this.ResumeLayout(false);

   }


  /**//// <summary>
  /// 应用程序的主入口点。
  /// </summary>
  [STAThread]
  static void Main()
  {
   Application.Run(new DemoForm());
  }

  private void DemoForm_Load(object sender, System.EventArgs e)
  {
   lvw.SmallImageList=ilsSmallIcons;
   for (int i=1;i<=30;i++)
   {
    itmX=lvw.Items.Add(i.ToString(),0);  //Add方法返回新增的ListViewItem对象
    itmX.SubItems.Add("未使用");
    itmX.SubItems.Add("");
    itmX.SubItems.Add("");
    itmX.SubItems.Add("");
    itmX.SubItems.Add("");

   }

   cboView.SelectedIndex=1;
  }

  private void cboView_SelectedIndexChanged(object sender, System.EventArgs e)
  {
   lvw.View=(View)cboView.SelectedIndex;
  }

  private void btnUse_Click(object sender, System.EventArgs e)
  {
   if (lvw.SelectedItems.Count>0)
   {
    itmX=lvw.SelectedItems[0];
    if (MessageBox.Show("确定使用" + itmX.Text + "号计算机吗?","提示",MessageBoxButtons.OKCancel,MessageBoxIcon.Question)==DialogResult.OK)
    {
     itmX.SubItems[2].Text=DateTime.Now.ToString();
     itmX.SubItems[1].Text="使用中";
     itmX.ImageIndex=1;
     itmX.SubItems[3].Text="";
     itmX.SubItems[4].Text="";
     itmX.SubItems[5].Text="";
     lvw_SelectedIndexChanged(lvw,EventArgs.Empty);
    }
   }
  }

  private void btnLeave_Click(object sender, System.EventArgs e)
  {
   if (lvw.SelectedItems.Count>0)
   {
    itmX=lvw.SelectedItems[0];
    if (MessageBox.Show("确定不再使用" + itmX.Text + "号计算机吗?","提示",MessageBoxButtons.OKCancel,MessageBoxIcon.Question)==DialogResult.OK)
    {
     itmX.SubItems[3].Text=DateTime.Now.ToString();
     itmX.SubItems[1].Text="未使用";
     itmX.ImageIndex=0;
     TimeSpan ts=DateTime.Now-DateTime.Parse(itmX.SubItems[2].Text);
     itmX.SubItems[4].Text=ts.Hours + "小时" + ts.Minutes + "分";
                   
     int hours=ts.Hours;
     if (ts.Minutes>0) hours++;//不足一小时按一小时计算

     itmX.SubItems[5].Text=hours*MoneyPerHour + "元";

     lvw_SelectedIndexChanged(lvw,EventArgs.Empty);
     SqlConnection cn=DBOperate.GetConnection();
     try
     {
      cn.Open();
      SqlCommand cmd=new SqlCommand("Insert into UsingDetails Values(@ComputerNo, @StartTime, @EndTime, @TotalMoney)",cn);
      cmd.Parameters.Add("@ComputerNo",int.Parse(itmX.Text));
      cmd.Parameters.Add("@StartTime",DateTime.Parse(itmX.SubItems[2].Text));
      cmd.Parameters.Add("@EndTime",DateTime.Now);
      cmd.Parameters.Add("@TotalMoney",(decimal)(hours*MoneyPerHour));
      cmd.ExecuteNonQuery();
     }
     finally
     {
      cn.Close();
     }
    }
   }
  }

  private void btnStat_Click(object sender, System.EventArgs e)
  {
//   StatForm frmStat=new StatForm();
//   frmStat.ShowDialog();
  }

  private void lvw_SelectedIndexChanged(object sender, System.EventArgs e)
  {
   if (lvw.SelectedItems.Count>0)
   {
    if (lvw.SelectedItems[0].ImageIndex==0)
    {
     btnUse.Enabled=true;
     btnLeave.Enabled=false;
    }
    else
    {
     btnUse.Enabled=false;
     btnLeave.Enabled=true;
    }
   }
  }
 }
}


 

posted on 2008-06-04 17:02  与非门  阅读(282)  评论(0编辑  收藏  举报

导航