C# 如何解决listbox中的冗余数据
我有一个列表框,将显示二维码解码文本。一旦二维码被解码,二维码中的文本将在列表框中列出。该程序解码速度非常快,这导致列表框中有多个具有相同数据的文本条目。我想对列表框进行编程,使其仅显示1个解码文本,而不会显示同一个qr码多次解码后产生的相同文本 下面是我的列表框源代码。我认为需要在那里进行额外的编程。很高兴收到任何关于此事的建议或指导C# 如何解决listbox中的冗余数据,c#,listbox,qr-code,C#,Listbox,Qr Code,我有一个列表框,将显示二维码解码文本。一旦二维码被解码,二维码中的文本将在列表框中列出。该程序解码速度非常快,这导致列表框中有多个具有相同数据的文本条目。我想对列表框进行编程,使其仅显示1个解码文本,而不会显示同一个qr码多次解码后产生的相同文本 下面是我的列表框源代码。我认为需要在那里进行额外的编程。很高兴收到任何关于此事的建议或指导 /// <summary> /// To show the result of decoding. the result is f
/// <summary>
/// To show the result of decoding. the result is feed to Barcode Format, QR Content and Scanned item.
/// </summary>
/// <param name="result"></param>
private void ShowResult(Result result)
{
currentResult = result;
txtBarcodeFormat.Text = result.BarcodeFormat.ToString();
txtContent.Text = result.Text;
fill_listbox();
}
/// <summary>
/// Item scanned will be listed in listbox
/// </summary>
void fill_listbox()
{
string item = txtContent.Text;
listBox1.Items.Add(item);
textBox1.Text = listBox1.Items.Count.ToString();
}
//
///显示解码结果。结果被馈送到条形码格式、QR内容和扫描项目。
///
///
私有void ShowResult(结果)
{
currentResult=结果;
txtBarcodeFormat.Text=result.BarcodeFormat.ToString();
txtContent.Text=结果.Text;
填充列表框();
}
///
///扫描的项目将列在列表框中
///
空白填充列表框()
{
string item=txtContent.Text;
列表框1.Items.Add(item);
textBox1.Text=listBox1.Items.Count.ToString();
}
再次感谢如果我假定
列表框1
是一个列表
,那么您可以将其更改为集合
,并防止添加重复项。如果它是不同的对象,则必须调用Contains
方法或迭代对象内部:
bool shouldAdd = true;
for(Foo foo : listBox1)
{
if(foo == toAdd)
{
shouldAdd = false;
break;
}
}
if(shouldAdd)
{
listBox1.Add(toAdd);
}
必须重写equals和hashcode 您可以检查列表框是否已包含该项:
void fill_listbox()
{
string item = txtContent.Text;
if(!listBox.Items.Contains(item))
{
listBox1.Items.Add(item);
}
textBox1.Text = listBox1.Items.Count.ToString();
}
嗨,先生,好的,我现在给它打一针。先生,我真的不明白。不需要写论点?