第三个目标:有若干张图片(自己确定,不少于5张,命名方法自定),窗体有3个控件(PictureBox,按钮,label);按下”随机显示“按钮,则随机显示一张图片,并给出该图片显示的次数在右边的label中;同时在”关闭“窗体时,把每张图片目前已经显示的次数写入到指定文件中,方便下次程序运行时使用。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Threading;
namespace randompicture
{
public partial class picture : Form
{
static int a = 0;
static int b = 0;
static int c = 0;
static int d = 0;
static int f = 0;
static int g = 0;
static int h = 0;
public picture()
{
InitializeComponent();
}
private void laltime_Click(object sender, EventArgs e)
{
laltime.Text = DateTime.Now.ToString();
}
private void picture_Load(object sender, EventArgs e)
{
}
private void pictureBox1_Click(object sender, EventArgs e)
{
}
private void label1_Click(object sender, EventArgs e)
{
}
private void random_Click(object sender, EventArgs e)
{
Random r = new Random();
int i = r.Next(0, 6);
pictureBox1.Image = imageList1.Images[i];
if (i == 0)
{
a++;
lblinfo.Text = a.ToString();
}
if (i == 1)
{
b++;
lblinfo.Text = b.ToString();
}
if (i == 2)
{
c++;
lblinfo.Text = c.ToString();
} if (i == 3)
{
d++;
lblinfo.Text = d.ToString();
} if (i == 4)
{
f++;
lblinfo.Text = f.ToString();
}
if (i == 5)
{
g++;
lblinfo.Text = g.ToString();
}
if (i == 6)
{
h++;
lblinfo.Text = h.ToString();
}
string text;
text = "图片1出现的次数:" + a +
"\r\n图片2出现的次数:" + b+
"\r\n图片3出现的次数:" + c +
"\r\n图片4出现的次数:" + d +
"\r\n图片5出现的次数:" + f +
"\r\n图片6出现的次数:" + g +
"\r\n图片7出现的次数:" + h;
string path = "C:\\Users\\FDZM\\Desktop\\show.txt";
File.WriteAllText(path, text);
}
}
}
结果演示:
<img src="https://img-blog.csdn.net/20150416083835429?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvRkRaTTE0/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
文本结果显示: