Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 当涉及循环时,如何向列表框添加项?_Vb.net_Loops_Listbox_Match - Fatal编程技术网

Vb.net 当涉及循环时,如何向列表框添加项?

Vb.net 当涉及循环时,如何向列表框添加项?,vb.net,loops,listbox,match,Vb.net,Loops,Listbox,Match,在这个循环中,标题列表框是为每个比赛添加的,但我只希望添加一次。如果在整个循环搜索后没有匹配结果,我还想添加“no match”。此循环中有多个匹配项,因此无法将其放在循环内或“else”下。为了能够知道是否找到匹配项,我通常在循环外添加一个布尔值,称之为“found”。然后我将其设置为false。If语句曾经是我在If中设置为true的匹配项。这样,当循环结束时,我将知道是否存在匹配 Dim intX, intY As Integer intY = Nums.GetUpperBound

在这个循环中,标题列表框是为每个比赛添加的,但我只希望添加一次。如果在整个循环搜索后没有匹配结果,我还想添加“no match”。此循环中有多个匹配项,因此无法将其放在循环内或“else”下。

为了能够知道是否找到匹配项,我通常在循环外添加一个布尔值,称之为“found”。然后我将其设置为false。If语句曾经是我在If中设置为true的匹配项。这样,当循环结束时,我将知道是否存在匹配

Dim intX, intY As Integer
    intY = Nums.GetUpperBound(0)
    For intX = 0 To intY
        With Nums(intX)
            If .strFtID = strID Then

        ‘calls various subs/functions to get results to show in listbox

                listbox.Items.Add(String.Format(strFmt, "various titles”))
                listbox.Items.Add(String.Format(strFmt, variable results))
            End If
        End With
    Next
对于列表框,我将执行以下操作:

Dim found as Boolean = false
For 
 If
  found = true
 End if
Next 

试试这个代码,我想这会符合你的要求

If Not listbox.Items.Contains(String.Format(strFmt, "various titles”)) Then
 listbox.Items.Add(String.Format(strFmt, "various titles”))
End if
现在在循环之后,只需检查列表框的计数。如果其计数大于零,则在上循环中找到了一些匹配项。因此,我们可以省去任何进一步的操作,其他方面只需在列表框中添加“无匹配项”一词,参考下面的代码

Dim intX, intY As Integer
intY = Nums.GetUpperBound(0)
    For intX = 0 To intY
        With Nums(intX)
            If .strFtID = strID Then

               'This following If statement will restricts the duplicate entries, That is
               'multiple matches in your words.

               If Not listbox.Items.Contains(String.Format(strFmt, "various titles”)) Then
                listbox.Items.Add(String.Format(strFmt, "various titles”))
                listbox.Items.Add(String.Format(strFmt, variable results))
               End if

            End If
        End With
    Next
if listbox.Items.count > 0
 listbox.items.add(" NO MATCHES FOUND ")
end if