This is my first post so please bear with me. I am trying to return the 'correct' line number from a textbox. I have seen several examples in forums but the nearly all do not work quite right. i.e when the user scrolls up and down the lines with the cursor keys there is inconsistent line number values.
The only one I have seen that works superbly is from the microsoft support website - Microsoft Knowledge Base Article - 186271 (HOWTO: Manipulate Text Box Contents). It works fine, BUT (there is always a but isn't there?) when the program exits, most of the time you get an Application Error...arrrgghhh!!!
Please, please does someone know how to code this line number problem (properly) or know where the knowledge base article error lies. I am using VB6, under Windows 2000.
Declare Function SendMessage Lib "user32" Alias "SendMessageA" (Byval hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Const EM_LINEFROMCHAR = &HC9
Dim LineNumber As Long
LineNumber = SendMessage(Me.Text1.hwnd, EM_LINEFROMCHAR, Me.Text1.SelStart, 0&)
SelStart is the current cursor position in your textbox (whether you have selected text or not).
Hope this helps.
Cheers,
Tinbum747
Zen-Programming:
If a compiler beeps in the IDE forest, and nobody hears it, was there really a bug?
Yes most of the available examples comprise that code, but if you place it in the keydown event of a text box and print the line number to a label (and type in some text) then the line numbers do not display properly.
Particularly, if you press the up cursor key then down cursor key, the line number actually 'decreases' by one (when you press down) before continuing to increase!!! Arrgghhhhh. Anyone else have some tips on why it is doing this???
You better implement that code in the KeyUp event.. If it still persist well you have to dig down more by subclassing the Textbox control and look for the most appropriate window message for the event..
Option Explicit
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
Const EM_LINEFROMCHAR = &HC9
Private Sub Text1_KeyUp(KeyCode As Integer, Shift As Integer)
Me.Caption = SendMessage(Me.Text1.hwnd, EM_LINEFROMCHAR, Me.Text1.SelStart, 0&) + 1
End Sub
Private Sub Text1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
Text1_KeyUp 0, 0
End Sub
A simple solution would be to use a RichTextBox control (from microsoft) instead of a textbox. And then call the GetLineFromChar funciton of the control to get the current line.
I can open a console window, does anyone know how to disable the close window button. As if you close the console window the VB program always close too, so if I can disable the button users won't be able to do this by mistake.
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.