C#记事本小程序

1、小程序图片

在这里插入图片描述

2、代码实现:

2.1打开对话框:

 private void OpenToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            //打开对话框标题
            openFileDialog.Title = "请选择你需要加载的文件";
            //选择文本类型
            openFileDialog.Filter = "文本文件|*.txt|所有文件|*.*";
            //可以多选
            openFileDialog.Multiselect = true;
            //初始路径
            openFileDialog.InitialDirectory = @"G:\Desktop";
            openFileDialog.ShowDialog();
            //获得用户选中文件路径
            string path = openFileDialog.FileName;
            //将文件路径添加到一个泛型集合中
            list.Add(path);
            //将用户选中文件添加到listbox1控件中作为浏览记录
            listBox1.Items.Add(Path.GetFileName(path));
            //如果没有选择文件,则直接跳出这个方法
            if (string.IsNullOrEmpty(path))
            {
                return;
            }
            //通过FileStream类将用户选中文件读到textbox1控件中
            using (FileStream fileStream = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Read))
            {
                byte[] byteRead = new byte[1024 * 1024];
                int cow = fileStream.Read(byteRead, 0, byteRead.Length);
                textBox1.Text = Encoding.UTF8.GetString(byteRead,0,cow);
            }
        }

2.2、保存对话框

 private void SaveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.Title = "请选择要保存路径";
            saveFileDialog.InitialDirectory = @"G:\Desktop";
            saveFileDialog.Filter = "文本文件|*.txt|所有文件|*.*";
            saveFileDialog.ShowDialog();

            string path = saveFileDialog.FileName;
            using (FileStream fileStream=new FileStream(path,FileMode.OpenOrCreate,FileAccess.Write))
            {
                byte[] byteWrite = Encoding.Default.GetBytes(textBox1.Text);
                fileStream.Write(byteWrite, 0, byteWrite.Length);
            }
            MessageBox.Show("保存成功!");
        }

2.3、字体对话框

private void FontToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FontDialog fontDialog = new FontDialog();
            fontDialog.ShowDialog();
            //将textbox1中字体换为用户选中的字体
            textBox1.Font = fontDialog.Font;
        }

2.4、颜色对话框

private void ColorToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ColorDialog colorDialog = new ColorDialog();
            colorDialog.ShowDialog();
            textBox1.ForeColor = colorDialog.Color;
        }

总结

希望可以帮到和我一样刚接触C#程序的同学,希望大家一起进步。
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值