c#简单记事本应用程序的快捷方式_用C#做一个简单记事本

1.我们先创建一个.NET Framework桌面应用程序。如下图:

创建项目

把程序命名为记事本

2. 放一个菜单栏写成下图:

3.放一个RichTextBox,把滚动条设为有竖向滚动条。

4、放一个Panel,里面放一个ListBox和Button。

5.给打开注册一个单击事件,代码如下:

List paths = new List();

private void 打开ToolStripMenuItem_Click(object sender, EventArgs e)

{

OpenFileDialog ofd = new OpenFileDialog();

ofd.Title = "请选择要打开的文件";

ofd.Filter = "文本文件(*.txt)|*.txt|rtf文件(*.rtf)|*.rtf|身份证文件(*.sfz)|*.sfz";

ofd.Multiselect = true;

ofd.ShowDialog();

<
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单记事本功能的 C# 代码示例: ``` using System; using System.IO; using System.Windows.Forms; namespace Notepad { public partial class Form1 : Form { string filePath = ""; public Form1() { InitializeComponent(); } private void NewFile() { if (textBox1.Text.Length > 0) { DialogResult result = MessageBox.Show("Do you want to save changes to this file?", "Notepad", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question); if (result == DialogResult.Yes) { Save(); } else if (result == DialogResult.Cancel) { return; } } textBox1.Text = ""; filePath = ""; } private void Open() { OpenFileDialog openFileDialog = new OpenFileDialog(); openFileDialog.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*"; if (openFileDialog.ShowDialog() == DialogResult.OK) { using (StreamReader reader = new StreamReader(openFileDialog.FileName)) { textBox1.Text = reader.ReadToEnd(); filePath = openFileDialog.FileName; } } } private void Save() { if (filePath.Length > 0) { using (StreamWriter writer = new StreamWriter(filePath)) { writer.Write(textBox1.Text); } } else { SaveFileDialog saveFileDialog = new SaveFileDialog(); saveFileDialog.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*"; if (saveFileDialog.ShowDialog() == DialogResult.OK) { using (StreamWriter writer = new StreamWriter(saveFileDialog.FileName)) { writer.Write(textBox1.Text); filePath = saveFileDialog.FileName; } } } } private void SaveAs() { SaveFileDialog saveFileDialog = new SaveFileDialog(); saveFileDialog.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*"; if (saveFileDialog.ShowDialog() == DialogResult.OK) { using (StreamWriter writer = new StreamWriter(saveFileDialog.FileName)) { writer.Write(textBox1.Text); filePath = saveFileDialog.FileName; } } } private void Form1_FormClosing(object sender, FormClosingEventArgs e) { if (textBox1.Text.Length > 0) { DialogResult result = MessageBox.Show("Do you want to save changes to this file?", "Notepad", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question); if (result == DialogResult.Yes) { Save(); } else if (result == DialogResult.Cancel) { e.Cancel = true; } } } private void newToolStripMenuItem_Click(object sender, EventArgs e) { NewFile(); } private void openToolStripMenuItem_Click(object sender, EventArgs e) { Open(); } private void saveToolStripMenuItem_Click(object sender, EventArgs e) { Save(); } private void saveAsToolStripMenuItem_Click(object sender, EventArgs e) { SaveAs(); } private void exitToolStripMenuItem_Click(object sender, EventArgs e) { Close(); } } } ``` 这个记事本应用程序基于 Windows 窗体,包括菜单栏、工具栏和一个文本框用于编辑文本。它支持新建、打开、保存、另存为和退出功能。它使用 OpenFileDialog 和 SaveFileDialog 对话框来打开和保存文件。在关闭应用程序时,如果存在未保存的更改,会提示用户保存更改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值