我在C#中开发语法编辑器,您可以在FastColoredTextBox组件中编写代码,然后将其另存为.html文件。但是,我有Save As
选项的代码。我遇到的唯一问题是当用户保存.html文件时,弹出相同的Save As
对话框。但我们之前已经保存过了。我想只需按下键盘上的Ctrl+S
,它就会在保存为.html文件后自动保存文件更改。
以下是Save As
选项的代码。
private void toolStripButton2_Click(object sender, EventArgs e)
{
SaveFileDialog sfd = default(SaveFileDialog);
if (FastColoredTextBox1.Text.Length > 0)
{
sfd = new SaveFileDialog();
sfd.Filter = "HTML Files|.html|" + "All Files|*.*";
sfd.DefaultExt = "html";
sfd.ShowDialog();
string location = null;
string sourcecode = FastColoredTextBox1.Text;
location = sfd.FileName;
if (!object.ReferenceEquals(sfd.FileName, ""))
{
using (System.IO.StreamWriter writer = new System.IO.StreamWriter(location, false))
{
writer.Write(sourcecode);
writer.Dispose();
}
}
}
if (Directory.Exists(sfd.FileName) == true)
{
string location = sfd.InitialDirectory;
File.WriteAllText(location, (FastColoredTextBox1.Text));
}
}
任何人都可以帮助我实现这一目标吗?请帮忙。
答案 0 :(得分:1)
你应该做其他人建议将其保存为带有.html作为扩展名的文本文件,但我在这里回答你的ctrl + s
问题。这假设你是一个winform(因为你还没有指定):
yourForm.KeyPreview = true;
yourForm.KeyDown += new KeyEventHandler(Form_KeyDown);
并且您的处理程序应该如下所示:
void Form_KeyDown(object sender, KeyEventArgs e)
{
if (e.Control && e.KeyCode == Keys.S)
{
string sourceCode = FastColoredTextBox1.Text;
// not sure what's going on for you "location" but you need to do that logic here too
File.WriteAllText(location, sourceCode);
e.SuppressKeyPress = true;
}
}
希望有助于萌芽