迅速讀懂VB.NET Integer
VB.NET有很多值得學(xué)習(xí)的地方,這里我們主要介紹VB.NET Integer,包括介紹VB.NET的文本框等方面。VB.NET的文本框沒(méi)有直接提供取當(dāng)前行號(hào)的功能,但我們可以有如下幾種方法實(shí)現(xiàn):
一.用windows API函數(shù),這也是VB的方法
先聲明如下API函數(shù),注意參數(shù)類型是用VB.NET Integer,因?yàn)閂B.NET Integer是32位的:
- Private Declare Function SendMessageinteger Lib "user32" Alias "SendMessageA"
(ByVal hwnd As Integer, ByVal wMsg As Integer,
ByVal wParam As Integer, ByVal lParam As Integer) As Integer- Const EM_LINEFROMCHAR = &HC9
- '計(jì)算文本框的當(dāng)前行號(hào)
- Friend Function LineNo(ByVal txthwnd As Integer) As Integer
- '計(jì)算文本框的當(dāng)前行號(hào)////////////////////////////徐應(yīng)成
- '參數(shù)txthwnd是文本框的句柄(handle)
- Try
- Return Format$( SendMessageinteger(txthwnd, EM_LINEFROMCHAR, -1&, 0&) + 1, "##,###")
- Catch ex As Exception
- End Try
- End Function
二.累加計(jì)算
通過(guò)計(jì)算累加每行字符總數(shù)是否大于插入點(diǎn)前總字符數(shù),來(lái)確定當(dāng)前行數(shù)。
- '不使用API函數(shù)
- Friend Function LineNo(ByVal sender As Object) As Integer
- '計(jì)算文本框的當(dāng)前行號(hào)////////////////////////////徐應(yīng)成
- Try
- Dim txtbox As TextBox
- Dim charCount As Integer
- Dim i As Integer
- txtbox = CType(sender, TextBox)
- For i = 0 To txtbox.Lines.GetUpperBound(0) '計(jì)算行數(shù)
- charCount += txtbox.Lines(i).Length + 2 '一個(gè)回車(chē)符長(zhǎng)度2
- If txtbox.SelectionStart < charCount Then
- Return i + 1
- End If
- Next
- Catch ex As Exception
- End Try
- End Function
【編輯推薦】