将字符串从一个目标设置到另一个目标

本文关键字:目标 一个 设置 另一个 字符串 | 更新日期: 2023-09-27 17:56:03

我想在第二个富文本框中包含字符串"oldSummary",但是摘要的所有功能都属于打开文件后的streamRead。有没有办法在单击 btn1 按钮时,它将显示oldSummary字符串?目前它是空白的,因为它的全局字符串设置为 ",但我希望它显示在mnuOpen按钮中设置的oldSummary字符串。

string oldSummary = "";
private void mnuOpen_Click(object sender, EventArgs e)
{
    //Load up file code which I remove for this example but goes here…
    //Add data from text file to rich text box
    richTextBox1.LoadFile(Chosen_File, RichTextBoxStreamType.PlainText);
    //Read lines of text in text file                
    string textLine = "";
    int lineCount = 0;
    System.IO.StreamReader txtReader;
    txtReader = new System.IO.StreamReader(Chosen_File);
    do
    {
        textLine = textLine + txtReader.ReadLine() + " ";
        lineCount++;
    }
    //Read line until there is no more characters
    while (txtReader.Peek() != -1);
    //seperate certain characters in order to find words
    char[] seperator = (" " + nl).ToCharArray();
    //number of words, characters and include extra line breaks variable
    int numberOfWords = textLine.Split(seperator, StringSplitOptions.RemoveEmptyEntries).Length;
    int numberOfChar = textLine.Length - lineCount;
    string divider = "------------------------";
    //Unprocessed Summary
    string oldSummary = "Word Count: " + numberOfWords + "Characters Count: " + numberOfChar + divider;
    txtReader.Close();
}
private void btn1_Click(object sender, EventArgs e)
{
    string wholeText = "";
    string copyText = richTextBox1.Text;
    wholeText = oldSummary + copyText;
    richTextBox2.Text = wholeText;
}

将字符串从一个目标设置到另一个目标

如果要使用全局变量 oldSummary,则不要在菜单打开事件处理程序中重新声明具有相同名称的变量,只需使用全局变量

//Unprocessed Summary
oldSummary = "Word Count: " + numberOfWords + "Characters Count: " + numberOfChar + divider;

尝试替换:

string oldSummary = "Word Count: " + numberOfWords + "Characters Count: " + numberOfChar + divider;

跟:

oldSummary = "Word Count: " + numberOfWords + "Characters Count: " + numberOfChar + divider;

以便将值分配给 btn1_Click 中使用的类字段。

相关文章: