028-86922220
建站资讯

网站建设资讯

为你提供网站建设行业资讯、网站优化知识、主机域名邮箱、网站开发常见问题等。

两种方法实现VB.NET文本框

学习VB.NET时,你可能会遇到VB.NET文本框问题,这里将介绍VB.NET文本框问题的解决方法,在这里拿出来和大家分享一下。VB.NET文本框没有直接提供取当前行号的功能,但我们可以有如下几种方法实现:

#t#一.用windows API函数,这也是VB的方法

先声明如下API函数,注意参数类型是用Integer,因为VB.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 
'计算文本框的当前行号
Friend Function LineNo(ByVal txthwnd As Integer) As Integer 
'计算文本框的当前行号
'参数txthwnd是文本框的句柄(handle) 
Try 
Return Format$( SendMessageinteger(txthwnd, EM_LINEFROMCHAR, -1&, 0&) + 1, "##,###") 
Catch ex As Exception 
End Try 
End Function

二.累加计算

通过计算累加每行字符总数是否大于插入点前总字符数,来确定当前行数。

 
 
 
  1. '不使用API函数  
  2. Friend Function LineNo(ByVal sender As Object) As Integer  
  3. '计算文本框的当前行号  
  4. Try  
  5. Dim txtbox As TextBox  
  6. Dim charCount As Integer  
  7. Dim i As Integer  
  8. txtbox = CType(sender, TextBox)  
  9. For i = 0 To txtbox.Lines.GetUpperBound(0) '计算行数  
  10. charCount += txtbox.Lines(i).Length + 2 '一个回车符长度2  
  11. If txtbox.SelectionStart < charCount Then  
  12. Return i + 1  
  13. End If  
  14. Next  
  15. Catch ex As Exception  
  16. End Try  
  17. End Function 

新闻名称:两种方法实现VB.NET文本框
文章转载:http://whjierui.cn/article/cocsojo.html

其他资讯