摘要:[VB6][VB.NET] 依 關鍵字 搜尋 陣列 值 / 字串
Filter相當好用主要是用來處理陣列的值,寫程式必備的技巧之一
資料出處:http://msdn.microsoft.com/zh-tw/library/fat7fw0s(VS.80).aspx
VB6:
Option Explicit
Private Sub Command1_Click()
Dim myArray()
Dim myIndex
Dim v
List1.Clear
myArray = Array("Apple", "Boy", "Can", "Dog", "Taipei")
myIndex = IIf(Option1(0).Value, _
Filter(myArray, UCase(Text1.Text), True, vbTextCompare), _
Filter(myArray, UCase(Text1.Text), False, vbTextCompare))
For Each v In myIndex
List1.AddItem v
Next
End Sub
Private Sub Command1_Click()
Dim myArray()
Dim myIndex
Dim v
List1.Clear
myArray = Array("Apple", "Boy", "Can", "Dog", "Taipei")
myIndex = IIf(Option1(0).Value, _
Filter(myArray, UCase(Text1.Text), True, vbTextCompare), _
Filter(myArray, UCase(Text1.Text), False, vbTextCompare))
For Each v In myIndex
List1.AddItem v
Next
End Sub
執行畫面如下
VB.NET:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button.Click
Dim myArray() As String = {"Apple", "Boy", "Can", "Dog", "Taipei"}
Dim myIndex
Dim v
ListBox1.Items.Clear()
myIndex = IIf(Me.RadioButton1.AutoCheck = True, _
Filter(myArray, UCase(TextBox1.Text), True, vbTextCompare), _
Filter(myArray, UCase(TextBox1.Text), False, vbTextCompare))
For Each v In myIndex
ListBox1.Items.Add(v)
Next
End Sub
End Class
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button.Click
Dim myArray() As String = {"Apple", "Boy", "Can", "Dog", "Taipei"}
Dim myIndex
Dim v
ListBox1.Items.Clear()
myIndex = IIf(Me.RadioButton1.AutoCheck = True, _
Filter(myArray, UCase(TextBox1.Text), True, vbTextCompare), _
Filter(myArray, UCase(TextBox1.Text), False, vbTextCompare))
For Each v In myIndex
ListBox1.Items.Add(v)
Next
End Sub
End Class
執行畫面如下
範例下載
若有謬誤,煩請告知,新手發帖請多包涵
Microsoft MVP Award 2010~2017 C# 第四季
Microsoft MVP Award 2018~2022 .NET