窗體上添加一個(gè)文本框,一個(gè)列表框,一個(gè)按鈕:
石城網(wǎng)站建設(shè)公司創(chuàng)新互聯(lián)建站,石城網(wǎng)站設(shè)計(jì)制作,有大型網(wǎng)站制作公司豐富經(jīng)驗(yàn)。已為石城千余家提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\外貿(mào)網(wǎng)站建設(shè)要多少錢,請(qǐng)找那個(gè)售后服務(wù)好的石城做網(wǎng)站的公司定做!
代碼如下:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim MyDir As String = "C:\123"
Dim MyFilter As String = "*" TextBox1.Text "*"
ListBox1.Items.Clear()
For Each MyFile As String In System.IO.Directory.GetFiles(MyDir, MyFilter)
ListBox1.Items.Add(MyFile)
Next
End Sub
是關(guān)鍵字。比如:SUB代表一個(gè)函數(shù)的開始,END SUB代表函數(shù)結(jié)束,函數(shù)主體語句就必須寫在這兩個(gè)關(guān)鍵字之間。CLASS類關(guān)鍵字,PRIVATE私有關(guān)鍵字,PUBLIC公用關(guān)鍵字,IF.....END IF是判斷關(guān)鍵字,等等。建議找本基礎(chǔ)書看一看。
Private Sub RichTextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RichTextBox1.TextChanged
Dim pos = RichTextBox1.SelectionStart
Dim i As String = Regex.Matches(RichTextBox1.Text, "\bDim\b").ToString
RichTextBox1.SelectAll
RichTextBox1.SelectionColor = Color.Black
If Regex.IsMatch(RichTextBox1.Text, "\bDim\b") = True Then
For Each mat As Match In Regex.Matches(RichTextBox1.Text, "\bDim\b")
RichTextBox1.SelectionStart = mat.Index
RichTextBox1.SelectionLength = mat.Length
RichTextBox1.SelectionColor = Color.YellowGreen
Next
End If
RichTextBox1.SelectionStart = pos
RichTextBox1.SelectionLength = 0
End Sub
我做了一個(gè)更詳細(xì)的正則:
Friend Keys As String =
"\b(#Const|#If|Then|#Else|#Region|Delegate|Namespace|Class|End|Firend|Partial|Module|Interface|Enum|Shared|Overrides|Overloads|Structure|Let|Const|Dim|As|Private|Public|New|Static|Option|Private|Module|IsArray|IsDate|IsEmpty|IsError|IsMissing|IsNull|IsNumeric|IsObject|TypeName|VarType|Me|Option|Explicit|Mod|Like|Is|Not|And|Or|Xor|Eqv|Imp|Clear|Error|Raise|Error|Err|CVErr|On|Error|Resume|IsError|Collection|Add|Remove|Item|DDB|SLN|SYD|FV|Rate|IRR|MIRR|NPer|IPmt|Pmt|PPmt|NPV|PV|Do|Loop|For|Next|For|Each|Next|While|Wend|With|Choose|If|Then|Else|Select|Case|Switch|Call|Function|Property|Get|Property|Let|Property|Set|Sub|Date|Now|Time|DateAdd|DateDiff|DatePart|DateSerial|DateValue|TimeSerial|TimeValue|Date|Time|Timer|CBool|CByte|CCur|CDate|CDbl|CDec|CInt|CLng|CSng|CStr|CVar|CVErr|Fix|Int|Boolean|Byte|Currency|Date|Double|Integer|Long|Object|Single|String|Object|Atn|Cos|Sin|Tan|Exp|Log|Sqr|Randomize|Rnd|Abs|Sgn|Fix|Int|IsArray|Array|Option|Base|Dim|Private|Public|ReDim|Static|LBound|UBound|Erase|ReDim|DeleteSetting|GetSetting|GetAllSettings|SaveSetting|Chr|Format|LCase|UCase|DateSerial|DateValue|Hex|Oct|Format|Str|CBool|CByte|CCur|CDate|CDbl|CDec|CInt|CLng|CSng|CStr|CVar|CVErr|Fix|Int|Day|Month|Weekday|Year|Hour|Minute|Second|Asc|Val|TimeSerial|TimeValue|StrComp|StrConv|Format|LCase|UCase|Space|String|Len|Format|LSet|RSet|InStr|Left|LTrim|Mid|Right|RTrim|Trim|Option|Compare|Asc|Chr|AppActivate|Shell|SendKeys|Beep|Command)\b"
顧名思義,new就是給變量賦值一個(gè)新的對(duì)象
dim不加new就是說變量是XX類型的,但它不是一個(gè)真正的XX對(duì)象。如果給這個(gè)變量賦值的話,實(shí)際上就是把某一對(duì)象的指針存到變量里,它依舊不是一個(gè)對(duì)象
一個(gè)普通的類,里面有一些數(shù)據(jù),你要弄一個(gè)對(duì)象的話,需要初始化所有類中的數(shù)據(jù)并調(diào)用構(gòu)造函數(shù)。這時(shí)候我們就用new了。如果你不用new,然后直接使用的話,會(huì)引發(fā)null異常
當(dāng)然上面的討論不包括Integer、String、Long這樣的數(shù)值類型,這些不能用new