Hi all I am kind of new to vb.net, and am wondering if anyone could tell me how I can remove the first line of text from a multiline textbox. Let's say I load a text file with 50 lines into a multiline textbox. How do I delete the first line of text from the textbox and have 49 lines left? Any help will be greatly appreciated.

satellites101

Recommended Answers

All 7 Replies

First, you need the following declarations:

Private Const EM_GETLINECOUNT As Integer = &HBA
    Private Const EM_GETLINE As Integer = &HC4
    Private Const EM_LINELENGTH As Integer = &HC1
    Private Const EM_LINEINDEX As Integer = &HBB

    Private Declare Function SendMessageINT Lib "user32.dll" _
        Alias "SendMessageA" (ByVal hWnd As IntPtr, _
        ByVal wMsg As Integer, ByVal wParam As Integer, _
        ByVal lParam As IntPtr) As Integer

Now, I placed this code in a button click event :
Counter set = 1 to start from second line.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim LineCount As Integer = SendMessageINT(TextBox1.Handle, EM_GETLINECOUNT, 0, IntPtr.Zero)
        Dim counter As Integer = 0
        Dim LineIndex As Integer = 0
        Dim lineLength As Integer
        Dim curLine As String = ""
        Dim stringPTR As IntPtr

        For counter = 1 To LineCount - 1
            'Get Index for line we want to retrieve
            LineIndex = SendMessageINT(TextBox1.Handle, EM_LINEINDEX, counter, IntPtr.Zero)
            'GetLineLength
            lineLength = SendMessageINT(TextBox1.Handle, EM_LINELENGTH, LineIndex, IntPtr.Zero)
            'Create the buffer
            curLine = New String("0"c, lineLength + 2)
            Mid(curLine, 1, 1) = Chr(lineLength And &HFF)
            Mid(curLine, 2, 1) = Chr(lineLength \ &H100)
            'Get the pointer for a buffer
            stringPTR = System.Runtime.InteropServices.Marshal.StringToHGlobalAnsi(curLine)
            'Fill the pointer with the current line
            SendMessageINT(TextBox1.Handle, EM_GETLINE, counter, stringPTR)
            'Read the line
            curLine = System.Runtime.InteropServices.Marshal.PtrToStringAnsi(stringPTR)
            curLine = curLine.Substring(0, lineLength)
            'fill textbox2
            TextBox2.Text = TextBox2.Text + curLine + vbCrLf
            'clear out the space
            System.Runtime.InteropServices.Marshal.FreeHGlobal(stringPTR)
            stringPTR = IntPtr.Zero
        Next

    End Sub

See the pic

commented: awesome.... :D +1
commented: So hard....:twisted: +1
commented: :confused: +2
Member Avatar for iamthwee

That looks way too complicated.
I would just skip the first line as you read it in from the text file. Simple enough.

Member Avatar for iamthwee

1) Drag a text box onto your form. Make sure the multiline option is true
2) Drag a button onto your form

Imports System.io

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        'read in file
        Dim objReader As New StreamReader("C:\test.txt")

        Dim sLine As String = ""

        Dim lineCounter As Integer = 0

        Do

            sLine = objReader.ReadLine()
            If lineCounter > 0 Then
                TextBox1.Text = TextBox1.Text + sLine + System.Environment.NewLine

            End If
            lineCounter = lineCounter + 1

        Loop Until sLine Is Nothing

        objReader.Close()

    End Sub
End Class

Great Code iamthwee :)

If you want short and simple, try this:

Dim b As String() = Split(TextBox1.Text, vbNewLine)
        TextBox1.Text = String.Join(vbNewLine, b, 1, b.Length - 2)

If you want short and simple, try this:

Dim b As String() = Split(TextBox1.Text, vbNewLine)
        TextBox1.Text = String.Join(vbNewLine, b, 1, b.Length - 2)

TY for all the replies. The code waynespangler posted hit the nail on the head on what I was looking for. I did have to change:

TextBox1.Text = String.Join(vbNewLine, b, 1, b.Length - 2)

To

TextBox1.Text = String.Join(vbNewLine, b, 1, b.Length - 1)

As it was, it deleted the first and last lines of the textbox. TYVM to all.

Sorry about the typo.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.