gpt4 book ai didi

c# - 为什么我的打开菜单项对话框打开保存文件对话框?

转载 作者:太空宇宙 更新时间:2023-11-03 16:24:25 25 4
gpt4 key购买 nike

<分区>

我制作了一个小型文本编辑器,可将 RichTextBox 中的文本保存到 .rtf 文件中,但每当我单击打开菜单选项时,它都会打开保存文件对话框。我不明白为什么有人可以帮助我?

这里是菜单对话框项的名称。

菜单条:menuStrip1保存:saveMenu打开:openMenuRichTextBox:richTextBox1

下面是我正在处理的代码...

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

//save the file

private void menuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{

// Text from the rich textbox rtfMain
string str = richTextBox1.Text;
// Create a new SaveFileDialog object
using (SaveFileDialog dlgSave = new SaveFileDialog())
try
{
// Available file extensions
dlgSave.Filter = "Rich Text Format (.rtf)|*.rtf";
// SaveFileDialog title
dlgSave.Title = "Save";
// Show SaveFileDialog
if (dlgSave.ShowDialog() == DialogResult.OK && dlgSave.FileName.Length > 0)
{
richTextBox1.SaveFile(dlgSave.FileName);

}
}
catch (Exception errorMsg)
{
MessageBox.Show(errorMsg.Message);
}


}

private void openMenu_ItemClicked(object sender, ToolStripItemClickedEventArgs e)

{
using (var of = new OpenFileDialog())
{
of.DefaultExt = "*.rtf";
of.Filter = "RTF Files|*.rtf";

if (of.ShowDialog() == DialogResult.OK)
richTextBox1.Rtf = System.IO.File.ReadAllText(of.FileName);
}



}

}
}

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com