Friday, May 30, 2014

How to delete a line from a textbox?

I believe this works correctly but it took awhile to figure out since a TextBox line from my text file that is a blank line has a count of zero for some reason. Even though there are two items in it representing a vbCrLf or Environment.NewLine perhaps.


I don't know if all text files will have the same amount of characters representing a new line though.


I also tested this with typed in text at the top of the TextBoxs text and some enters for new lines and it worked.



Option Strict On

Public Class Form1

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.CenterToScreen()
TextBox1.Text = My.Computer.FileSystem.ReadAllText("C:\Users\John\Desktop\Samsung disallow calls.Txt")
NumericUpDown1.Minimum = 0
If TextBox1.Text <> "" Then
NumericUpDown1.Maximum = TextBox1.Lines.Count - 1
Else
NumericUpDown1.Maximum = 0
End If
End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim StartIndex As Integer = 0
Me.Text = TextBox1.Lines(1).Count.ToString
For i = 0 To TextBox1.Lines.Count - 1
If i < CInt(NumericUpDown1.Value) Then
If TextBox1.Lines(i).Count > 0 Then
StartIndex += TextBox1.Lines(i).Count + 2
ElseIf TextBox1.Lines(i).Count = 0 Then
StartIndex += 2
End If
End If
Next
TextBox1.Text = TextBox1.Text.Remove(StartIndex, TextBox1.Lines(CInt(NumericUpDown1.Value)).Count + 2) ' Add 2 to remove the new line character.
End Sub

Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
If TextBox1.Text <> "" Then
NumericUpDown1.Minimum = 0
NumericUpDown1.Maximum = TextBox1.Lines.Count - 1
Label1.Text = "Line nr " & NumericUpDown1.Value.ToString & " is - " & ChrW(34) & TextBox1.Lines(CInt(NumericUpDown1.Value)) & ChrW(34)
End If
End Sub

Private Sub NumericUpDown1_ValueChanged(sender As Object, e As EventArgs) Handles NumericUpDown1.ValueChanged
Label1.Text = "Line nr " & NumericUpDown1.Value.ToString & " is - " & ChrW(34) & TextBox1.Lines(CInt(NumericUpDown1.Value)) & ChrW(34)
End Sub

End Class






La vida loca


No comments:

Post a Comment