程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> c#記事本實現代碼

c#記事本實現代碼

編輯:關於C語言

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

如果需要全部源碼的請留言或者發郵件至[email protected]

版本: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;

}
}
}

}
}

其他代碼可以省略吧。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved