3

我正在 VB.NET 中制作一个使用 CheckedListBox 来存储项目的个人应用程序。我的表单上有三个按钮,我想用它们将所选项目的颜色更改为(绿色、橙色和红色。)

我已经尝试了很多方法来解决这个问题,但没有这样的运气。有人可以伸出援助之手吗?

4

4 回答 4

3

请改用 ListView。它支持复选框和选定的项目颜色。

于 2011-05-30T20:42:30.030 回答
1

这里有一个非常相似的答案:

对于 CheckedListBox 中的每个 <item>。<item> 返回为 Object 而不是 Control

基本上,这个控件不会做你想做的事(至少不是没有太多复杂性)。您需要将控件升级到 ListView。

于 2011-05-30T20:44:30.717 回答
1

您还可以使用外观和行为类似于选中列表框的 TreeView:

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

Me.TreeView1.ShowLines = False

Me.TreeView1.CheckBoxes = True

Dim MyColors() As Color = {Color.Black, Color.Blue, Color.Red, Color.Green, Color.Aqua}

For x As Integer = 0 To 4

Dim NewNode As TreeNode = Me.TreeView1.Nodes.Add("Node" & x)

NewNode.ForeColor = MyColors(x)

Next

End Sub
于 2011-05-30T20:46:08.203 回答
1

接受了使用 ListView 的建议。在我的 Windows 7 和 Vista 盒子上看起来和工作得很好,但在 XP 上,ListView 没有正确显示(项目被相互挤压,复选框并不总是显示)。检查以确保它不是框架版本问题,也不是屏幕分辨率问题。最终退回到没有不一致的 CheckedListBox 实现。

找到了这个解决方案,为我完成了 CheckedListBox 场景中蓝色突出显示的移除。但是,使用它您必须以另一种方式跟踪选择(全局变量,查看选中的项目等)。

我只是在处理“..._SelectedIndexChanged”后清除所选项目。我在“..._SelectedIndexChange”中做的第一件事是测试没有选择,如果这是变化,什么也不做。结果是当前选中的项目显示为未选中(实际上是未选中,即没有蓝色突出显示),但 CheckBox 保持选中状态,指示用户最近的选择。

示例 ==>

Private Sub ModelCheckedListBox_SelectedIndexChanged(ByVal sender As System.Object,...
    Dim x As Short = ModelCheckedListBox.SelectedIndex
    If x >= 0 Then

  'Something I always do since the Selection Mode = "One" doesn't bother to clear
        'the checks itself

        ModelCheckedListBox.SetItemChecked(x, True)
        If ModelCheckedListBox.CheckedItems.Count > 1 Then
            For Each item In ModelCheckedListBox.CheckedIndices
                If item <> x Then
                    ModelCheckedListBox.SetItemChecked(item, False)
                End If
            Next
        End If
        ModelCheckedListBox.Refresh()

  'More of your code

        ModelCheckedListBox.ClearSelected()
    End If
End Sub
于 2011-07-14T17:53:30.150 回答