Removing First Line Of Text From multiline textbox
Please support our VB.NET advertiser: DiscountASP.NET – 3 Months Free on VB.NET Web Hosting
Thread Solved
![]() |
•
•
Posts: 3
Reputation:
Solved Threads: 0
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
satellites101
First, you need the following declarations:
Now, I placed this code in a button click event :
Counter set = 1 to start from second line.
See the pic
vb.net Syntax (Toggle Plain Text)
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.
vb.net Syntax (Toggle Plain Text)
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
Last edited by Jx_Man : Aug 26th, 2008 at 12:24 am.
Never tried = Never Know
So, Please do something before post your thread.
* PM Asking will be ignored *
So, Please do something before post your thread.
* PM Asking will be ignored *
1) Drag a text box onto your form. Make sure the multiline option is true
2) Drag a button onto your form
2) Drag a button onto your form
vb Syntax (Toggle Plain Text)
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
Last edited by iamthwee : Aug 26th, 2008 at 7:12 am.
*Voted best profile in the world*
Great Code iamthwee
Never tried = Never Know
So, Please do something before post your thread.
* PM Asking will be ignored *
So, Please do something before post your thread.
* PM Asking will be ignored *
•
•
Posts: 461
Reputation:
Solved Threads: 56
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) Wayne
It is hard to understand how a cemetery can raise its burial rates and blame it on the cost of living.
It is hard to understand how a cemetery can raise its burial rates and blame it on the cost of living.
•
•
Posts: 3
Reputation:
Solved Threads: 0
•
•
•
•
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)
TextBox1.Text = String.Join(vbNewLine, b, 1, b.Length - 1)
![]() |
Other Threads in the VB.NET Forum
- Previous Thread: please i want to make contact details program with VB
- Next Thread: Save picture in access
•
•
•
•
Views: 3024 | Replies: 7 | Currently Viewing: 1 (0 members and 1 guests)






Linear Mode