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

ですNull ' それは毎回起こった。

-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

はいこれは本当です、ListBoxは、選択されたインデックスを削除しますが、TextBoxはその文字列のocurenceを失います。私の英語のために申し訳ありません –

関連する問題