C#入门小程序ZHP_02.doc_第1页
C#入门小程序ZHP_02.doc_第2页
C#入门小程序ZHP_02.doc_第3页
C#入门小程序ZHP_02.doc_第4页
C#入门小程序ZHP_02.doc_第5页
已阅读5页,还剩7页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

C#入门小程序集锦二(2则)这个集锦来源于我开始接触C#时所分析过的一些小程序和一些自己设计的小程序,我是从.NET Framework v2.0 SDK开始的,所以一开始的程序各个.NET版本都能够运行。Test_03:ListView的用法using System;using System.Windows.Forms;using System.Drawing;using System.IO;/包含ListView(列表)、ListViewItem(项、行)、ColumnHeader(列)相关联三者的一般用法namespace test class T_ListView : Form System.ComponentModel.Container components = null;STAThreadstatic void Main(string args)Application.Run(new T_ListView();/属性Button b1;T_ListView()this.Start_Screen();/清空正在使用的资源protected override void Dispose( bool disposing )if( disposing )if (components != null) components.Dispose();base.Dispose( disposing );void b1_Click(Object sender,EventArgs e)Application.Exit();#region Windows 窗体设计器生成的代码void Start_Screen()this.Text = test;this.MaximizeBox = false;this.MinimizeBox = false;this.HelpButton = false;this.StartPosition = FormStartPosition.CenterScreen;this.Show();b1 = new Button();b1.Text = Cancel;b1.Location = new Point(10,10);b1.Click += new EventHandler(this.b1_Click);this.CreateMyListView(b1);this.Controls.Add(b1);#endregionprivate void CreateMyListView(Button b1) ListView listView1 = new ListView(); listView1.Bounds = new Rectangle(new Point(b1.Left,b1.Top+b1.Height+10), new Size(300,200); listView1.View = View.Details; listView1.LabelEdit = true; listView1.AllowColumnReorder = true; listView1.CheckBoxes = true; listView1.FullRowSelect = true; listView1.GridLines = true; listView1.Sorting = SortOrder.Ascending; ListViewItem item1 = new ListViewItem(item1,0); item1.Checked = true; item1.SubItems.Add(1); item1.SubItems.Add(2); item1.SubItems.Add(3); ListViewItem item2 = new ListViewItem(item2,1); item2.SubItems.Add(4); item2.SubItems.Add(5); item2.SubItems.Add(6); ListViewItem item3 = new ListViewItem(item3,0); item3.Checked = true; item3.SubItems.Add(7); item3.SubItems.Add(8); item3.SubItems.Add(9); listView1.Columns.Add(Item Column, -2, HorizontalAlignment.Left); listView1.Columns.Add(Column 2, -2, HorizontalAlignment.Left); listView1.Columns.Add(Column 3, -2, HorizontalAlignment.Left); listView1.Columns.Add(Column 4, -2, HorizontalAlignment.Center); listView1.Items.AddRange(new ListViewItemitem1,item2,item3); /*ImageList imageListSmall = new ImageList(); ImageList imageListLarge = new ImageList(); imageListSmall.Images.Add(Bitmap.FromFile(C:MySmallImage1.bmp); imageListSmall.Images.Add(Bitmap.FromFile(C:MySmallImage2.bmp); imageListLarge.Images.Add(Bitmap.FromFile(C:MyLargeImage1.bmp); imageListLarge.Images.Add(Bitmap.FromFile(C:MyLargeImage2.bmp); listView1.LargeImageList = imageListLarge; listView1.SmallImageList = imageListSmall;*/PopulateListView(listView1); this.Controls.Add(listView1);/列标头的应用:ColumnHeaderprivate void PopulateListView(ListView l) ListView ListView1 = l; ColumnHeader header1, header2; header1 = new ColumnHeader(); header2 = new ColumnHeader(); header1.Text = File name; header1.TextAlign = HorizontalAlignment.Left; header1.Width = 70; header2.TextAlign = HorizontalAlignment.Left; header2.Text = Location; header2.Width = 200; ListView1.Columns.Add(header1); ListView1.Columns.Add(header2); /*System.IO.DirectoryInfo dirInfo = new System.IO.DirectoryInfo( C:Documents and SettingsAll Users + DocumentsMy PicturesSample Pictures); System.IO.FileInfo files = dirInfo.GetFiles(*.jpg); if (files != null) foreach ( System.IO.FileInfo file in files ) ListViewItem item = new ListViewItem(file.Name); item.SubItems.Add(file.FullName); ListView1.Items.Add(item); */Test_04:自主设计程式/C#自主设计程式一/功能:计算器(从整数A到整数B的连乘计算器)using System;using System.Windows.Forms;using System.Drawing;namespace ZHP.TestAPP class MainForm : Form /变量设置string firstNumber=null,finalNumber=null;enum ButtonOK firstOK,finalOK,resultOK;ButtonOK bOK;TextBox t1,t2,t3;/为主窗口生成显示的3个TextBox控件/程序入口STAThreadstatic void Main(string args)Application.Run(new MainForm();/构造函数MainForm()bOK = ButtonOK.firstOK;this.InitializeComponent();/窗口初始化#regionvoid InitializeComponent()/为主窗口增加3个Label和12个ButtonLabel l1,l2,l3;Button b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15;/优化程式this.SuspendLayout();/Button设置b1 = new Button();b1.Text = 1;b1.Location = new Point(5,5);b1.Size = new Size(30,30);b1.Click += new System.EventHandler(Button_Click);b2 = new Button();b2.Text = 2;b2.Location = new Point(40,5);b2.Size = new Size(30,30);b2.Click += new System.EventHandler(Button_Click);b3 = new Button();b3.Text = 3;b3.Location = new Point(75,5);b3.Size = new Size(30,30);b3.Click += new System.EventHandler(Button_Click);b4 = new Button();b4.Text = 4;b4.Location = new Point(110,5);b4.Size = new Size(30,30);b4.Click += new System.EventHandler(Button_Click);b5 = new Button();b5.Text = 5;b5.Location = new Point(5,40);b5.Size = new Size(30,30);b5.Click += new System.EventHandler(Button_Click);b6 = new Button();b6.Text = 6;b6.Location = new Point(40,40);b6.Size = new Size(30,30);b6.Click += new System.EventHandler(Button_Click);b7 = new Button();b7.Text = 7;b7.Location = new Point(75,40);b7.Size = new Size(30,30);b7.Click += new System.EventHandler(Button_Click);b8 = new Button();b8.Text = 8;b8.Location = new Point(110,40);b8.Size = new Size(30,30);b8.Click += new System.EventHandler(Button_Click);b9 = new Button();b9.Text = 9;b9.Location = new Point(5,75);b9.Size = new Size(30,30);b9.Click += new System.EventHandler(Button_Click);b10 = new Button();b10.Text = 0;b10.Location = new Point(40,75);b10.Size = new Size(30,30);b10.Click += new System.EventHandler(Button_Click);b11 = new Button();b11.Text = 确定;b11.Font = new Font(b10.Font.Name,7.5F);b11.Location = new Point(75,75);b11.Size = new Size(30,30);b11.Click += new System.EventHandler(ButtonOK_Click);b12 = new Button();b12.Text = 结果;b12.Font = new Font(b10.Font.Name,7.5F);b12.Location = new Point(110,75);b12.Size = new Size(30,30);b12.Click += new System.EventHandler(ButtonResult_Click);b13 = new Button();b13.Text = 结果清零;b13.Location = new Point(5,110);b13.Size = new Size(135,30);b13.Click += new System.EventHandler(ButtonDispose_Click);b14 = new Button();b14.Text = 结果显示;b14.Location = new Point(140,110);b14.Size = new Size(65,30);b14.Click += new System.EventHandler(ButtonBig_Click);b15 = new Button();b15.Text = 删除;b15.Location = new Point(210,110);b15.Size = new Size(65,30);b15.Click += new System.EventHandler(ButtonRemove_Click);/Labell1 = new Label();l1.Location = new Point(140,5);l1.TextAlign = ContentAlignment.MiddleCenter;l1.Text = 初值;l1.Size = new Size(50,30);l2 = new Label();l2.Location = new Point(140,40);l2.TextAlign = ContentAlignment.MiddleCenter;l2.Text = 末值;l2.Size = new Size(50,30);l3 = new Label();l3.Location = new Point(140,75);l3.TextAlign = ContentAlignment.MiddleCenter;l3.Text = 结果;l3.Size = new Size(50,30);/TextBoxt1 = new TextBox();t1.Multiline = false;t1.Text = 0;t1.TextAlign = HorizontalAlignment.Right;t1.Enabled = false;t1.MaxLength = 6;t1.Location = new Point(200,5);t1.Width = 75;t1.Font = new Font(this.Font.Name,12);t2 = new TextBox();t2.Multiline = false;t2.Text = 0;t2.TextAlign = HorizontalAlignment.Right;t2.Enabled = false;t2.MaxLength = 6;t2.Location = new Point(200,40);t2.Width = 75;t2.Font = new Font(this.Font.Name,12);t3 = new TextBox();t3.Multiline = false;t3.Text = 0;t3.TextAlign = HorizontalAlignment.Right;t3.Enabled = false;t3.MaxLength = 6;t3.Location = new Point(200,75);t3.Width = 75;t3.Font = new Font(this.Font.Name,12);/为主窗口加入控件this.Controls.Add(b1);this.Controls.Add(b2);this.Controls.Add(b3);this.Controls.Add(b4);this.Controls.Add(b5);this.Controls.Add(b6);this.Controls.Add(b7);this.Controls.Add(b8);this.Controls.Add(b9);this.Controls.Add(b10);this.Controls.Add(b11);this.Controls.Add(b12);this.Controls.Add(b13);this.Controls.Add(b14);this.Controls.Add(b15);this.Controls.Add(l1);this.Controls.Add(l2);this.Controls.Add(l3);this.Controls.Add(t1);this.Controls.Add(t2);this.Controls.Add(t3);/主窗口设置this.Text = 自主设计程式一;this.HelpButton = true;this.MinimizeBox = true;this.MaximizeBox = false;this.FormBorderStyle = FormBorderStyle.FixedSingle;this.StartPosition = FormStartPosition.CenterScreen;this.ClientSize = new Size(280,140);this.ResumeLayout(false);#endregion/数位键事件void Button_Click(Object sender,EventArgs e)Button clickedButton = (Button)sender;if(bOK = ButtonOK.firstOK)if(firstNumber != null)if(firstNumber.Length = 6)AddPromptForm(最大为六位数!,0);elsefirstNumber += clickedButton.Text;t3.Text = firstNumber;elsefirstNumber += clickedButton.Text;t3.Text = firstNumber;else if(bOK = ButtonOK.finalOK)if(finalNumber != null)if(finalNumber.Length = 6)AddPromptForm(最大为六位数!,0);elsefinalNumber += clickedButton.Text;t3.Text = finalNumber;elsefinalNumber += clickedButton.Text;t3.Text = finalNumber;else if(bOK = ButtonOK.resultOK)AddPromptForm(初末值已经确定,请点击结果按钮得出结果。n想重新计算请清零。,0);/OK键事件void ButtonOK_Click(Object sender,EventArgs e)if(bOK = ButtonOK.firstOK)t1.Text = firstNumber;bOK = ButtonOK.finalOK;t3.Text = 0;else if(bOK = ButtonOK.finalOK)if(Int32.Parse(finalNumber) = (Int32.Parse(firstNumber)t2.Text = finalNumber;bOK = ButtonOK.resultOK;t3.Text = 0;elseAddPromptForm(末值不能比初值小,请重新输入!,1);elseAddPromptForm(初末值已经确定,请点击结果按钮得出结果。,0);/提示框void AddPromptForm(string s,int i)this.Enabled = false;Form promptForm = new Form();promptForm.MaximizeBox = false;promptForm.MinimizeBox = false;if(i = 0)promptForm.Text = 提示框;else if(i = 1)promptForm.Text = 警告框;else if(i = 2)promptForm.Text = 结果放大窗口;elsepromptForm.Text = 错误框;Label l1 = new Label();l1.Text = s;l1.TextAlign = ContentAlignment.MiddleCenter;l1.Location = new Point(0,0);l1.AutoSize = true;promptForm.Controls.Add(l1);promptForm.ClientSize = l1.Size;promptForm.FormBorderStyle = FormBorderStyle.FixedSingle;promptForm.StartPosition = FormStartPosition.CenterScreen

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论