📄 mainform.cs
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.IO;
namespace ApplicationForm
{
/// <summary>
/// Summary description for Main.
/// </summary>
public class MainForm : System.Windows.Forms.Form
{
private System.Windows.Forms.Button btnAdd;
private System.Windows.Forms.ComboBox cboSelectItem;
private System.Windows.Forms.Button btnExit;
private System.Windows.Forms.Label lblInfo;
private System.Windows.Forms.Button btnDisplay;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
private FileStream fsStudent, fsEmployee;
public MainForm()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
[STAThread]
static void Main()
{
Application.Run(new MainForm());
}
#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.btnAdd = new System.Windows.Forms.Button();
this.cboSelectItem = new System.Windows.Forms.ComboBox();
this.btnExit = new System.Windows.Forms.Button();
this.lblInfo = new System.Windows.Forms.Label();
this.btnDisplay = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// btnAdd
//
this.btnAdd.Location = new System.Drawing.Point(29, 95);
this.btnAdd.Name = "btnAdd";
this.btnAdd.Size = new System.Drawing.Size(90, 25);
this.btnAdd.TabIndex = 1;
this.btnAdd.Text = "添加(&A)";
this.btnAdd.Click += new System.EventHandler(this.btnAdd_Click);
//
// cboSelectItem
//
this.cboSelectItem.Items.AddRange(new object[] {
"Student",
"Employee"});
this.cboSelectItem.Location = new System.Drawing.Point(157, 34);
this.cboSelectItem.Name = "cboSelectItem";
this.cboSelectItem.Size = new System.Drawing.Size(173, 20);
this.cboSelectItem.TabIndex = 0;
//
// btnExit
//
this.btnExit.DialogResult = System.Windows.Forms.DialogResult.Cancel;
this.btnExit.ImeMode = System.Windows.Forms.ImeMode.NoControl;
this.btnExit.Location = new System.Drawing.Point(240, 95);
this.btnExit.Name = "btnExit";
this.btnExit.Size = new System.Drawing.Size(90, 25);
this.btnExit.TabIndex = 3;
this.btnExit.Text = "退出(&X)";
this.btnExit.Click += new System.EventHandler(this.btnExit_Click);
//
// lblInfo
//
this.lblInfo.Location = new System.Drawing.Point(19, 34);
this.lblInfo.Name = "lblInfo";
this.lblInfo.Size = new System.Drawing.Size(120, 25);
this.lblInfo.TabIndex = 17;
this.lblInfo.Text = "选择类别:";
//
// btnDisplay
//
this.btnDisplay.Location = new System.Drawing.Point(134, 95);
this.btnDisplay.Name = "btnDisplay";
this.btnDisplay.Size = new System.Drawing.Size(90, 25);
this.btnDisplay.TabIndex = 2;
this.btnDisplay.Text = "显示(&D)";
this.btnDisplay.Click += new System.EventHandler(this.btnDisplay_Click);
//
// MainForm
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(355, 134);
this.Controls.Add(this.btnDisplay);
this.Controls.Add(this.lblInfo);
this.Controls.Add(this.btnExit);
this.Controls.Add(this.cboSelectItem);
this.Controls.Add(this.btnAdd);
this.Name = "MainForm";
this.SizeGripStyle = System.Windows.Forms.SizeGripStyle.Show;
this.Text = "主窗体";
this.ResumeLayout(false);
}
#endregion
//Show modal form to add student details or employee details depends on option selected
private void btnAdd_Click(object sender, System.EventArgs e)
{
if(string.Compare(this.cboSelectItem.Text.Trim(),"Student",false)==0)
{
AddStudentForm objStdForm = new AddStudentForm();
objStdForm.ShowDialog();
}
else if (string.Compare(this.cboSelectItem.Text.Trim(),"Employee",false)==0)
{
AddEmployeeForm objEmpForm = new AddEmployeeForm();
objEmpForm.ShowDialog();
}
}
//Close the form
private void btnExit_Click(object sender, System.EventArgs e)
{
this.Close();
}
//show the details of student or employee
private void btnDisplay_Click(object sender, System.EventArgs e)
{
fsStudent = File.Open("C:\\Student.txt",FileMode.OpenOrCreate,FileAccess.ReadWrite);
fsEmployee = File.Open("C:\\Employee.txt",FileMode.OpenOrCreate,FileAccess.ReadWrite);
if(string.Compare(this.cboSelectItem.Text.Trim(),"Student",false)==0)
{
if(fsStudent.Length!=0)
{
this.fsStudent.Close();
this.fsEmployee.Close();
DisplayStudentDetailsForm objStdDtlForm = new DisplayStudentDetailsForm();
objStdDtlForm.ShowDialog();
}
else
{
this.fsStudent.Close();
this.fsEmployee.Close();
MessageBox.Show("文件为空:未找到任何记录");
}
}
else if (string.Compare(this.cboSelectItem.Text.Trim(),"Employee",false)==0)
{
if(fsEmployee.Length!=0)
{
this.fsEmployee.Close();
this.fsStudent.Close();
DisplayEmployeeDetailsForm objEmpDtlForm = new DisplayEmployeeDetailsForm();
objEmpDtlForm.ShowDialog();
}
else
{
this.fsEmployee.Close();
this.fsStudent.Close();
MessageBox.Show("文件为空:未找到任何记录");
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -