Results 1 to 6 of 6

Thread: [RESOLVED] Goto Line In TextBox

  1. #1

    Thread Starter
    Fanatic Member BenJones's Avatar
    Join Date
    Mar 2010
    Location
    Wales UK
    Posts
    814

    Resolved [RESOLVED] Goto Line In TextBox

    hi I am trying to use the API sendmessage to goto a line in my textbox but I get this error.

    Code:
    A call to PInvoke function 'MyBasic!MyBasic.frmmain::SendMessage' has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature.
    Code:
        Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long,
        ByVal wParam As Long, ByVal LParam As Integer) As Long
    
        Private Sub GotoLine(ByVal tb As TextBox, ByVal Line As Integer)
            Dim Ret As Integer = SendMessage(tb.Handle, EM_LINEINDEX, Line - 1, 0)
            tb.SelectionStart = Ret
        End Sub
    GotoLine(txtCode, 5)

  2. #2
    Frenzied Member
    Join Date
    Nov 2005
    Posts
    1,834

    Re: Goto Line In TextBox

    Your declaration of SendMessage is wrong.

    http://www.pinvoke.net/default.aspx/user32.sendmessage

  3. #3
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Goto Line In TextBox

    Without the API

    Code:
        Private Sub GotoLine(ByVal tb As TextBox, ByVal Line As Integer)
            Dim idx As Integer = 0
            For x As Integer = 0 To Line - 1
                For Each c As Char In tb.Lines(x)
                    idx += 1
                Next
                idx += Environment.NewLine.Length
            Next
            tb.SelectionStart = idx
            tb.SelectionLength = 0
            tb.Select()
        End Sub
    assumes that Line is based on a zero index, the first line is at index 0.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  4. #4

    Thread Starter
    Fanatic Member BenJones's Avatar
    Join Date
    Mar 2010
    Location
    Wales UK
    Posts
    814

    Re: Goto Line In TextBox

    Thanks to both working now.

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,415

    Re: [RESOLVED] Goto Line In TextBox

    there's a simpler method than both of the previously mentioned methods:

    Code:
    Public Class Form1
    
        Private Sub NumericUpDown1_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NumericUpDown1.ValueChanged
            TextBox1.SelectionStart = TextBox1.GetFirstCharIndexFromLine(CInt(NumericUpDown1.Value))
            TextBox1.Focus()
        End Sub
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            NumericUpDown1.Minimum = 0
            NumericUpDown1.Maximum = 9
            'TextBox1 contains 10 lines of text
            NumericUpDown1.Value = 0
        End Sub
    
    End Class

  6. #6
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: [RESOLVED] Goto Line In TextBox

    Quote Originally Posted by .paul. View Post
    there's a simpler method than both of the previously mentioned methods:

    Code:
    Public Class Form1
    
        Private Sub NumericUpDown1_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NumericUpDown1.ValueChanged
            TextBox1.SelectionStart = TextBox1.GetFirstCharIndexFromLine(CInt(NumericUpDown1.Value))
            TextBox1.Focus()
        End Sub
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            NumericUpDown1.Minimum = 0
            NumericUpDown1.Maximum = 9
            'TextBox1 contains 10 lines of text
            NumericUpDown1.Value = 0
        End Sub
    
    End Class
    Applauding GetFirstCharIndexFromLine!
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

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