如何在 fastcoloredtextbox 中保存文件?
How do i save a file in fastcoloredtextbox?
我正在用 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));
}
}
谁能帮我实现这个目标?请帮忙。
您应该按照其他人的建议将其另存为扩展名为 .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;
}
}
希望对萌芽有所帮助
我正在用 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));
}
}
谁能帮我实现这个目标?请帮忙。
您应该按照其他人的建议将其另存为扩展名为 .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;
}
}
希望对萌芽有所帮助