首页 > 编程开发 > Objective-C编程 >
-
c#记事本实现代码
编辑模块只实现了全选和时间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;
}
}
}
}
}
其他代码可以省略吧。
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);