Removing First Line Of Text From multiline textbox

Please support our VB.NET advertiser: $4.95 a Month - ASP.NET Web Hosting – Click Here!
Thread Solved
Reply

Join Date: Sep 2006
Posts: 3
Reputation: satellites101 is an unknown quantity at this point 
Solved Threads: 0
satellites101 satellites101 is offline Offline
Newbie Poster

Removing First Line Of Text From multiline textbox

 
0
  #1
Aug 25th, 2008
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
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 2,640
Reputation: Jx_Man is a glorious beacon of light Jx_Man is a glorious beacon of light Jx_Man is a glorious beacon of light Jx_Man is a glorious beacon of light Jx_Man is a glorious beacon of light Jx_Man is a glorious beacon of light 
Solved Threads: 245
Jx_Man's Avatar
Jx_Man Jx_Man is offline Offline
Posting Maven

Re: Removing First Line Of Text From multiline textbox

 
4
  #2
Aug 26th, 2008
First, you need the following declarations:

  1. Private Const EM_GETLINECOUNT As Integer = &HBA
  2. Private Const EM_GETLINE As Integer = &HC4
  3. Private Const EM_LINELENGTH As Integer = &HC1
  4. Private Const EM_LINEINDEX As Integer = &HBB
  5.  
  6. Private Declare Function SendMessageINT Lib "user32.dll" _
  7. Alias "SendMessageA" (ByVal hWnd As IntPtr, _
  8. ByVal wMsg As Integer, ByVal wParam As Integer, _
  9. ByVal lParam As IntPtr) As Integer

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

  1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  2. Dim LineCount As Integer = SendMessageINT(TextBox1.Handle, EM_GETLINECOUNT, 0, IntPtr.Zero)
  3. Dim counter As Integer = 0
  4. Dim LineIndex As Integer = 0
  5. Dim lineLength As Integer
  6. Dim curLine As String = ""
  7. Dim stringPTR As IntPtr
  8.  
  9. For counter = 1 To LineCount - 1
  10. 'Get Index for line we want to retrieve
  11. LineIndex = SendMessageINT(TextBox1.Handle, EM_LINEINDEX, counter, IntPtr.Zero)
  12. 'GetLineLength
  13. lineLength = SendMessageINT(TextBox1.Handle, EM_LINELENGTH, LineIndex, IntPtr.Zero)
  14. 'Create the buffer
  15. curLine = New String("0"c, lineLength + 2)
  16. Mid(curLine, 1, 1) = Chr(lineLength And &HFF)
  17. Mid(curLine, 2, 1) = Chr(lineLength \ &H100)
  18. 'Get the pointer for a buffer
  19. stringPTR = System.Runtime.InteropServices.Marshal.StringToHGlobalAnsi(curLine)
  20. 'Fill the pointer with the current line
  21. SendMessageINT(TextBox1.Handle, EM_GETLINE, counter, stringPTR)
  22. 'Read the line
  23. curLine = System.Runtime.InteropServices.Marshal.PtrToStringAnsi(stringPTR)
  24. curLine = curLine.Substring(0, lineLength)
  25. 'fill textbox2
  26. TextBox2.Text = TextBox2.Text + curLine + vbCrLf
  27. 'clear out the space
  28. System.Runtime.InteropServices.Marshal.FreeHGlobal(stringPTR)
  29. stringPTR = IntPtr.Zero
  30. Next
  31.  
  32. End Sub

See the pic
Last edited by Jx_Man; Aug 26th, 2008 at 1:24 am.
Attached Thumbnails
multiline.JPG  
Never tried = Never Know
So, Please do something before post your thread.
* PM Asking will be ignored *
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 376
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Removing First Line Of Text From multiline textbox

 
0
  #3
Aug 26th, 2008
That looks way too complicated.
I would just skip the first line as you read it in from the text file. Simple enough.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 376
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Removing First Line Of Text From multiline textbox

 
0
  #4
Aug 26th, 2008
1) Drag a text box onto your form. Make sure the multiline option is true
2) Drag a button onto your form

  1. Imports System.io
  2.  
  3. Public Class Form1
  4.  
  5. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  6.  
  7. 'read in file
  8. Dim objReader As New StreamReader("C:\test.txt")
  9.  
  10. Dim sLine As String = ""
  11.  
  12. Dim lineCounter As Integer = 0
  13.  
  14. Do
  15.  
  16. sLine = objReader.ReadLine()
  17. If lineCounter > 0 Then
  18. TextBox1.Text = TextBox1.Text + sLine + System.Environment.NewLine
  19.  
  20. End If
  21. lineCounter = lineCounter + 1
  22.  
  23. Loop Until sLine Is Nothing
  24.  
  25. objReader.Close()
  26.  
  27. End Sub
  28. End Class
Last edited by iamthwee; Aug 26th, 2008 at 8:12 am.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 2,640
Reputation: Jx_Man is a glorious beacon of light Jx_Man is a glorious beacon of light Jx_Man is a glorious beacon of light Jx_Man is a glorious beacon of light Jx_Man is a glorious beacon of light Jx_Man is a glorious beacon of light 
Solved Threads: 245
Jx_Man's Avatar
Jx_Man Jx_Man is offline Offline
Posting Maven

Re: Removing First Line Of Text From multiline textbox

 
0
  #5
Aug 26th, 2008
Great Code iamthwee
Never tried = Never Know
So, Please do something before post your thread.
* PM Asking will be ignored *
Reply With Quote Quick reply to this message  
Join Date: Dec 2002
Posts: 461
Reputation: waynespangler is on a distinguished road 
Solved Threads: 56
waynespangler waynespangler is offline Offline
Posting Pro in Training

Re: Removing First Line Of Text From multiline textbox

 
0
  #6
Aug 26th, 2008
If you want short and simple, try this:
  1. Dim b As String() = Split(TextBox1.Text, vbNewLine)
  2. 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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 3
Reputation: satellites101 is an unknown quantity at this point 
Solved Threads: 0
satellites101 satellites101 is offline Offline
Newbie Poster

Re: Removing First Line Of Text From multiline textbox

 
0
  #7
Aug 26th, 2008
Originally Posted by waynespangler View Post
If you want short and simple, try this:
  1. Dim b As String() = Split(TextBox1.Text, vbNewLine)
  2. 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:
  1. TextBox1.Text = String.Join(vbNewLine, b, 1, b.Length - 2)
To
  1. 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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2002
Posts: 461
Reputation: waynespangler is on a distinguished road 
Solved Threads: 56
waynespangler waynespangler is offline Offline
Posting Pro in Training

Re: Removing First Line Of Text From multiline textbox

 
0
  #8
Aug 27th, 2008
Sorry about the typo.
Wayne

It is hard to understand how a cemetery can raise its burial rates and blame it on the cost of living.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the VB.NET Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC