c# 記事本 V2.0

來源:互聯網
上載者:User

在記事本V1.0的基礎上增加了編輯中的剪下,賦值,粘貼,刪除功能,對程式關閉前的處理有做了一下改動。

 

新增加的代碼:

//系統剪下板的操作
        private void 複製ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.textBox1.SelectedText != "")
            {
                Clipboard.SetDataObject(this.textBox1.SelectedText);
            }
            else
                MessageBox.Show("沒有選中文字!");
        }

        private void 粘帖ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            IDataObject iData = Clipboard.GetDataObject();

            //檢測資料是否是可以使用的格式,即文字格式設定
            if (iData.GetDataPresent(DataFormats.Text))
            {
                if (this.textBox1.SelectedText != "")
                {
                    textBox1.SelectedText = (String)iData.GetData(DataFormats.Text);
                }
                else
                {
                    textBox1.Text += (String)iData.GetData(DataFormats.Text);
                }
            }
            else
            {
                MessageBox.Show("沒有從剪下板中接收到資料!");
            }
        }

        private void 刪除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.textBox1.SelectedText = "";
        }

        private void 剪下XToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.textBox1.SelectedText != "")
            {
                Clipboard.SetDataObject(this.textBox1.SelectedText);
                this.textBox1.SelectedText = "";
            }
            else
                MessageBox.Show("沒有選中文字!");

        }

 

有改動的代碼:

 //關閉之前看檔案是否儲存
        private void MyNotePad_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (this.textBox1.Text != "")
            {
                if (!File.Exists(this.Text))
                {
                    DialogResult d = MessageBox.Show("檔案 " + this.Text + @" 的文字已經改變。
想儲存檔案嗎?", "MyNotePad", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly);
                    switch (d)
                    {
                        case DialogResult.Yes:
                            using (SaveFileDialog saveDig = new SaveFileDialog())
                            {
                                saveDig.Filter = @"文字文件(*.txt)|*.txt";
                                saveDig.FileName = "*.txt";
                                if (saveDig.ShowDialog() == DialogResult.OK)
                                {
                                    StreamWriter sw = new StreamWriter(saveDig.FileName, false, System.Text.Encoding.Default);
                                    sw.Write(this.textBox1.Text);
                                    sw.Close();
                                    this.Text = saveDig.FileName;
                                }
                            }
                            break;
                        case DialogResult.No:
                            break;
                        case DialogResult.Cancel:
                            break;

                        default: System.Diagnostics.Debug.Assert(false);
                            break;
                    }
                }
            }
        }

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.