使用带有checkedlistbox的contextmenustrip - 不是列表中的项目,而是checkedlistbox本身 [英] Use a contextmenustrip with a checkedlistbox - not the items in the list but the checkedlistbox itself

查看:90
本文介绍了使用带有checkedlistbox的contextmenustrip - 不是列表中的项目,而是checkedlistbox本身的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法让我的contextmenustrip给我我想要的结果。



我希望能够根据需要禁用/启用我的checkedlistboxes。我遇到的问题是我不能让我的contextmenustrip与列表框本身一起工作,因为它成为控件的焦点我必须点击一个项目然后突出显示它并检查框(我肯定不想要这个)



另一个问题是,当列表框被禁用时,它不允许我使用contextmenustrip重新启用它。



另外,如果有人可以告诉我如何阻止物品突出显示在我的checkedlistbox中,我会非常感激 - 即使你取消选中一个项目它仍然会突出显示非常烦人。



我的尝试:



 私有  Sub  MarkCrewInactiveToolStripMenuItem_Click(发件人 As  对象,e 作为 EventArgs)句柄 MarkCrewInactiveToolStripMenuItem.Click 
如果 TypeOf .ActiveControl CheckedListBox 然后
Dim objControl As CheckedListBox = Me .ActiveControl
ActiveControl.BackColor = SystemColors.ActiveBorder
ActiveControl.Enabled = 错误
结束 如果
结束 Sub

< span class =code-keyword>私有 Sub MarkCrewActiveToolStripMenuItem_Click(发件人作为 对象,e As EventArgs)句柄 MarkCrewActiveToolStripMenuItem.Click
If TypeOf .ActiveControl CheckedListBox < span class =code-keyword>然后
Dim objControl As CheckedListBox = .ActiveControl
ActiveControl.BackColor = SystemColors.Window
ActiveControl.Enabled = True
结束 如果

结束 Sub

解决方案

请,首先阅读我对这个问题的评论。我在那里提到过良好的编程习惯。因此,您的代码可以更改为:

 私有  Sub  MarkCrewActiveToolStripMenuItem_Click(发件人作为 对象,e  As  EventArgs)句柄 MarkCrewActiveToolStripMenuItem.Click,MarkCrewInactiveToolStripMenuItem.Click 

Dim tsmi As ToolStripMenuItem = DirectCast (sender,ToolStripMenuItem)
如果 tsmi IsNot Nothing 然后
Dim myarg 作为 整数 = 如果(tsmi.Name = MarkCrewInactiveToolStripMenuItem, - 1, 0
UnCheckCheckedListBox(myarg)
结束 如果

结束 Sub


私有 Sub UnCheckCheckedListBox(可选 ByVal action As 整数 = 0

< span class =code-keyword>如果 TypeOf .ActiveControl CheckedListBox 然后
Dim objControl 作为 CheckedListBox = Me .ActiveControl
objControl.BackColor = If (action = 0,SystemColors.ActiveBorder,SystemColors.Window)
objControl.Enabled = action
.Focus
结束 如果

结束 Sub





关于你的主要问题...

好​​像,你必须把注意力集中在另一个控制上或禁用选择 [ ^ ] ...


I am having trouble getting my contextmenustrip to give me the results I want.

I want to be able to disable/enable my checkedlistboxes as required. The problem I am having is that I cant get my contextmenustrip to work with the listbox itself, for it to become the focus of the control I have to click an item which then highlights it and checks the box (I definately do not want this)

The other problem is that when the listbox is disabled it wont allow me to re-enable it using the contextmenustrip.

Also, if someone could tell me how to stop the items from highlighting in my checkedlistbox I would very much appreciate it - even if you uncheck an item it still remains highlighted which is extremely annoying.

What I have tried:

Private Sub MarkCrewInactiveToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles MarkCrewInactiveToolStripMenuItem.Click
        If TypeOf Me.ActiveControl Is CheckedListBox Then
            Dim objControl As CheckedListBox = Me.ActiveControl
            ActiveControl.BackColor = SystemColors.ActiveBorder
            ActiveControl.Enabled = False
        End If
    End Sub

    Private Sub MarkCrewActiveToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles MarkCrewActiveToolStripMenuItem.Click
        If TypeOf Me.ActiveControl Is CheckedListBox Then
            Dim objControl As CheckedListBox = Me.ActiveControl
            ActiveControl.BackColor = SystemColors.Window
            ActiveControl.Enabled = True
        End If

    End Sub

解决方案

Please, read my comment to the question first. I've mentioned there about good programming practice. So, your code can be changed to:

Private Sub MarkCrewActiveToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles MarkCrewActiveToolStripMenuItem.Click, MarkCrewInactiveToolStripMenuItem.Click

Dim tsmi As ToolStripMenuItem = DirectCast(sender, ToolStripMenuItem)
If tsmi IsNot Nothing Then
    Dim myarg As Integer = If(tsmi.Name = "MarkCrewInactiveToolStripMenuItem", -1, 0)
    UnCheckCheckedListBox(myarg)
End If

End Sub


Private Sub UnCheckCheckedListBox(Optional ByVal action As Integer = 0)

If TypeOf Me.ActiveControl Is CheckedListBox Then
    Dim objControl As CheckedListBox = Me.ActiveControl
    objControl.BackColor = If(action=0, SystemColors.ActiveBorder, SystemColors.Window)
    objControl.Enabled = action
    Me.Focus
End If

End Sub



As to your main question...
Seems, you have to move focus on the other control or
disable selection[^]...


这篇关于使用带有checkedlistbox的contextmenustrip - 不是列表中的项目,而是checkedlistbox本身的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆