📄 frmstudentfile.cs
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.IO;
using System.Text;
namespace Example_7
{
/// <summary>
/// Summary description for frmDisplay.
/// </summary>
public class frmStudentFile : System.Windows.Forms.Form
{
private System.Windows.Forms.Label lblGrade;
private System.Windows.Forms.Label lblClass;
private System.Windows.Forms.Label lblLastName;
private System.Windows.Forms.Label lblFirstName;
private System.Windows.Forms.Button btnExit;
private System.Windows.Forms.Label lblFirstNameValue;
private System.Windows.Forms.Label lblLastNameValue;
private System.Windows.Forms.Label lblClassValue;
private System.Windows.Forms.Label lblGradeValue;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public frmStudentFile()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(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.lblGrade = new System.Windows.Forms.Label();
this.lblClass = new System.Windows.Forms.Label();
this.lblLastName = new System.Windows.Forms.Label();
this.lblFirstName = new System.Windows.Forms.Label();
this.btnExit = new System.Windows.Forms.Button();
this.lblFirstNameValue = new System.Windows.Forms.Label();
this.lblLastNameValue = new System.Windows.Forms.Label();
this.lblClassValue = new System.Windows.Forms.Label();
this.lblGradeValue = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// lblGrade
//
this.lblGrade.Location = new System.Drawing.Point(19, 138);
this.lblGrade.Name = "lblGrade";
this.lblGrade.Size = new System.Drawing.Size(77, 25);
this.lblGrade.TabIndex = 7;
this.lblGrade.Text = "年级:";
//
// lblClass
//
this.lblClass.Location = new System.Drawing.Point(19, 103);
this.lblClass.Name = "lblClass";
this.lblClass.Size = new System.Drawing.Size(77, 25);
this.lblClass.TabIndex = 6;
this.lblClass.Text = "班级:";
//
// lblLastName
//
this.lblLastName.Location = new System.Drawing.Point(19, 60);
this.lblLastName.Name = "lblLastName";
this.lblLastName.Size = new System.Drawing.Size(77, 25);
this.lblLastName.TabIndex = 5;
this.lblLastName.Text = "姓氏:";
//
// lblFirstName
//
this.lblFirstName.Location = new System.Drawing.Point(19, 26);
this.lblFirstName.Name = "lblFirstName";
this.lblFirstName.Size = new System.Drawing.Size(87, 25);
this.lblFirstName.TabIndex = 4;
this.lblFirstName.Text = "姓名:";
//
// btnExit
//
this.btnExit.Location = new System.Drawing.Point(144, 181);
this.btnExit.Name = "btnExit";
this.btnExit.Size = new System.Drawing.Size(90, 25);
this.btnExit.TabIndex = 8;
this.btnExit.Text = "退出(&E)";
this.btnExit.Click += new System.EventHandler(this.btnExit_Click);
//
// lblFirstNameValue
//
this.lblFirstNameValue.Location = new System.Drawing.Point(104, 26);
this.lblFirstNameValue.Name = "lblFirstNameValue";
this.lblFirstNameValue.Size = new System.Drawing.Size(120, 25);
this.lblFirstNameValue.TabIndex = 9;
//
// lblLastNameValue
//
this.lblLastNameValue.Location = new System.Drawing.Point(104, 60);
this.lblLastNameValue.Name = "lblLastNameValue";
this.lblLastNameValue.Size = new System.Drawing.Size(120, 25);
this.lblLastNameValue.TabIndex = 10;
//
// lblClassValue
//
this.lblClassValue.Location = new System.Drawing.Point(104, 103);
this.lblClassValue.Name = "lblClassValue";
this.lblClassValue.Size = new System.Drawing.Size(120, 25);
this.lblClassValue.TabIndex = 11;
//
// lblGradeValue
//
this.lblGradeValue.Location = new System.Drawing.Point(104, 138);
this.lblGradeValue.Name = "lblGradeValue";
this.lblGradeValue.Size = new System.Drawing.Size(120, 25);
this.lblGradeValue.TabIndex = 12;
//
// frmStudentFile
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(240, 212);
this.Controls.Add(this.lblGradeValue);
this.Controls.Add(this.lblClassValue);
this.Controls.Add(this.lblLastNameValue);
this.Controls.Add(this.lblFirstNameValue);
this.Controls.Add(this.btnExit);
this.Controls.Add(this.lblGrade);
this.Controls.Add(this.lblClass);
this.Controls.Add(this.lblLastName);
this.Controls.Add(this.lblFirstName);
this.Name = "frmStudentFile";
this.Text = "学生文件";
this.Load += new System.EventHandler(this.frmDisplay_Load);
this.ResumeLayout(false);
}
#endregion
private void frmDisplay_Load(object sender, System.EventArgs e)
{
FileStream fstream = File.OpenRead("C:\\Student.txt");
long filesize = fstream.Length;
//create a byte array to read the data
byte[] arr = new byte[filesize];
UTF8Encoding data = new UTF8Encoding(true);
//read the contents of the file into an array
fstream.Read(arr,0,arr.Length);
//split the array to retrieve individual field values
string text = data.GetString(arr);
string delim = " ";
char [] delimiter = delim.ToCharArray();
string [] split = null;
for (int x = 1; x <= text.Length; x++)
{
split = text.Split(delimiter, x);
}
//assign the values to the respective field on the form
lblFirstNameValue.Text = split[0];
lblLastNameValue.Text = split[1];
lblClassValue.Text = split[2];
lblGradeValue.Text = split[3];
}
private void btnExit_Click(object sender, System.EventArgs e)
{
Application.Exit ();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -