4这是属于上面文件的代码using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.Diagnostics;//秒表所需要的累namespace 秒表{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } Timer time = new Timer(); //timer对象, TimeSpan t;//时间间隔 Stopwatch sw;///测量运行时间 bool a = true; int b = 0; int count = 1; string str = null; private void button2_Click(object sender, EventArgs e) { this.button1.Text = '开始'; b = 0; sw = new Stopwatch(); label1.Text = b.ToString(); textBox1.Text = string.Format('{0:D2}:{1:D2}:{2:D2}:{3:D3}', 0, 0, 0, 0); listView1.Items.Clear(); dataGridView1.Rows.Clear();//清除数据 count = 1; } private void button1_Click(object sender, EventArgs e) { if (a) { sw = new Stopwatch(); this.button1.Text = '暂停'; this.button2.Enabled = false; time.Start();//时间跑动 sw.Start();//开始测量 } else { this.button1.Text = '继续'; time.Stop(); sw.Stop(); this.button2.Enabled = true; } a = !a; time.Tick+=new EventHandler(time_Tick); //加委托 time.Interval =1; //时间间隔为1毫秒 } void time_Tick(object sender, EventArgs e)//显示的频率,取决t.interval { t = sw.Elapsed; textBox1.Text = string.Format('{0:D2}:{1:D2}:{2:D2}:{3:D2}', t.Hours, t.Minutes, t.Seconds, t.Milliseconds);//D2是double2,补两位数, str = textBox1.Text; label1.Text = b.ToString(); b++; } private void add_Click(object sender, EventArgs e) { string[] arry={(count).ToString(),str}; dataGridView1.Rows.Add(arry); // listView1.Items.Add(str,str);//这方法只能加到第一个item ListViewItem lv1 = new ListViewItem(arry[0]); lv1.SubItems.Add(arry[1]); lv1.SubItems.Add(arry[0]);//子items,分成两列 this.listView1.Items.Add(lv1);//加到这一行中去 count++; } private void Form1_Load(object sender, EventArgs e) { listView1.Items.Clear(); } }}
5这是窗体外表的代码namespace 秒表{ partial class Form1 { /// /// 必需的设计器变量。 /// private System.ComponentModel.IContainer components = null; /// /// 清理所有正在使用的资源。 /// /// 如果应释放托管资源,为 true;否则为 false。 protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows 窗体设计器生成的代码 /// /// 设计器支持所需的方法 - 不要 /// 使用代码编辑器修改此方法的内容。 /// private void InitializeComponent() { System.Windows.Forms.ListViewItem listViewItem1 = new System.Windows.Forms.ListViewItem(new string[] { '', '', ''}, -1); System.Windows.Forms.ColumnHeader 序号; System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1)); this.button1 = new System.Windows.Forms.Button(); this.textBox1 = new System.Windows.Forms.TextBox(); this.button2 = new System.Windows.Forms.Button(); this.listView1 = new System.Windows.Forms.ListView(); this.时间 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader())); this.add = new System.Windows.Forms.Button(); this.label1 = new System.Windows.Forms.Label(); this.dataGridView1 = new System.Windows.Forms.DataGridView(); this.Column1 = new System.Windows.Forms.DataGridViewTextBoxColumn(); this.Column2 = new System.Windows.Forms.DataGridViewTextBoxColumn(); this.label2 = new System.Windows.Forms.Label(); this.label3 = new System.Windows.Forms.Label(); 序号 = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader())); ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit(); this.SuspendLayout(); // // button1 // this.button1.Location = new System.Drawing.Point(611, 57); this.button1.Name = 'button1'; this.button1.Size = new System.Drawing.Size(122, 95); this.button1.TabIndex = 0; this.button1.Text = '开始'; this.button1.UseVisualStyleBackColor = true; this.button1.Click += new System.EventHandler(this.button1_Click); // // textBox1 // this.textBox1.Font = new System.Drawing.Font('宋体', 42F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134))); this.textBox1.Location = new System.Drawing.Point(48, 31); this.textBox1.Multiline = true; this.textBox1.Name = 'textBox1'; this.textBox1.Size = new System.Drawing.Size(466, 143); this.textBox1.TabIndex = 1; // // button2 // this.button2.Location = new System.Drawing.Point(611, 210); this.button2.Name = 'button2'; this.button2.Size = new System.Drawing.Size(132, 47); this.button2.TabIndex = 2; this.button2.Text = '复位'; this.button2.UseVisualStyleBackColor = true; this.button2.Click += new System.EventHandler(this.button2_Click); // // listView1 // this.listView1.AccessibleRole = System.Windows.Forms.AccessibleRole.None; this.listView1.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] { 序号, this.时间}); this.listView1.GridLines = true; listViewItem1.ToolTipText = '序号'; this.listView1.Items.AddRange(new System.Windows.Forms.ListViewItem[] { listViewItem1}); this.listView1.Location = new System.Drawing.Point(12, 210); this.listView1.Name = 'listView1'; this.listView1.Size = new System.Drawing.Size(250, 190); this.listView1.TabIndex = 3; this.listView1.UseCompatibleStateImageBehavior = false; this.listView1.View = System.Windows.Forms.View.Details; // // 序号 // 序号.Text = '序号'; 序号.Width = 57; // // 时间 // this.时间.Text = '时间'; this.时间.Width = 271; // // add // this.add.Location = new System.Drawing.Point(611, 300); this.add.Name = 'add'; this.add.Size = new System.Drawing.Size(132, 50); this.add.TabIndex = 4; this.add.Text = '添加'; this.add.UseVisualStyleBackColor = true; this.add.Click += new System.EventHandler(this.add_Click); // // label1 // this.label1.AutoSize = true; this.label1.Location = new System.Drawing.Point(631, 18); this.label1.Name = 'label1'; this.label1.Size = new System.Drawing.Size(55, 15); this.label1.TabIndex = 5; this.label1.Text = 'label1'; // // dataGridView1 // this.dataGridView1.AllowUserToDeleteRows = false; this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize; this.dataGridView1.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] { this.Column1, this.Column2}); this.dataGridView1.EnableHeadersVisualStyles = false; this.dataGridView1.ImeMode = System.Windows.Forms.ImeMode.NoControl; this.dataGridView1.Location = new System.Drawing.Point(268, 210); this.dataGridView1.MultiSelect = false; this.dataGridView1.Name = 'dataGridView1'; this.dataGridView1.RightToLeft = System.Windows.Forms.RightToLeft.No; this.dataGridView1.RowHeadersVisible = false; this.dataGridView1.RowHeadersWidth = 20; this.dataGridView1.RowTemplate.Height = 27; this.dataGridView1.Size = new System.Drawing.Size(320, 190); this.dataGridView1.TabIndex = 6; // // Column1 // this.Column1.HeaderText = '序号'; this.Column1.Name = 'Column1'; this.Column1.Width = 50; // // Column2 // this.Column2.HeaderText = '时间(时分秒)'; this.Column2.Name = 'Column2'; this.Column2.Width = 400; // // label2 // this.label2.AutoSize = true; this.label2.Location = new System.Drawing.Point(398, 190); this.label2.Name = 'label2'; this.label2.Size = new System.Drawing.Size(133, 15); this.label2.TabIndex = 7; this.label2.Text = 'datagridview显示'; // // label3 // this.label3.AutoSize = true; this.label3.Location = new System.Drawing.Point(80, 190); this.label3.Name = 'label3'; this.label3.Size = new System.Drawing.Size(109, 15); this.label3.TabIndex = 8; this.label3.Text = 'listView1显示'; // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 15F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(810, 421); this.Controls.Add(this.label3); this.Controls.Add(this.label2); this.Controls.Add(this.dataGridView1); this.Controls.Add(this.label1); this.Controls.Add(this.add); this.Controls.Add(this.listView1); this.Controls.Add(this.button2); this.Controls.Add(this.textBox1); this.Controls.Add(this.button1); this.Icon = ((System.Drawing.Icon)(resources.GetObject('$this.Icon'))); this.Name = 'Form1'; this.Text = '秒表 李'; this.Load += new System.EventHandler(this.Form1_Load); ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit(); this.ResumeLayout(false); this.PerformLayout(); } #endregion private System.Windows.Forms.Button button1; private System.Windows.Forms.TextBox textBox1; private System.Windows.Forms.Button button2; private System.Windows.Forms.ListView listView1; private System.Windows.Forms.ColumnHeader 时间; private System.Windows.Forms.Button add; private System.Windows.Forms.Label label1; private System.Windows.Forms.DataGridView dataGridView1; private System.Windows.Forms.DataGridViewTextBoxColumn Column1; private System.Windows.Forms.DataGridViewTextBoxColumn Column2; private System.Windows.Forms.Label label2; private System.Windows.Forms.Label label3; }}