如何更改文件后保存/更新文件?

时间:2017-10-20 17:36:53

标签: c# winforms

在SlectedIndexChanged事件中,我正在读取文件内容并将内容添加到richTextBox。

void listView_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ListViewCostumControl.SelectedItems.Count > 0)
            {
                results = new List<int>();
                richTextBox1.Text = File.ReadAllText(ListViewCostumControl.Items[ListViewCostumControl.SelectedIndices[0]].Text);
                FileInfo fi = new FileInfo(ListViewCostumControl.Items[ListViewCostumControl.SelectedIndices[0]].Text);
                label17.Text = ExtensionMethods.ToFileSize(fi.Length);
                label17.Visible = true;
                label26.Text = fi.Name;
                label26.Visible = true;
                button5.Enabled = true;
                filePath = Path.GetDirectoryName(fi.FullName);
                string word = textBox1.Text;
                string[] test = word.Split(new string[] { ",," }, StringSplitOptions.None);
                foreach (string myword in test)
                {
                    HighlightPhrase(richTextBox1, myword, Color.Yellow);
                    label16.Text = results.Count.ToString();
                    label16.Visible = true;
                    if (results.Count > 0)
                    {
                        numericUpDown1.Maximum = results.Count;
                        numericUpDown1.Enabled = true;
                        richTextBox1.SelectionStart = results[(int)numericUpDown1.Value - 1];
                        richTextBox1.ScrollToCaret();
                    }
                }
            }
        }

然后我在richTextBox中键入一些文本或删除文本。

private void button5_Click(object sender, EventArgs e)
        {
            if (label4.Text == "Viewer")
            {
                richTextBox1.ReadOnly = false;
                button5.ForeColor = Color.Red;
                label4.Text = "Edit";
            }
            else
            {
                richTextBox1.ReadOnly = true;
                button6.Enabled = false;
                button5.ForeColor = Color.Black;
                label4.Text = "Viewer";
            }
        }

        private void button6_Click(object sender, EventArgs e)
        {

        }

        private void richTextBox1_TextChanged(object sender, EventArgs e)
        {

            button6.Enabled = true;
        }

问题是,一旦我正在阅读并将SelectedIndexChanged事件中的文本添加到richTextBox1,它就会转到richTextBox1_TextChanged事件并启用button6为真。

但是我希望它只在我键入/删除了某些内容时才启用button6才能使用文本更改内容而不是文本刚刚添加到richTextBox中。

0 个答案:

没有答案