C#第二次作业

                                                             Program.cs

[csharp]  view plain copy
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Threading.Tasks;  
  5. using System.Windows.Forms;  
  6.   
  7. namespace tupian1  
  8. {  
  9.     static class Program  
  10.     {  
  11.         /// <summary>  
  12.         /// 应用程序的主入口点。  
  13.         /// </summary>  
  14.         [STAThread]  
  15.         static void Main()  
  16.         {  
  17.             Application.EnableVisualStyles();  
  18.             Application.SetCompatibleTextRenderingDefault(false);  
  19.             Application.Run(new Form1());  
  20.         }  
  21.     }  
  22. }  

 Form1.cs

[csharp]  view plain copy
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Threading.Tasks;  
  9. using System.Windows.Forms;  
  10. using System.IO;  
  11. namespace tupian1  
  12. {  
  13.     public partial class Form1 : Form  
  14.     {  
  15.         const int COUNT = 5;  
  16.         int[] nTimes = new int[COUNT];  
  17.         String strDirImg = @"C:\Users\Administrator\Desktop\新建文件夹";  
  18.   
  19.         public Form1()  
  20.         {  
  21.             InitializeComponent();  
  22.         }  
  23.   
  24.         private void Form1_Load(object sender, EventArgs e)  
  25.         {  
  26.             lbl.Text = "显示次数:";  
  27.             String strPath = strDirImg + "/1.jpg";  
  28.             pictureBox1.Image = new Bitmap(strPath);  
  29.             String strPathCount = strDirImg + "count.txt";  
  30.             StreamReader sd = new StreamReader(strPathCount);  
  31.             String line;  
  32.             int nLine = 0;  
  33.             while ((line =sd.ReadLine())!=null)  
  34.             {  
  35.                 int n = Convert.ToInt32(line);  
  36.                 nTimes[nLine] = n;  
  37.                 nLine++;  
  38.             }  
  39.         }  
  40.   
  41.         private void btn_Click(object sender, EventArgs e)  
  42.         {  
  43.             Random rnd = new Random();  
  44.             int num = rnd.Next(1, COUNT + 1);  
  45.             nTimes[num - 1] = nTimes[num - 1] + 1;  
  46.             String strPath = strDirImg +"/"+ num + ".jpg";  
  47.             pictureBox1.Image = new Bitmap(strPath);  
  48.             lbl.Text = "图片" +  num + ".jpg" + "    显示次数:" + nTimes[num - 1];  
  49.         }  
  50.   
  51.         private void Form1_FormClosed(object sender, FormClosedEventArgs e)  
  52.         {  
  53.             String strPathCount = strDirImg + "count.txt";  
  54.             StreamWriter sw = new StreamWriter(strPathCount);  
  55.             for (int n = 0; n < COUNT; n++)  
  56.             {  
  57.                 sw.WriteLine(nTimes[n]);  
  58.             }  
  59.             sw.Close();  
  60.         }  
  61.     }  
  62. }  

    FormDesigners.cs

[csharp]  view plain copy
  1. namespace tupian1  
  2. {  
  3.     partial class Form1  
  4.     {  
  5.         /// <summary>  
  6.         /// 必需的设计器变量。  
  7.         /// </summary>  
  8.         private System.ComponentModel.IContainer components = null;  
  9.   
  10.         /// <summary>  
  11.         /// 清理所有正在使用的资源。  
  12.         /// </summary>  
  13.         /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>  
  14.         protected override void Dispose(bool disposing)  
  15.         {  
  16.             if (disposing && (components != null))  
  17.             {  
  18.                 components.Dispose();  
  19.             }  
  20.             base.Dispose(disposing);  
  21.         }  
  22.  
  23.         #region Windows 窗体设计器生成的代码  
  24.   
  25.         /// <summary>  
  26.         /// 设计器支持所需的方法 - 不要  
  27.         /// 使用代码编辑器修改此方法的内容。  
  28.         /// </summary>  
  29.         private void InitializeComponent()  
  30.         {  
  31.             System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));  
  32.             this.pictureBox1 = new System.Windows.Forms.PictureBox();  
  33.             this.btn = new System.Windows.Forms.Button();  
  34.             this.lbl = new System.Windows.Forms.Label();  
  35.             ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();  
  36.             this.SuspendLayout();  
  37.             //   
  38.             // pictureBox1  
  39.             //   
  40.             this.pictureBox1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));  
  41.             this.pictureBox1.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox1.Image")));  
  42.             this.pictureBox1.Location = new System.Drawing.Point(49, 61);  
  43.             this.pictureBox1.Name = "pictureBox1";  
  44.             this.pictureBox1.Size = new System.Drawing.Size(177, 107);  
  45.             this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom;  
  46.             this.pictureBox1.TabIndex = 0;  
  47.             this.pictureBox1.TabStop = false;  
  48.             //   
  49.             // btn  
  50.             //   
  51.             this.btn.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));  
  52.             this.btn.AutoSize = true;  
  53.             this.btn.Location = new System.Drawing.Point(149, 218);  
  54.             this.btn.Name = "btn";  
  55.             this.btn.Size = new System.Drawing.Size(89, 23);  
  56.             this.btn.TabIndex = 1;  
  57.             this.btn.Text = "随机显示";  
  58.             this.btn.UseVisualStyleBackColor = true;  
  59.             this.btn.Click += new System.EventHandler(this.btn_Click);  
  60.             //   
  61.             // lbl  
  62.             //   
  63.             this.lbl.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));  
  64.             this.lbl.AutoSize = true;  
  65.             this.lbl.Location = new System.Drawing.Point(49, 190);  
  66.             this.lbl.Name = "lbl";  
  67.             this.lbl.Size = new System.Drawing.Size(41, 12);  
  68.             this.lbl.TabIndex = 2;  
  69.             this.lbl.Text = "label1";  
  70.             //   
  71.             // Form1  
  72.             //   
  73.             this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);  
  74.             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;  
  75.             this.ClientSize = new System.Drawing.Size(284, 262);  
  76.             this.Controls.Add(this.lbl);  
  77.             this.Controls.Add(this.btn);  
  78.             this.Controls.Add(this.pictureBox1);  
  79.             this.Name = "Form1";  
  80.             this.Text = "Form1";  
  81.             this.FormClosed += new System.Windows.Forms.FormClosedEventHandler(this.Form1_FormClosed);  
  82.             this.Load += new System.EventHandler(this.Form1_Load);  
  83.             ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();  
  84.             this.ResumeLayout(false);  
  85.             this.PerformLayout();  
  86.   
  87.         }  
  88.  
  89.         #endregion  
  90.   
  91.         private System.Windows.Forms.PictureBox pictureBox1;  
  92.         private System.Windows.Forms.Button btn;  
  93.         private System.Windows.Forms.Label lbl;  
  94.     }  
  95. }  

运行结果:


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值