Read and delete lines - Printable Version +- BP Forums (https://bpforums.info) +-- Forum: Archived Forums (https://bpforums.info/forumdisplay.php?fid=55) +--- Forum: Archived Forums (https://bpforums.info/forumdisplay.php?fid=56) +---- Forum: VB.NET (Visual Basic 2010/2008) (https://bpforums.info/forumdisplay.php?fid=8) +----- Forum: Programming Help (https://bpforums.info/forumdisplay.php?fid=9) +----- Thread: Read and delete lines (/showthread.php?tid=645) |
Read and delete lines - brco900033 - 09-14-2012 Hello guys I have a question; how can I read and delete a specific line from a textbox (multiline already set to true ofcourse). I searched a lot of forums but those codes always contains little errors. Have a nice day! Re: Read and delete lines - brco900033 - 09-14-2012 Sorry, forgot to enter some extra info: - I want to delete a line that contains a part of a text Re: Read and delete lines - brandonio21 - 09-14-2012 Are you referring to a sentence? Or an entire line? Re: Read and delete lines - brco900033 - 09-15-2012 A line. For example: -the textbox contains: I live in belgium my name is brco I'm 15 years old ... -I search for 'My name' -the second line contains 'my name' so that whole line will be deleted. Re: Read and delete lines - brandonio21 - 09-15-2012 Ah, alright! Well this solution is rather fancy, if I don't say so myself. So, I have created this method that completely removes a line from the textbox which contains a specified value. [code2=vbnet]Public Sub RemoveLine(ByVal containing As String) If (TextBox1.Text.Contains(containing)) Then 'Now, we need to find which line index contains the thing Dim lineIndex As Integer = TextBox1.GetLineFromCharIndex(TextBox1.Text.IndexOf(containing)) 'Now, let's delete that line Dim lines As New List(Of String) lines.AddRange(TextBox1.Text.Split(vbNewLine)) lines.RemoveAt(lineIndex) 'Now, reset the text TextBox1.Text = String.Join(vbNewLine, lines.ToArray, 0, lines.Count) End If End Sub[/code2] So, if you want to delete a line containing whatever TextBox2.Text has in it, for example, add a button to your form (Button1), then, just call this code from your button: [code2=vbnet]RemoveLine(TextBox2.Text)[/code2] Hopefully this works for you! Re: Read and delete lines - brco900033 - 09-16-2012 Ah thanks! Needed this a lot <!-- s:mrgreen: --><img src="{SMILIES_PATH}/icon_mrgreen.gif" alt=":mrgreen:" title="Mr. Green" /><!-- s:mrgreen: --> Re: Read and delete lines - brandonio21 - 09-16-2012 brco900033 Wrote:Ah thanks! Needed this a lot <!-- s:mrgreen: --><img src="{SMILIES_PATH}/icon_mrgreen.gif" alt=":mrgreen:" title="Mr. Green" /><!-- s:mrgreen: -->No problemo! |