Thursday, 20 September 2012

29. Japanese candlestiscks and C# - RichTextBox

How to have comments and use RichTextBox control? I can enter free text in commenT (control name) RichTextBox and save it for later. It means when form is loaded or activated commneT is filled with text saved in disk.

So, how to save data from RichTextBox to file. Under menu item Comments are Save and Load code.

This code is used to save content:

private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (activeIndice.Text == "") { MessageBox.Show("No indice name", "Saving error - Select indice"); goto end1; } // to save file name I need index name
            makeSaveFileName msfN = new makeSaveFileName(gloB.workingPath, activeIndice.Text, true); // fix and make file name and path
            string fileNameFull = msfN.indexPathName;
            string fileName = msfN.indexName;
            fileNameFull = fileNameFull.Replace(fileName, fileName + "Comment"); // full file name is finished
            File.WriteAllText(fileNameFull, commenT.Text); // save stream to file
        end1: ;
        }


This is code which fills RichTextBox from text file:

 private void loadToolStripMenuItem_Click(object sender, EventArgs e)
        {

            if (activeIndice.Text == "") { MessageBox.Show("No indice name", "Loading error - Select indice"); goto end1; } // to read file name I need index name
            makeSaveFileName msfN = new makeSaveFileName(gloB.workingPath, activeIndice.Text, true); // fix and make file name and path
            string fileNameFull = msfN.indexPathName;
            string fileName = msfN.indexName;
            fileNameFull = fileNameFull.Replace(fileName, fileName + "Comment"); // full name is finished
            if (File.Exists(fileNameFull))
            { commenT.Text = File.ReadAllText(fileNameFull); } // load file to form field
          
        end1: ;
        }


These codes could be under buttons, no difference.

Code for class MakeFileSaveName which fixes file name and generates filename and path. from indices's name. You can use absolute string definitions for fileNameFull and FileName and comment class:

 internal class makeSaveFileName // construct file name from global path and selected index
    {
        public makeSaveFileName(string patH, string indeX, Boolean daNe)
        {
            indeX = indeX.Replace(":", "-");
            if (daNe== true) indeX = indeX.Replace("CZ", "HR");
            indexPathName = patH + indeX + ".txt";    // make full path,file name and add .txt extension as it is text file
            indexName=indeX;
        }
        public string indexPathName { get; private set; }
        public string indexName { get; private set;}
    }


And small trick if we want to automate saving process. If we put code under event LostFocus of commenT control text we'll be saved as soon as we leave commenT field. We call the procedure under menu control (in bold).

private void commenT_LostFocus(object sender, EventArgs e)
{ saveToolStripMenuItem.PerformClick(); }


No comments:

Post a Comment