Zombie Waffles
Zombie Waffles

Reputation: 195

vb.net Find and REMOVE a line in a textbox

I'm very frustrated trying to get my code to work.

I'm trying to have a selected item in a listbox removed also in the textbox.

Getting ready to remove text; enter image description here

Removed the text; But it's still in the textbox.

enter image description here

Here is my code

    Public Class Form1
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            ListBox1.Items.Add(TextBox1.Text)
            TextBox2.Text += TextBox1.Text & vbNewLine
        End Sub
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            ListBox1.Items.Remove(ListBox1.SelectedItem)
'
'//HOW TO REMOVE THE SELECTED TEXT IN THE LISTBOX ALSO REMOVED IN THE TEXTBOX2??
'
'
        End Sub
        Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
            Dim filenames As String = "C:\log\log.txt"
            My.Computer.FileSystem.WriteAllText(filenames, TextBox2.Text, False)
        End Sub
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim filenames As String = "C:\log\log.txt"
            If My.Computer.FileSystem.FileExists(filenames) Then
                TextBox2.Text = My.Computer.FileSystem.ReadAllText(filenames)
                Dim items()
                items = TextBox2.Lines()
                For Each item In items
                    ListBox1.Items.Add(item)
                Next
            End If
        End Sub
        Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
            Clipboard.SetText(ListBox1.SelectedItem)
        End Sub
    End Class

The worst part is that every time I try to look it up online, there are no errors until I click the button that says 'Value Cannot Be Null' It happened every single time.

Please, before you mash the -1 button, at least tell me why. I'm new to this.

Upvotes: 0

Views: 987

Answers (1)

Stoica Nicusor
Stoica Nicusor

Reputation: 211

This should work for you

Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    TextBox1.Text = TextBox1.Text.Replace(ListBox1.Items(ListBox1.SelectedIndex), Nothing)
    ListBox1.Items.RemoveAt(ListBox1.SelectedIndex)
End Sub

End Class

Upvotes: 2

Related Questions