两种方法实现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://bzwzjz.com/article/cocsojo.html

其他资讯

Copyright © 2007-2020 广东宝晨空调科技有限公司 All Rights Reserved 粤ICP备2022107769号
友情链接: 成都品牌网站建设 温江网站设计 手机网站建设 企业网站建设公司 成都网站制作 成都网站建设公司 成都网站建设 企业网站建设 重庆网站建设 网站建设公司 成都营销网站建设 网站制作 重庆网站制作 网站设计公司 成都网站制作 成都网站制作 外贸网站设计方案 成都网站设计 LED网站设计方案 四川成都网站设计 攀枝花网站设计 成都模版网站建设