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;
}
}
}
}
}
转载于:https://www.cnblogs.com/w84717/archive/2008/06/04/1213743.html
listview的使用一例相关推荐
- c语言 listview,C语言 SDK编程之通用控件的使用--ListView
一.ListView控件属于通用控件CONTROL中的一种,在SDK编程方式时要使用通用控件 必须包含comctl32.dll,所以代码中要有头文件: commctrl.h 导入库:comctl ...
- ListView的使用用ViewHolder提升效率
public class ListView extends AbsListView java.lang.Object ↳ android.view.View ↳ android.vie ...
- Android内存泄漏简介
2019独角兽企业重金招聘Python工程师标准>>> 前言 不少人认为JAVA程序,因为有垃圾回收机制,应该没有内存泄露. 其实如果我们一个程序中,已经不再使用某个对象,但是因为仍 ...
- Android内存泄漏就这样产生了
为什么80%的码农都做不了架构师?>>> 1.资源对象没关闭造成的内存泄漏 描述: 资源性对象比如(Cursor,File文件等)往往都用了一些缓冲,我们在不使用的时候,应该及 ...
- android如何避免内存泄露,Android开发中应该避免的内存泄露
一.背景和目的: 目前许多开发人员在Android开发过程中,较少关注实现细节和内存使用,容易会造成内存泄露,导致程序OOM. 本文会通过代码向大家介绍在Android开发过程中常见的内存泄露. 二. ...
- DDMS的使用、内存溢出的调试和模拟器的启动命令参数
DDMS 的全称是Dalvik Debug Monitor Service,它为我们提供例如:为测试设备截屏,针对特定的进程查看正在运行的线程以及堆信息.Logcat.广播状态信息.模拟电话呼叫.接收 ...
- 国内一线互联网公司内部面试题库
原文链接:https://github.com/JackyAndroid/AndroidInterview-Q-A/blob/master/README-CN.md 欢迎在GitHub或者掘金上关注我 ...
- 内存分析工具MAT的使用
原文链接:http://www.jianshu.com/p/d8e247b1e7b2 MAT简介 MAT(Memory Analyzer Tool),一个基于Eclipse的内存分析工具,是一个快速. ...
- Android内存泄漏问题(一)
前言 不少人认为JAVA程序,因为有垃圾回收机制,应该没有内存泄露. 其实如果我们一个程序中,已经不再使用某个对象,但是因为仍然有引用指向它,垃圾回收器就无法回收它,当然该对象占用的内存就无法被使用, ...
- JavaFX UI控件教程(十五)之Combo Box
翻译自 Combo Box 本章介绍如何在JavaFX应用程序中使用组合框.它讨论了可编辑和不可编辑的组合框,教您如何跟踪可编辑组合框中的更改并处理它们上的事件,并解释如何使用单元工厂来更改组合框 ...
最新文章
- java 类的存储结构设计_Doris存储层设计介绍1——存储结构设计解析
- 关于Object.create()与原型链的面试题?
- Struts2 关于返回type=chain的用法.
- 如何查看keepalived版本号_Linux下Keepalived 安装与配置
- 全新释放 | RealSight APM, 让客户的极致数字体验成为可能
- 论坛一大早白屏,无法访问
- java的整数扩展,浮点数扩展,字符扩展,转义字符,布尔值扩展
- python基础-深浅拷贝
- https post 报400地址匹配不正确_如何发布领英动态post/article?
- iOS 开发应用内跳转到App Store
- linux安装curl扩展
- 什么是cmm3规范?什么是CMMI5 呢?
- Java 读取Excel ( xls 和 xlsx 格式 )
- 青云诀2显示登录服务器超时,青云诀2游戏突然显示数据包损坏怎么办 解决方案分享...
- 仿京东或淘宝的订单中心页面
- 小白攻略(一):数学建模是什么,为什么,怎样做?
- 第十五篇:UE4 漫游Matinee的制作和如何在漫游中实现自由转向(改进)
- Nginx安装配置(lua全模块、GEOIP、加入系统服务)---无坑安装
- 一文带你看懂算术编码(C语言)
- 大红喜庆版UI猜灯谜又叫猜字谜威信小程序源码下载
热门文章
- android 加载中自定义控件,Android 自定义一款炫酷的加载控件
- linux 解压加密zip,linux 系统下 zip 的加密压缩与解压缩命令
- 你写的api接口代码真是_百度AI接口之JavaAPI方式调用示例代码[持续完善中]
- 线接触和面接触的区别_接触器是啥?跟继电器有啥区别,6大常见故障怎么处理...
- mysql 全文搜索怎么设置_mysql 全文搜索 技巧
- TensorFlow基础:Graph与Variable
- 进程和线程的基本概念与区别
- Two Sum(C、C++、Python)
- 用python写WordCount的MapReduce代码
- 强化学习各概念整理(value/policy-based、on/off-policy、model-based/free、offline)