C# 小白初入,請各位大神看看能不能最佳化下

來源:互聯網
上載者:User

標籤: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# 小白初入,請各位大神看看能不能最佳化下

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.