When you click the save text button written in RichTextBox , the OpenFileDialog displayed. The first five words from the first line in the RichTextBox should be the name of the file when saving. How to do it?

  private void buttonSave_Click(object sender, EventArgs e) { SaveFileDialog svf = new SaveFileDialog(); svf.Filter = "Text Files (.rtf)|*.rtf"; svf.Title = "Save"; if (svf.ShowDialog() == DialogResult.OK) { System.IO.StreamWriter sw = new System.IO.StreamWriter(svf.FileName); sw.Write(richTextBox1.Text); sw.Close(); } } 

    1 answer 1

     private void buttonSave_Click(object sender, EventArgs e) { SaveFileDialog svf = new SaveFileDialog(); var firstLine = richTextBox1.Lines.FirstOrDefault(); if (!String.IsNullOrEmpty(firstLine)) { svf.FileName = String.Join(" ", firstLine.Split(' ').Take(5)); } ... } 

    Read:

    • Captures already a few sentences. Can I somehow limit the name to about 5 words? - Aker
    • @Kaya possible. Update the question. - MihailPw
    • one
      @Kaya updated. - MihailPw