VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > c#编程 >
  • 用C#开发一个记事本程序

现我们就用C#开发一个记事本程序,这是一个很好的初学者项目。下面我们就简单讲解一下记事本程序的示例代码,它使用Windows Forms来创建图形用户界面(GUI)。
 
首先,确保你已经安装了Visual Studio,并且创建了一个新的Windows Forms App (.NET Framework)项目。
 
然后,在Form1.cs文件中,你可以添加以下代码:
 
using System;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;
 
namespace SimpleNotepad
{
    public partial class Form1 : Form
    {
        private string filePath = null;
        private bool isFileModified = false;
 
        public Form1()
        {
            InitializeComponent();
            this.Text = "Simple Notepad";
            this.richTextBox1.Font = new Font("Consolas", 10);
        }
 
        private void newToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (isFileModified)
            {
                DialogResult result = MessageBox.Show("Do you want to save changes?", "Save Changes", MessageBoxButtons.YesNoCancel);
                if (result == DialogResult.Yes)
                {
                    saveFile();
                }
                else if (result == DialogResult.Cancel)
                {
                    return;
                }
            }
 
            filePath = null;
            richTextBox1.Clear();
            isFileModified = false;
        }
 
        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            using (OpenFileDialog openFileDialog = new OpenFileDialog())
            {
                openFileDialog.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*";
                if (openFileDialog.ShowDialog() == DialogResult.OK)
                {
                    filePath = openFileDialog.FileName;
                    loadFile();
                }
            }
        }
 
        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (filePath == null)
            {
                saveFile();
            }
            else
            {
                File.WriteAllText(filePath, richTextBox1.Text);
                isFileModified = false;
            }
        }
 
        private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            saveFile();
        }
 
        private void saveFile()
        {
            using (SaveFileDialog saveFileDialog = new SaveFileDialog())
            {
                saveFileDialog.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*";
                if (saveFileDialog.ShowDialog() == DialogResult.OK)
                {
                    filePath = saveFileDialog.FileName;
                    File.WriteAllText(filePath, richTextBox1.Text);
                    isFileModified = false;
                }
            }
        }
 
        private void loadFile()
        {
            try
            {
                richTextBox1.Text = File.ReadAllText(filePath);
                isFileModified = false;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error loading file: " + ex.Message);
            }
        }
 
        private void richTextBox1_TextChanged(object sender, EventArgs e)
        {
            isFileModified = true;
            this.Text = "Simple Notepad*"; // Add an asterisk to indicate changes
        }
 
        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (isFileModified)
            {
                DialogResult result = MessageBox.Show("Do you want to save changes?", "Save Changes", MessageBoxButtons.YesNoCancel);
                if (result == DialogResult.Yes)
                {
                    saveFile();
                }
                else if (result == DialogResult.Cancel)
                {
                    return;
                }
            }
 
            Application.Exit();
        }
    }
}
 
上面的代码实现了一个简单的记事本程序,它包含了以下功能:
 
- 新建文件(清空当前文本)
- 打开文件(选择并加载一个文本文件)
- 保存文件(如果已打开文件,则保存当前文本到该文件;否则提示保存位置)
- 另存为(提示保存位置,并将当前文本保存到该位置)
- 退出程序(如果文本被修改,则提示是否保存)
 
你需要在Visual Studio中设计一个简单的菜单,包括“新建”、“打开”、“保存”、“另存为”和“退出”等选项,并将它们的事件与上面的代码中的对应方法关联起来。


最后,如果你对python语言还有任何疑问或者需要进一步的帮助,请访问https://www.xin3721.com 本站原创,转载请注明出处:https://www.xin3721.com/ArticlecSharp/c49070.html
 
 


相关教程