CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Jul 2003
    Posts
    5

    Angry Return line number from text box

    Hello to everyone,

    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!!!

    The article is at:

    http://support.microsoft.com/default...NoWebContent=1

    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.

    Thanks,

    David.
    Attached Images Attached Images

  2. #2
    Join Date
    Jun 2003
    Location
    Planet Earth
    Posts
    186
    David,

    If you already know that the textbox lines are being separated by vbLf or vbCrlf, etc. then here's one solution:

    Dim aStringArray () As String

    sString = txtViewTextBox.Text
    aStringArray = Split(Text1.Text, vbCrLf)

    MsgBox UBound(aStringArray)

  3. #3
    Join Date
    Jun 2003
    Location
    Planet Earth
    Posts
    186
    Oops, disregard this code:

    sString = txtViewTextBox.Text

    I forgot to take it out before posting.

  4. #4
    Join Date
    Jul 2003
    Posts
    5
    Hi,

    Thanks for that but I need something a little more rigorous. Anyone else help?

  5. #5
    Join Date
    May 2003
    Location
    Australia
    Posts
    155
    Hi,

    Try the following ...

    Code:
    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?

  6. #6
    Join Date
    Jul 2003
    Posts
    5

    Still doesn't work

    Hi,

    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???

    Cheers in advance....

    Frustrated Dobby

  7. #7
    Join Date
    Jan 2003
    Location
    7,107 Islands
    Posts
    2,487
    Hi Dobby,

    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..
    Busy

  8. #8
    Join Date
    Jan 2003
    Location
    7,107 Islands
    Posts
    2,487
    You may try the following code..
    Code:
    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
    Busy

  9. #9
    Join Date
    Jan 2002
    Location
    Quebec/Canada
    Posts
    124
    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.

  10. #10
    Join Date
    Jul 2003
    Posts
    5

    disable console window close button

    Hi Guys,

    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.

    Cheers.

    dobby

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured