迅速读懂VB.NET Integer

VB.NET有很多值得学习的地方,这里我们主要介绍VB.NET Integer,包括介绍VB.NET的文本框等方面。VB.NET的文本框没有直接提供取当前行号的功能,但我们可以有如下几种方法实现:

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

先声明如下API函数,注意参数类型是用VB.NET Integer,因为VB.NET Integer是32位的:

 
 
 
  1. 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  
  2. Const EM_LINEFROMCHAR = &HC9  
  3. '计算文本框的当前行号  
  4. Friend Function LineNo(ByVal txthwnd As Integer) As Integer  
  5. '计算文本框的当前行号////////////////////////////徐应成  
  6. '参数txthwnd是文本框的句柄(handle)  
  7. Try  
  8. Return Format$( SendMessageinteger(txthwnd, EM_LINEFROMCHAR, -1&, 0&) + 1, "##,###")  
  9. Catch ex As Exception  
  10. End Try  
  11. 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 

【编辑推荐】

  1. 深入探讨VB.NET重载方法
  2. 简单讨论VB.NET使用缺省属性
  3. VB.NET Sub创建方法简介
  4. 轻松实现Flash控制VB.NET程序
  5. 讲解VB.NET开发语言和C#简单结合
THE END