⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form1.cs

📁 这是一个用txt文本实现数据库功能的小软件,具备新建,查询,修改等数据库基本功能,这对没有数据库的朋友是个不错的选择
💻 CS
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;

namespace htmlMaker
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.TextBox txbName;
        private System.Windows.Forms.TextBox txbFilename;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.Button btnGenerateCode;
        private System.Windows.Forms.Button btnClearAll;
        private System.Windows.Forms.Button btnExit;
        private System.Windows.Forms.StatusBar stbMsg;

        private string name = "";
        private string filename = "";
        private StreamWriter writer = null;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.Label label4;
		private System.Windows.Forms.TextBox scoretext;
		private System.Windows.Forms.TextBox numtext;
		private System.Windows.Forms.Button form2;

		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public Form1()
		{
			//
			// 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.label1 = new System.Windows.Forms.Label();
			this.label2 = new System.Windows.Forms.Label();
			this.btnExit = new System.Windows.Forms.Button();
			this.btnClearAll = new System.Windows.Forms.Button();
			this.btnGenerateCode = new System.Windows.Forms.Button();
			this.stbMsg = new System.Windows.Forms.StatusBar();
			this.txbFilename = new System.Windows.Forms.TextBox();
			this.txbName = new System.Windows.Forms.TextBox();
			this.label3 = new System.Windows.Forms.Label();
			this.scoretext = new System.Windows.Forms.TextBox();
			this.label4 = new System.Windows.Forms.Label();
			this.numtext = new System.Windows.Forms.TextBox();
			this.form2 = new System.Windows.Forms.Button();
			this.SuspendLayout();
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(32, 48);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(56, 16);
			this.label1.TabIndex = 0;
			this.label1.Text = "姓名";
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(32, 112);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(48, 16);
			this.label2.TabIndex = 0;
			this.label2.Text = "文件名";
			// 
			// btnExit
			// 
			this.btnExit.Location = new System.Drawing.Point(176, 152);
			this.btnExit.Name = "btnExit";
			this.btnExit.TabIndex = 7;
			this.btnExit.Text = "退  出";
			this.btnExit.Click += new System.EventHandler(this.btnExit_Click);
			// 
			// btnClearAll
			// 
			this.btnClearAll.Location = new System.Drawing.Point(96, 152);
			this.btnClearAll.Name = "btnClearAll";
			this.btnClearAll.TabIndex = 6;
			this.btnClearAll.Text = "清  空";
			this.btnClearAll.Click += new System.EventHandler(this.btnClearAll_Click);
			// 
			// btnGenerateCode
			// 
			this.btnGenerateCode.Location = new System.Drawing.Point(16, 152);
			this.btnGenerateCode.Name = "btnGenerateCode";
			this.btnGenerateCode.TabIndex = 5;
			this.btnGenerateCode.Text = "保存信息";
			this.btnGenerateCode.Click += new System.EventHandler(this.btnGenerateCode_Click);
			// 
			// stbMsg
			// 
			this.stbMsg.Location = new System.Drawing.Point(0, 205);
			this.stbMsg.Name = "stbMsg";
			this.stbMsg.Size = new System.Drawing.Size(348, 20);
			this.stbMsg.TabIndex = 3;
			// 
			// txbFilename
			// 
			this.txbFilename.Location = new System.Drawing.Point(104, 104);
			this.txbFilename.Name = "txbFilename";
			this.txbFilename.Size = new System.Drawing.Size(200, 21);
			this.txbFilename.TabIndex = 4;
			this.txbFilename.Text = "b121.txt";
			// 
			// txbName
			// 
			this.txbName.Location = new System.Drawing.Point(104, 40);
			this.txbName.Name = "txbName";
			this.txbName.Size = new System.Drawing.Size(200, 21);
			this.txbName.TabIndex = 2;
			this.txbName.Text = "";
			// 
			// label3
			// 
			this.label3.Location = new System.Drawing.Point(32, 80);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(56, 16);
			this.label3.TabIndex = 4;
			this.label3.Text = "成绩:";
			// 
			// scoretext
			// 
			this.scoretext.Location = new System.Drawing.Point(104, 72);
			this.scoretext.Name = "scoretext";
			this.scoretext.Size = new System.Drawing.Size(200, 21);
			this.scoretext.TabIndex = 3;
			this.scoretext.Text = "";
			// 
			// label4
			// 
			this.label4.Location = new System.Drawing.Point(32, 16);
			this.label4.Name = "label4";
			this.label4.Size = new System.Drawing.Size(48, 16);
			this.label4.TabIndex = 6;
			this.label4.Text = "编号";
			// 
			// numtext
			// 
			this.numtext.Location = new System.Drawing.Point(104, 8);
			this.numtext.Name = "numtext";
			this.numtext.Size = new System.Drawing.Size(200, 21);
			this.numtext.TabIndex = 1;
			this.numtext.Text = "";
			this.numtext.TextChanged += new System.EventHandler(this.numtext_TextChanged);
			// 
			// form2
			// 
			this.form2.Location = new System.Drawing.Point(256, 152);
			this.form2.Name = "form2";
			this.form2.Size = new System.Drawing.Size(72, 24);
			this.form2.TabIndex = 8;
			this.form2.Text = "修改信息";
			this.form2.Click += new System.EventHandler(this.form2_Click);
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
			this.ClientSize = new System.Drawing.Size(348, 225);
			this.Controls.Add(this.form2);
			this.Controls.Add(this.numtext);
			this.Controls.Add(this.label4);
			this.Controls.Add(this.scoretext);
			this.Controls.Add(this.label3);
			this.Controls.Add(this.stbMsg);
			this.Controls.Add(this.btnExit);
			this.Controls.Add(this.btnClearAll);
			this.Controls.Add(this.btnGenerateCode);
			this.Controls.Add(this.txbFilename);
			this.Controls.Add(this.label2);
			this.Controls.Add(this.txbName);
			this.Controls.Add(this.label1);
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow;
			this.Name = "Form1";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "添加学生信息";
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new Form1());
		}

        private void btnGenerateCode_Click(object sender, System.EventArgs e)
        {
			string i = this.numtext.Text;
			string a = this.scoretext.Text;
			int b ;
			int c ;
			try
			{
				b = int.Parse(i);
				c = int.Parse(a);
			}
			catch
			{
				MessageBox.Show("错误:编号和成绩不能是英文");
			} 
			//以上的代码是用来限制编号和成绩的输入(只能输入数字,否则就会提示出错)
            name = txbName.Text.Trim();
            filename = txbFilename.Text.Trim();

            if((txbName.Text == "") || (txbFilename.Text == "") || (numtext.Text == "") || (scoretext.Text == ""))
            {
                stbMsg.Text = "信息提示:请先输入\"编号\"和\"姓名\"和\"成绩\"和\"文件名\"";
                txbName.Focus();
            }			
            else
            {
                if(GenerateCode())
                {
                    stbMsg.Text = "已创建文件" + filename;
                }
                else
                {
                    stbMsg.Text = "创建文件失败!";
                }
            }
        }

        private void btnClearAll_Click(object sender, System.EventArgs e)
        {
			txbName.Text = "";
            txbFilename.Text = "";
			numtext.Text = "" ;
			scoretext.Text="";
            stbMsg.Text = "清空所有输入框";
        }

        private bool GenerateCode()
        {
            // 文件存在时是否覆盖
           // FileInfo f = new FileInfo(filename);
           // if(f.Exists)
           // {
             //   DialogResult s = MessageBox.Show("该文件已经存在,是否覆盖?" , "警告", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation);
              //  if(String.Format("{0}" , s) == "Yes")
               // {
                //    f.Delete();
               // }
               // else
               // {
                   // s = MessageBox.Show("请输入不同的文件名!" , "警告");
                 //   txbFilename.Focus();
                  //  return false;
               // }				
         //   }
			//说明:以上验证如果加入,将只能加一次,并转换文本,这样子一个文本文件只能存贮一个学生信息,这样的做法不推荐,故不采用...

            // 写文件
            FileStream outputfile = null;
            try
            {
                outputfile = new FileStream(filename,FileMode.OpenOrCreate, FileAccess.Write);
                writer = new StreamWriter(outputfile);
                writer.BaseStream.Seek(0, SeekOrigin.End);
                DoWrite ("编号:"+ numtext.Text);
                DoWrite ("姓名:"+ txbName.Text);
                DoWrite ("成绩:"+ scoretext.Text);
				DoWrite ("--------" );
                writer.Close();
            }
            catch(Exception ex)
            {
                Console.WriteLine("Exception GenerateCode = "+ex);
                stbMsg.Text = "Error";
                outputfile = null;
                writer = null;
                return false;
            }
            return true;
        }

        private void DoWrite (String line)
        {
			line += "\r\n";			// 加上换行符
            
			byte[] binfo = new System.Text.UTF8Encoding(true).GetBytes(line);
			char[] info = new System.Text.UTF8Encoding(true).GetChars(binfo);

			writer.Write(info,0,info.Length);
			writer.Flush();
            //writer.WriteLine(line);
            //writer.Flush();
        }

        private void btnExit_Click(object sender, System.EventArgs e)
        {
            this.Close();
        }

		private void form2_Click(object sender, System.EventArgs e)
		{
			Form2 frm = new Form2();//创建一个登录窗口
			frm.ShowDialog();
		}

		private void numtext_TextChanged(object sender, System.EventArgs e)
		{
		
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -