首页 > 解决方案 > 如何从文本框中删除重复的行结果

问题描述

这是我的数据结果代码

它得到重复的行结果

我想删除重复的结果

请帮我

private void DataResult(string result, string acc, string file)
{
    lock (this)
    {
        if (result == "good")
        {
            MetroTextBox metroTextBox = this.textBox1;
            metroTextBox.Text = metroTextBox.Text + acc + Environment.NewLine;
            file = Path.Combine(this.papka, "good.txt");
            if (!Directory.Exists(this.papka))
            {
                Directory.CreateDirectory(this.papka);
            }
            File.AppendAllText(file, acc + "\r\n");
            Listing.good++;
        }
        if (result == "error")
        {
            Listing.error++;
        }
    }
}

标签: c#

解决方案


Assuming this method is the only way lines can get added to the text box, maybe you should check if the text box contains acc before you add it...

if(!metroTextBox.Text.Contains(acc))
  metroTextBox.Text = metroTextBox.Text + acc + Environment.NewLine;

Side note; if you rename your text box on the form, you won't need to establish variables to it with other names. Click the text box on the form, and in the properties grid where it says (Name) textbox1, change that to metroTextBox

Side note 2; this code appends the contents of the text box to a file every time it adds a line to the text box. This could also be a source of duplication if the file name doesn't change because after adding 3 lines your file will look like:

line1
line1
line2
line1
line2
line3

I don't recommend you write a file as often as you add a line to a text box; one operation is trivial, the other is really slow and involved. Separate these things into different methods and call write file less often


推荐阅读