2013-05-10 40 views
0

我有一個列表框,它將顯示二維碼解碼文本。一旦qr碼被解碼,來自qr碼的文本將被列在列表框中。該程序可以非常快速地對其進行解碼,從而將相同數據的多個文本輸入到列表框中。我想編一個列表框來顯示只有一個解碼文本,並且不會顯示相同的文本,這是多個時間解碼相同的二維碼的結果。下面如何解決列表框中的冗餘數據

enter image description here

是我的列表框的源代碼。我認爲需要在那裏完成額外的編程。會很樂意接受任何建議或教程就此事

/// <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(); 


    } 

再次感謝

回答

0

,你可以檢查列表框中已經包含項目:

void fill_listbox() 
{ 
    string item = txtContent.Text; 
    if(!listBox.Items.Contains(item)) 
    { 
     listBox1.Items.Add(item); 
    } 
    textBox1.Text = listBox1.Items.Count.ToString(); 


} 
+0

先生,真的不明白。不需要寫參數? – 2013-05-10 16:08:06

+0

現在就試試看。感謝先生 – 2013-05-10 17:38:18

+0

先生,謝謝你的解決方案。我的工作很棒 – 2013-05-10 18:04:24

0

如果我要假定listBox1List比你可以將此更改爲Set,防止重複被添加。如果它是一個不同的對象,你就必須調用Contains方法或遍歷對象內部:

bool shouldAdd = true; 
for(Foo foo : listBox1) 
{ 
    if(foo == toAdd) 
    { 
     shouldAdd = false; 
     break; 

    } 
} 
if(shouldAdd) 
{ 
    listBox1.Add(toAdd); 
} 

這是你必須重載equals和hashCode。

+0

嗨,先生,好了,我給它現在出手。感謝您展示道路。 – 2013-05-10 16:06:55