943,733 Members | Top Members by Rank

Ad:
  • VB.NET Discussion Thread
  • Marked Solved
  • Views: 9471
  • VB.NET RSS
Aug 25th, 2008
0

Removing First Line Of Text From multiline textbox

Expand Post »
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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
satellites101 is offline Offline
3 posts
since Sep 2006
Aug 26th, 2008
4

Re: Removing First Line Of Text From multiline textbox

First, you need the following declarations:

vb.net Syntax (Toggle Plain Text)
  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.

vb.net Syntax (Toggle Plain Text)
  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
Attached Thumbnails
Click image for larger version

Name:	multiline.JPG
Views:	214
Size:	13.1 KB
ID:	7177  
Last edited by Jx_Man; Aug 26th, 2008 at 1:24 am.
Reputation Points: 1182
Solved Threads: 392
Posting Sensei
Jx_Man is offline Offline
3,138 posts
since Nov 2007
Aug 26th, 2008
0

Re: Removing First Line Of Text From multiline textbox

That looks way too complicated.
I would just skip the first line as you read it in from the text file. Simple enough.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Aug 26th, 2008
0

Re: Removing First Line Of Text From multiline textbox

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

vb Syntax (Toggle Plain Text)
  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.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Aug 26th, 2008
0

Re: Removing First Line Of Text From multiline textbox

Great Code iamthwee
Reputation Points: 1182
Solved Threads: 392
Posting Sensei
Jx_Man is offline Offline
3,138 posts
since Nov 2007
Aug 26th, 2008
0

Re: Removing First Line Of Text From multiline textbox

If you want short and simple, try this:
VB.NET Syntax (Toggle Plain Text)
  1. Dim b As String() = Split(TextBox1.Text, vbNewLine)
  2. TextBox1.Text = String.Join(vbNewLine, b, 1, b.Length - 2)
Reputation Points: 84
Solved Threads: 58
Posting Pro in Training
waynespangler is offline Offline
461 posts
since Dec 2002
Aug 26th, 2008
0

Re: Removing First Line Of Text From multiline textbox

If you want short and simple, try this:
VB.NET Syntax (Toggle Plain Text)
  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:
VB.NET Syntax (Toggle Plain Text)
  1. TextBox1.Text = String.Join(vbNewLine, b, 1, b.Length - 2)
To
VB.NET Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
satellites101 is offline Offline
3 posts
since Sep 2006
Aug 27th, 2008
0

Re: Removing First Line Of Text From multiline textbox

Sorry about the typo.
Reputation Points: 84
Solved Threads: 58
Posting Pro in Training
waynespangler is offline Offline
461 posts
since Dec 2002

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in VB.NET Forum Timeline: please i want to make contact details program with VB
Next Thread in VB.NET Forum Timeline: Save picture in access





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC