C# 小白初入,请各位大神看看能不能优化下

2021-03-29 02:27

阅读:463

标签:namespace   文件   成功   textbox   down   names   model   不能   string   

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

 

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        //编辑框1内容变动产生触发事件 功能区
        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            if (textBox2.Text == "")
            {
                textBox1.Text = "";
                textBox2.Focus();
                label5.Text = "请先输入编码长度";
                label5.Visible = true;
            }
            else
            {
                textBox2.Enabled = false;
                label5.Text = "双击编码长度字样可解锁编辑";
                label5.Visible = true;
                textBox1.MaxLength = Convert.ToInt32(textBox2.Text);
                if (textBox1.MaxLength == textBox1.Text.Length)
                {
                    String text = textBox1.Text;
                    int indexToText = richTextBox1.Find(text,RichTextBoxFinds.MatchCase);
                    if (indexToText >= 0)
                    {
                        textBox1.Text = "";
                        textBox1.Focus();
                    }
                    else
                    {
                        //+ System.Environment.NewLine 此命令可以为richtextbox.text内容进行自动换行
                        richTextBox1.AppendText(textBox1.Text + System.Environment.NewLine);
                        textBox1.Text = "";
                        //下面两行代码实现richTextBox1.text内容与滚动条一起滚动
                        this.richTextBox1.SelectionStart = this.richTextBox1.TextLength;
                        this.richTextBox1.ScrollToCaret();                      
                    }
                }
            }          
        }

        //编码长度标签被双击事件 功能区
        private void label2_DoubleClick(object sender, EventArgs e)
        {
            textBox2.Enabled = true;
        }

        //编辑框3回车键按下事件功能区
        private void textBox3_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                    String text = textBox3.Text;
                    int Text3 = richTextBox1.Find(text, RichTextBoxFinds.MatchCase);
                    if (Text3 >= 0)
                    {
                        label4.Text = "已扫描";
                    }
                    else
                    {
                        label4.Text = "此码不存在";
                        textBox3.Text = "";
                    }
            }
        }

        //生成文件按钮功能区
        private void button1_Click(object sender, EventArgs e)
        {
            System.Windows.Forms.SaveFileDialog bcSave = new System.Windows.Forms.SaveFileDialog();
            bcSave.Filter = "文本文件|*.txt|" + "WordPad文件|*.rtf|" + "Word文件|*.doc|" + "Excel文件|*.xlsx|" + "所有文件|*.*";
            /* 此行按时间自动设备文件名
            bcSave.FileName = "自定义文件名" + DateTime.Now.ToString("yyyyMMddHHmm") + ".txt";
             */

          if (bcSave.ShowDialog() == DialogResult.OK)
            {
              richTextBox1.SaveFile(bcSave.FileName, RichTextBoxStreamType.PlainText);
              MessageBox.Show("文件已成功保存");             
            }
        }

        //清除按钮功能区
        private void button2_Click(object sender, EventArgs e)
        {
            if (richTextBox1.Text != "")
            {
                richTextBox1.Text = "";
                MessageBox.Show("记录清除成功");
            }
            else
            {
                MessageBox.Show("快点工作吧,点来点去的");
            }
           
        }

     }
}

C# 小白初入,请各位大神看看能不能优化下

标签:namespace   文件   成功   textbox   down   names   model   不能   string   

原文地址:https://www.cnblogs.com/lipstoo/p/9311894.html


评论


亲,登录后才可以留言!