如何在fastcoloredtextbox中保存文件? [英] How do i save a file in fastcoloredtextbox?
问题描述
我正在用C#开发语法编辑器,您可以在 FastColoredTextBox 组件,然后将其另存为.html文件.但是,我有Save As
选项的代码.我唯一的问题是,当用户保存.html文件时,将弹出相同的Save As
对话框.但是我们之前已经保存了它.我只想按键盘上的Ctrl+S
键,它当然会保存为.html文件,然后会自动保存文件更改.
I'm developing a syntax editor in C# where you can write code in the FastColoredTextBox component, then saving it as a .html file. However, I have the code for the Save As
option. The only problem I have is when the user saves the .html file, the same Save As
dialog pops up. But we have already saved it before. I want to just press Ctrl+S
on the keyboard and it will automatically save the file changes after saving as a .html file of course.
这是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));
}
}
有人可以帮助我实现这一目标吗?请帮忙.
Can anyone help me to achieve this? Please help.
推荐答案
您应该执行其他建议,将其另存为扩展名为.html的文本文件,但是我在这里回答您的
You should do what the others suggested about saving it as a text file with a .html as extension as well, but I'm here to answer your ctrl + s
question. This is assuming you're on a winform (because you haven't specified yet):
yourForm.KeyPreview = true;
yourForm.KeyDown += new KeyEventHandler(Form_KeyDown);
您的处理程序应如下所示:
and your handler should look something like this:
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;
}
}
希望发芽的希望
这篇关于如何在fastcoloredtextbox中保存文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!