Click here to Skip to main content
15,078,056 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

VB.NET
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
Posted
Updated 22-May-17 11:38am
v2
Comments
Maciej Los 22-May-17 17:18pm
   
When some piece of code is used more than once, you have to move them into another subroutine (with or without parameters). Take a look at your code, the only differ is in BackColor and state of Enabled property.

1 solution

Please, read my comment to the question first. I've mentioned there about good programming practice. So, your code can be changed to:
VB.NET
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[^]...
   
Comments
Maciej Los 23-May-17 1:50am
   
Note: Above code has been writen "ad-hoc", so it could contain mispellings, etc.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900