2016-04-23 68 views
0

我非常沮丧地试图让我的代码工作。vb.net查找并删除文本框中的一行

我想在列表框中删除一个选定的项目也在文本框中。

准备好删除文本; enter image description here

删除了文字; 但它仍然在文本框中。

enter image description here

这里是我的代码

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 

最糟糕的是,每次我试图在网上查一查,有没有错误,直到我点击,上面写着按钮“值不能空' 它每一次都发生。

请在你捣烂-1按钮之前,至少告诉我为什么。我对此很陌生。

回答

2

这应该工作,你

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 

末级

+0

要知道,这只会如果您在列表框中没有任何两次相同的项目工作...但如果这对你合适,这将起作用 –

+0

是的,列表框将删除选定的索引,但是文本框将失去该字符串的任何出现。对不起我的英语不好 –