c# - 如何从文本框中删除重复的行结果
问题描述
这是我的数据结果代码
它得到重复的行结果
我想删除重复的结果
请帮我
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++;
}
}
}
解决方案
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
推荐阅读
- reactjs - material-ui Drawer - findDOMNode 在 StrictMode 中已弃用
- python - Python 在 Linux 上获得空闲时间
- swift - SwiftUI:为什么我不能在一个 HStack 中有多个 TextField?
- python - 从 matlab 获取一组 3D 子图?
- java - ReplaceAll Regex:替换前更新组
- c++ - 我的随机数生成器出现问题,数字超出范围
- excel - 如何将表格和书签插入Word,每页一个
- javascript - 问题如何使用cheerio获取具有沙盒元素的iframe的src
- spyder - 如何将配色方案导入 Spyder 4?
- mongodb - 如何访问每个 docker 容器