RSS Forums RSS

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

  #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
AddThis Social Bookmark Button
Reply With Quote  
Posts: 2,628
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: 244
Jx_Man's Avatar
Jx_Man Jx_Man is offline Offline
Posting Maven

Re: Removing First Line Of Text From multiline textbox

  #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 12:24 am.
Attached Images
File Type: jpg multiline.JPG (13.1 KB, 9 views)
Never tried = Never Know
So, Please do something before post your thread.
* PM Asking will be ignored *
Reply With Quote  
Posts: 5,068
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 
Solved Threads: 355
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Removing First Line Of Text From multiline textbox

  #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  
Posts: 5,068
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 
Solved Threads: 355
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: Removing First Line Of Text From multiline textbox

  #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 7:12 am.
*Voted best profile in the world*
Reply With Quote  
Posts: 2,628
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: 244
Jx_Man's Avatar
Jx_Man Jx_Man is offline Offline
Posting Maven

Re: Removing First Line Of Text From multiline textbox

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

  #6  
Aug 26th, 2008
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.
Reply With Quote  
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

  #7  
Aug 26th, 2008
Originally Posted by waynespangler View Post
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.
Reply With Quote  
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

  #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  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.



Other Threads in the VB.NET Forum
Views: 3024 | Replies: 7 | Currently Viewing: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 12:42 am.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC