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

开发 后端
这里介绍VB.NET文本框,VB.NET文本框没有直接提供取当前行号的功能,但我们可以有如下两种种方法实现:用windows API函数,这也是VB的方法和累加计算。

学习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 
责任编辑:佚名 来源: IT168
相关推荐

2010-01-21 15:56:31

VB.NET文本框

2010-01-18 17:37:32

VB.NET文本框处理

2010-01-14 11:09:35

VB.NET文本框

2009-10-26 10:30:57

VB.NET处理FTP

2010-01-19 18:06:14

VB.NET发送邮件

2009-10-30 16:40:04

VB.NET Inte

2010-01-15 16:29:47

VB.NET对象存储

2010-01-13 17:47:59

VB.NET拖放

2010-01-15 16:12:40

VB.NET调用DLL

2010-01-21 17:48:25

VB.NET Sing

2010-01-15 18:12:28

VB.NET超链接

2010-01-15 19:17:23

2010-01-15 10:56:50

VB.NET继承实现多

2009-11-03 09:26:13

VB.NET方法

2010-01-13 14:41:18

VB.NET列出目录内

2010-01-07 16:51:56

VB.NET窗体钩子

2011-05-20 16:49:21

VB.NET

2012-06-06 15:15:00

jQuery

2009-10-20 14:05:42

VB.NET路径

2010-01-08 16:10:05

VB.NET读写文本文
点赞
收藏

51CTO技术栈公众号