2016-04-23 49 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

是的,列表框將刪除選定的索引,但是文本框將失去該字符串的任何出現。對不起我的英語不好 –