2017-08-08 34 views
-1

請幫我這個!
我有一個ListView啓用複選框。我需要禁用所有選中的項目複選框,用戶不應該再次點擊它。 這是我的代碼,我收到錯誤。Listview禁用某些複選框一旦檢查

Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click 
    Try 
     ' submit 
     Dim path As String = "C:\Users\jtb43661\Documents\Visual Studio 2017\Projects\IGI Event Tracker\IGI Event Tracker\bin\Debug\Logs\Event.LOG" 
     If Not File.Exists(path) Then 
      Using sw As StreamWriter = File.CreateText(path) 
      End Using 
     End If 
     Using sw As StreamWriter = File.AppendText(path) 
      For Each item In ListView1.CheckedItems 
       sw.WriteLine(item.Text & "->" & " [email protected]> " & Label2.Text) 
       item.SubItems.Add("Completed") 
       item.BackColor = Color.GreenYellow 
       'If item.subItems.text = "Completed" Then 
       'here I need to disable or lock the checked checkboxes 

       'End If 
      Next 
      sw.Close() 
     End Using 
     MsgBox("Events Submitted Successfully") 
    Catch ex As Exception 
     MsgBox(ex.Message.ToString) 
    Finally 
    End Try 
End Sub 

回答

2

如果我正確理解你的暗示邏輯,一旦ListviewItem檢查幷包含Text屬性等於ListViewSubItem爲「已完成」你不希望用戶能夠取消該項目。添加「已完成」子項中一個按鈕單擊事件處理程序是這樣進行的:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 
    For Each itm As ListViewItem In ListView1.CheckedItems 
     ' add "Completed" subitem only if it does not currently exist 
     Dim hasCompleted As Boolean = False 
     For Each subitem As ListViewItem.ListViewSubItem In itm.SubItems 
      hasCompleted = subitem.Text.Equals("Completed") 
      If hasCompleted Then Exit For 
     Next 
     If Not hasCompleted Then itm.SubItems.Add("Completed") 
    Next 
End Sub 

據我所知,目前還沒有辦法直接禁用ListViewItem,以防止它被選中。但是,ListView的確有ItemCheck事件,可用於防止更改「檢查」狀態。如果「UnChecked」項目具有帶「完成」文本的子項目,以下代碼可防止檢查狀態更改。

Private Sub ListView1_ItemCheck(sender As Object, e As ItemCheckEventArgs) Handles ListView1.ItemCheck 
    If e.CurrentValue = CheckState.Checked Then 
     Dim item As ListViewItem = ListView1.Items(e.Index) 
     For Each subitem As ListViewItem.ListViewSubItem In item.SubItems 
      If subitem.Text.Equals("Completed") Then 
       e.NewValue = e.CurrentValue ' do not allow the change 
       Exit For 
      End If 
     Next 
    End If 
End Sub 
+0

謝謝:)你讓我的一天 – tharun

+0

@tharun如果是這樣,請考慮upvoting答案。您已成爲會員3年,但從未投過Q或A的投票。您認爲有幫助或信息豐富的帖子有助於他人找到好帖子。如果您無法發佈答案,這是一種幫助他人的方式。簡要介紹更多。 – Plutonix

+0

Sure Plutonix :) – tharun