新聞中心
學(xué)習(xí)VB.NET時(shí),你可能會(huì)遇到VB.NET文本框問(wèn)題,這里將介紹VB.NET文本框問(wèn)題的解決方法,在這里拿出來(lái)和大家分享一下。VB.NET文本框沒(méi)有直接提供取當(dāng)前行號(hào)的功能,但我們可以有如下幾種方法實(shí)現(xiàn):

#t#一.用windows API函數(shù),這也是VB的方法
先聲明如下API函數(shù),注意參數(shù)類(lèi)型是用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)
'參數(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)
- 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
文章名稱(chēng):兩種方法實(shí)現(xiàn)VB.NET文本框
文章URL:http://fisionsoft.com.cn/article/cocsojo.html


咨詢(xún)
建站咨詢(xún)
