2011-04-28 86 views
1

我有一個ListView和這樣無法刪除ListView項

enter image description here

下面的按鈕是我已經使用的數據的缺失從列表視圖

Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click 
    If lvNotesList.SelectedItems.Count > 0 Then 
     Dim Result = MsgBox("Are sure you want to Delete the Selected Item ?", MessageBoxButtons.YesNo + vbQuestion) 
     If Result = DialogResult.Yes Then 
      Dim ID As String = lvNotesList.SelectedItems(0).SubItems(0).Text 

      Try 
       Dim sqlConnection As New SQLite.SQLiteConnection() 
       Dim sqlCommand As New SQLiteCommand("", sqlConnection) 
       Dim sqlPath As String = "Data Source=" & Application.StartupPath & "\Database\SimpleDB.db3" 
       Dim sqlQuery As String = "DELETE FROM Notes WHERE NoteID = " & ID 
       sqlConnection.ConnectionString = sqlPath 
       sqlConnection.Open() 
       sqlCommand.CommandText = sqlQuery 
       sqlCommand.ExecuteNonQuery() 
       sqlConnection.Close() 
       MsgBox("Operation Successfull", vbInformation) 
      Catch ex As Exception 
       MsgBox("Error: Operation Unsuccessfull." & Chr(13) & "Sumamry:" & ex.Message, vbExclamation) 
      End Try 

     Else 
      Exit Sub 
     End If 
    Else 
     MsgBox("Select an Item First", vbExclamation) 
    End If 
End Sub 

出於某種原因,它的代碼產生這樣的錯誤

enter image description here

我該如何解決這個問題?

回答

2

MsgBox導致ListView失去焦點,從而清除選擇。您必須將ListView的HideSelection屬性設置爲false

編輯:

試試這個

If lvNotesList.SelectedItems.Count > 0 Then 
     Dim ID As String = lvNotesList.SelectedItems(0).SubItems(0).Text 
     Dim Result = MsgBox("Are sure you want to Delete the Selected Item ?", MessageBoxButtons.YesNo + vbQuestion) 
     If Result = DialogResult.Yes Then 
+0

是的,它完美的作品,當我刪除MSGBOX。但是,我怎麼能警告用戶? – 2011-04-28 22:11:32

+0

將HideSelection從設計器設置爲false。 – 2011-04-28 22:12:45

+0

它不能像那樣工作 – 2011-04-28 22:14:13