Pregunta: | 44392 - NÚMERO DE LÍNEAS DE CAJAS DE TEXTO |
Autor: | Carlos Piña |
Quiero saber si en Visual Basic se puede contar el número de líneas de una caja de texto y si al buscar un caracter se puede obtener el número de la línea donde se encuentra. |
Respuesta: | SuNcO |
Si, se puede con Api's
Este codigo requiere 2 Labels (Label1,Label2), un Timer con Interval de 100 y por supuesto un TextBox (Text1) En el Label 1 se va a poner cuantas lineas tiene el TextBox y en el Label2 se pone en cual linea estas actualmente Private Declare Function SendMessage Lib "user32" _ Alias "SendMessageA" _ (ByVal hwnd As Long, _ ByVal wMsg As Long, _ ByVal wParam As Long, _ lParam As Any) As Long Private Const EM_LINEFROMCHAR = &HC9 Private Const EM_GETLINECOUNT = &HBA Sub Text1_Change() Dim lineCount As Long On Local Error Resume Next 'get/show the number of lines in the edit control lineCount = SendMessage(Text1.hwnd, EM_GETLINECOUNT, 0&, ByVal 0&) Label1.Caption = Format$(lineCount, "##,###") Dim currLine As Long On Local Error Resume Next currLine = SendMessage(Text1.hwnd, EM_LINEFROMCHAR, -1&, ByVal 0&) + 1 Label2.Caption = Format$(currLine, "##,###") End Sub Private Sub Timer1_Timer() Dim lineCount As Long On Local Error Resume Next 'get/show the number of lines in the edit control lineCount = SendMessage(Text1.hwnd, EM_GETLINECOUNT, 0&, ByVal 0&) Label1.Caption = Format$(lineCount, "##,###") Dim currLine As Long On Local Error Resume Next currLine = SendMessage(Text1.hwnd, EM_LINEFROMCHAR, -1&, ByVal 0&) + 1 Label2.Caption = Format$(currLine, "##,###") End Sub Por supuesto, fuera mas sencillo si el Foro permitiera subir Archivos (creo que ya se esta trabajando en ello) |