Requirements:
Introduction to the function of Goal 3: There are several pictures (self-determination, not less than 5, named Method Custom), the form has 3 controls (PictureBox, button, label), press the "Random display" button, the random display of a picture, and gives the number of times the picture is displayed in the label on the right, and in the close form, writes the number of times each picture is currently displayed to the specified file so that it is easy to use the next time the program runs.
You can add more features than the slice name is called "Study number name", display name above the picture (add a label); ; getting closer to playing "guess" (showing a photo, given 3 candidate names, guessing, or showing 3 photos, given 1 candidate names, guessing is that picture, etc.).
The first step: the visual design and the initialization of some attributes:
As shown in the following:
650) this.width=650; "src="/e/u261/themes/default/images/spacer.gif "style=" Background:url ("/e/u261/lang/zh-cn/ Images/localimage.png ") no-repeat center;border:1px solid #ddd;" alt= "Spacer.gif"/>650 "this.width=650;" src= "http ://s3.51cto.com/wyfs02/m00/6b/bf/wkiol1u2jlbaasx4aaezmofcq6y671.jpg "title=" TYOJNQ9L08X@XEFK) L ' 5 ' U0.png "alt=" Wkiol1u2jlbaasx4aaezmofcq6y671.jpg "/>
Step two: Encapsulate some events and functions in Form1.cs:
The code is as follows:
Form1.cs:
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;namespace Windowsformsapplication2{ public partial class form1 : form { public form1 () { InitializeComponent (); } private void show () { random x = new random (); int I = x.next (1, 10); string r = @ "c:\users\administrator.lbdz-20121019bz\documents\visual studio 2013\ Projects\windowsformsapplication2\windowsformsapplication2\imgdata\ "+i+". jpg "; bitmap img1 = new bitmap (R); pictureBox1.Image=img1; lbl1. text = "Random times:" + op; lbl2. text = "Picture:" +i + ". jpg"; } private void infoout () { using&nbSP; (Filestream afile = new filestream (@ "C:\Users\Administrator.LBDZ-20121019BZ\Documents\ Visual studio 2013\projects\windowsformsapplication2\windowsformsapplication2\imgdata\info.txt ", filemode.openorcreate)) { using ( Streamwriter sw = new streamwriter (Afile, encoding.utf8)) {               SW. WriteLine (LBL1. Text);    SW. WriteLine (the "currently displayed picture is" +lbl2.) Text); } } } private void button1_click (object Sender, eventargs e) { op++; show (); } private void form1_formclosed (object sender, formclosedeventargs e) { Infoout (); } }}
The specific function and property codes are as follows
Form1.Designer.cs:
namespace windowsformsapplication2{ partial class form1 { /// <summary> /// Required designer variable. /// </summary> private system.componentmodel.icontainer components = null; /// <summary> /// clean up any resources being used. /// </summary> /// <param name= "disposing" >true if managed resources should be disposed; otherwise, false.</param> &nbsP;protected override void dispose (bool disposing) { if (disposing && (components != null) { Components. Dispose (); } base. Dispose (disposing); } #region Windows Form Designer generated code /// <summary> /// required method for designer support - do not modify /// the contents of this method with the code editor. /// </ Summary> private void initializecomponent () { this.btn1 = new system.windows.forms.button (); this.picturebox1 = new system.windows.forms.picturebox (); this.lbl1 = new System.Windows.Forms.Label (); this.lbl2 = new system.windows.forms.label (); (System.componentmoDel. ISupportInitialize) (This.picturebox1)). BeginInit (); this. SuspendLayout (); // // btn1 // this.btn1.BackColor = System.Drawing.SystemColors.ActiveCaption; this.btn1.location = new system.drawing.point (87, 337); this.btn1.name = "BTN1"; this.btn1.size = new system.drawing.size (75, 23); this.btn1.tabindex = 0; this.btn1.text = "Random Display"; this.btn1.usevisualstylebackcolor = false; this.btn1.click += new system.eventhandler (This.button1_Click); // // pictureBox1 // this.picturebox1.imagelocation = "c:\\users\\administrator.lbdz-20121019bz\\documents\\visual Studio 2013\\projects\\wind " + " owsformsapplication2\\ Windowsformsapplication2\\imgdata\\1.jpg "; this.picturebox1.location = new System.Drawing.Point (87, 24); this.picturebox1.name = "PictureBox1"; this.picturebox1.size = new system.drawing.size (363, 280); this.pictureBox1.SizeMode = system.windows.forms.pictureboxsizemode.stretchimage; this.pictureBox1.TabIndex = 1; this.pictureBox1.TabStop = false; // lbl1 // this.lbl1.autosize = true; this.lbl1.backcolor = system.drawing.systemcolors.controllight; this.lbl1.location = new system.drawing.point (385, 342 ); this.lbl1.name = "LBL1"; this.lbl1.Size = new System.Drawing.Size (65, 12); this.lbl1.tabindex = 2; this.lbl1.text = "Random number: 0"; // // lbl2 // this.lbl2.autosize = true; this.lbl2.backcolor = system.drawing.systemcolors.controllight; this.lbl2.location = new system.drawing.point (268, 342 ); this.lbl2.name = "LBL2"; this.lbl2.Size = new System.Drawing.Size (65, 12); this.lbl2.tabindex = 3; this.lbl2.text = "Image: 1.jpG "; // // Form1 // this. Autoscaledimensions = new system.drawing.sizef (6f, 12f); this. autoscalemode = system.windows.forms.autoscalemode.font; this. backcolor = system.drawing.systemcolors.gradientactivecaption; this. Clientsize = new system.drawing.size (582, 377); this. Controls.Add (THIS.LBL2); &nBsp; this. Controls.Add (THIS.LBL1); this. Controls.Add (this.picturebox1); this. Controls.Add (THIS.BTN1); this. name = "Form1"; this. text = "Random picture display"; this. Formclosed += new system.windows.forms.formclosedeventhandler (this. form1_formclosed); (( System.ComponentModel.ISupportInitialize) (This.picturebox1)). EndInit (); this. ResumeLayout (false); this. PerformLayout (); } #endregion int op=0; private system.windows.forms.button btn1; private system.windows.forms.picturebox picturebox1; private system.windows.forms.label lbl1; private system.windows.forms.label lbl2; }}
Program.cs:
using system;using system.collections.generic;using system.linq;using system.threading.tasks;using system.windows.forms;namespace windowsformsapplication2{ static class Program { /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void main () { application.enablevisualstyles (); application.setcompatibletextrenderingdefault ( FALSE); &Nbsp; application.run (New form1 ()); } }}
Step three: Running results and files:
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/6B/C3/wKiom1U2JKrxGQL4AAGY0_08SC0894.jpg "title=" I~0yg " Du_jnk}v (N_ (}pm$j.png "alt=" Wkiom1u2jkrxgql4aagy0_08sc0894.jpg "/>
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/6B/BF/wKioL1U2JlOSjywqAAJM61oRHi8656.jpg "style=" float: none; "Title=" e0l925t__70[b1ju7l) opy3.jpg "alt=" Wkiol1u2jlosjywqaajm61orhi8656.jpg "/>
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/6B/BF/wKioL1U2Jq-D7_vuAADvMO2zVMU955.jpg "title=" Wbfpu48m1{gzd1 ' 39{lpyki.png "alt=" Wkiol1u2jq-d7_vuaadvmo2zvmu955.jpg "/>
Second job WinForm visual design of random picture display