2017-02-15 81 views
0

我有一个checkedlistbox1,它通过一个带有folderbrowserdialog的搜索功能来填充。一旦我检查了一个项目(= XML文件),它会通过调用一个单独的类来根据特定节点填充列表框。这工作正常。Checkedlistbox:取消选中项目并在新选择时重新执行动作

我希望接下来做的是当我在checkedlistbox1中选择另一个项目时,它取消选中先前检查的项目并再次运行单独的类以显示新选定项目的节点。

我的代码是根据我所做的其他搜索尝试模糊。请注意我希望它做什么,这不像我现在有我的代码,因为我不希望它在我选择另一个项目时抛出错误。我只是想让它取消选择上一个,然后再次对新选择的项目执行操作。

我希望有人能帮我解决这个问题。

代码:

Try 
     Dim checkLstBox As CheckedListBox = CType(sender, CheckedListBox) 
     Dim targetNum As Integer = 1 
     If e.NewValue = CheckState.Checked AndAlso checkLstBox.CheckedItems.Count + 1 > targetNum Then 
      Call ClsMessageBoxes.CheckedListbox1_maxcheck_Form2() 
      e.NewValue = CheckState.Unchecked 
      For i As Integer = 0 To f5.CheckedListBox1.Items.Count - 1 
       f5.CheckedListBox1.SetItemChecked(i, False) 
      Next 'This part at least throws an error if I select a new item in checkedlistbox1 and de-selects the previous item' 

     Else 
      'this part does not work' 
      f5.ListBoxDestPlate.Items.Clear() 
      f5.CheckedListlistbox2.SelectedItems.Clear() 

      'this part is meant to select an item in another checkbox according to certain tekst in the filename' 
      Dim i As Integer 
      If ClsSharedProperties2.filePath2.Contains("Text1") Then 
       i = 1 
       f5.Checkedlistbox2.SetItemChecked(i, True) 
       Call ClsScan.scanning2() 

      ElseIf ClsSharedProperties2.filePath2.Contains("Text2") Then 
       i = 2 
       f5.Checkedlistbox2.SetItemChecked(i, True) 
       Call ClsScan.scanning2() 
      End If 
     End If 
    Catch ex As Exception 
     MessageBox.Show(ex.Message & vbCrLf & "Stack Trace: " & vbCrLf & ex.StackTrace) 
End Try 
+2

'我没有有用的代码'当然可以。发布你最近的努力。它将帮助任何想回答的人理解这个问题。请阅读[问]并参加[tour] – Plutonix

+0

谢谢Plutonix。我现在添加了代码。 – Woudi

回答

0

你的代码是有点乱用我猜测,以其他形式引用(F5 ClsScan?)。

在一般情况下,该代码将与检查项目工作,并且取消所有现有项目:

Private Sub clb_ItemCheck(sender As Object, e As ItemCheckEventArgs) Handles clb.ItemCheck 
    If e.NewValue = CheckState.Checked Then 
    For Each i As Integer In clb.CheckedIndices 
     clb.SetItemChecked(i, False) 
    Next 
    MessageBox.Show("Checked " & clb.Items(e.Index).ToString) 
    End If 
End Sub 

在MessageBox线将与您传递项目参考你需要做的任何函数或方法所取代过滤。

关于ItemCheck事件需要注意的一件事是,集合中的项目实际上并不是已检查。这就是为什么你必须依赖e.Index值。

+0

谢谢拉尔斯。 CheckedIndices是我正在寻找的。它现在很好用!对不起,乱码。我很高兴你能绕过它。 – Woudi

相关问题