-1

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+Son the keyboard and it will automatically save the file changes after saving as a .html file of course.

Here's the code I have for the Save As option.

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.

Capn Jack
  • 1,201
  • 11
  • 28
Robin
  • 127
  • 10
  • If you want to save it, don't use the SaveFileDialog, just use File.WriteAllText. –  Aug 02 '17 at 13:47
  • Along with @Will's suggestion, if you want this to be bound to control+s, you'll need to have a Key_Down event handler on your form (assuming this is winforms). You'll then need to check if it's already been saved, and just use File.WriteAllText, or if it hasn't been saved, launch the SaveFileDialog. EDIT: I see this is on a toolstrip button click now. Instead of using the Key_Down event, you can use the properties of the button to bind to ctrl+s. All else stays the same. – Michael Aug 02 '17 at 13:50
  • Can you specify as to whether this is winforms or not? Also please consider doing the best you can at presenting your questions in good grammatical fashion. Seeing a question with poor grammar may make some users feel as though you aren't really invested in the question. – Capn Jack Aug 02 '17 at 13:51
  • 1
    And that's the problem of copying code from Internet without understanding it... Just store the file name the first time and don't show the dialog if 'ts set. – Gusman Aug 02 '17 at 13:56

1 Answers1

0

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;
        }
    }

Hope that helps bud

Capn Jack
  • 1,201
  • 11
  • 28