c#記事本實現代碼

來源:互聯網
上載者:User

編輯模組只實現了全選和時間2個功能,自動換行的功能還沒寫。

如果需要全部源碼的請留言或者發郵件至henanlinzhoulcl@163.com

版本:1.0

主要代碼如下:

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

namespace MyNotePad
{
public partial class MyNotePad : Form
{
public MyNotePad()
{
InitializeComponent();
this.toolStripStatusLabel3.Text = DateTime.Now.DayOfWeek.ToString();
}

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
//textBox1.Text += e.KeyChar;
}

private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Exit();
}

private void 日期ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (this.textBox1.SelectedText != "")
{
this.textBox1.SelectedText = DateTime.Now.ToString();
}
else
{
this.textBox1.Text += DateTime.Now;
}
}

private void timer1_Tick(object sender, EventArgs e)
{
this.toolStripStatusLabel1.Text = "現在時間是:" + DateTime.Now.ToString();
}

private void 狀態列ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (this.statusStrip1.Visible == false)
{
this.statusStrip1.Visible = true;
}
else
{
this.statusStrip1.Visible = false;
this.textBox1.Height += 10;
}
}

private void 關於MyNotePadAToolStripMenuItem_Click(object sender, EventArgs e)
{
About ab = new About();
ab.Show();
}

private void 全選ToolStripMenuItem_Click(object sender, EventArgs e)
{
this.textBox1.SelectAll();
}

private void 字型ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (fontDialog1.ShowDialog() == DialogResult.OK)
{
textBox1.Font = fontDialog1.Font;
}
}

private void MyNotePad_Resize(object sender, EventArgs e)
{
this.panel1.Width = this.Size.Width - 25;
this.panel1.Height = this.Size.Height - 50;
this.textBox1.Width = this.panel1.Width;
this.textBox1.Height = this.panel1.Height;
}

private void 自動換行ToolStripMenuItem_Click(object sender, EventArgs e)
{

}

private void 儲存ToolStripMenuItem_Click(object sender, EventArgs e)
{
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;
}
}
}

private void 另存新檔ToolStripMenuItem_Click(object sender, EventArgs e)
{
using (SaveFileDialog saveDig = new SaveFileDialog())
{
saveDig.Filter = @"文字文件(*.txt)|*.txt";
saveDig.FileName = this.Text;
if (saveDig.ShowDialog() == DialogResult.OK)
{
StreamWriter sw = new StreamWriter(saveDig.FileName, false, System.Text.Encoding.Default);
sw.Write(this.textBox1.Text);
sw.Close();
}
}
}

private void 建立ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (this.textBox1.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:
this.textBox1.Text = "";
break;
case DialogResult.Cancel:
break;

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

}

}
}

private void 開啟ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (this.textBox1.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:
this.textBox1.Text = "";
break;
case DialogResult.Cancel:
break;

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

}
}
else
{
using (OpenFileDialog dlgText = new OpenFileDialog())
{
dlgText.Filter = @"(*.txt)|*.txt";
if (dlgText.ShowDialog() == DialogResult.OK)
{
if (File.Exists(dlgText.FileName))
{
StreamReader rStream = new StreamReader(dlgText.FileName, System.Text.Encoding.Default);
string s=default(string);
this.textBox1.Text = "";
while ((s = rStream.ReadLine()) != null)
{
this.textBox1.Text += s;
}
rStream.Close();
}
}
}
}
}

private void MyNotePad_FormClosing(object sender, FormClosingEventArgs e)
{
if (this.textBox1.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.