今天突然要用VB写个小程序,遇到要在comboBox中删除重复的项,有段时间没写VB一下子还真忘了怎么个实现方法,计上心头如下的笨方法实现:
================VB版===============
|
While Not objRs.IsEOF If Not IsNull(objRs.GetFieldValue(cmbField.Text)) Then strValue = Trim(objRs.GetFieldValue(cmbField.Text)) If cmbFieldValue.ListCount > 0 Then '//过滤重复项 If IsExists(strValue, cmbFieldValue) = False Then cmbFieldValue.AddItem strValue End If Else cmbFieldValue.AddItem strValue End If End If objRs.MoveNext Wend
'//判断是否存在 Public Function IsExists(strTmp As String, objCom As MiniComboBox) As Boolean Dim i As Integer For i = 0 To objCom.ListCount - 1 If strTmp = objCom.List(i) Then IsExists = True Exit Function End If Next IsExists = False End Function |
==================C#版===================
|
Hashtable ht = new Hashtable();
for (int i = 0; i < this.comboBox1.Items.Count; i++) { if(!ht.ContainsValue(this.comboBox1.Items[i].ToString())) { ht.Add(i,this.comboBox1.Items[i].ToString()); } } this.comboBox1.Items.Clear(); foreach (DictionaryEntry de in ht) { this.comboBox1.Items.Add(de.Value); } |