Mouse Click Event Problem w/ Contextmenustrip

josh27

New member
Joined
Aug 3, 2008
Messages
2
Programming Experience
3-5
Hi all,

I have a problem with the UI I am working on. I want to dynamically add a label to a table layout panel and add a handle to allow the user to click on that label (right click) and popup a context menu. Now ON that right click, I want to populate some items in the context menu strip. All of this is no problem and I have it working. However based on the even I am linking with AddHandler, I am running into a problem with how the event is managed. Here is my code.

VB.NET:
' this code links my new label control to a function called BuildTeacherContextMenuList
Me.tlpDaily.Controls.Add(defaultLabel, colIndex, 0)
                        AddHandler defaultLabel.MouseUp, AddressOf BuildTeacherContextMenuList

' this is my BuildTeacherContextMenuList function
 ' if the right mouse button is clicked...
            If e.Button = Windows.Forms.MouseButtons.Right Then

                tsmiScheduleTeacher.DropDownItems.Clear()

                For Each Teacher In dsUsers.Tables(0).Rows
                    tsmiScheduleTeacher.DropDownItems.Add(Teacher("Teacher"))
                Next
            End If

^^ These above are of course just small chunks of the functions I am working with, but I believe they are the relevant parts...

So as you can see, I am using the MouseUp event to track this right click. I am also using some DB code which I cut out of the buildteacher function but you can get the jist.

THE MAIN PROBLEM IS ... when I right click on an "added" -defaultLabel- object on my table layout panel, if I click AGAIN on the context menu strip...say for example to expand a context menu strip item's drop down list ...it completely messes up the event and my third function:

VB.NET:
Private Sub AnotherSub  Handles tsmiScheduleTeacher.DropDownItemClicked

^^^ this guy gets completely confused and doesnt handle anything.

This is getting very confusing, but the short version is, if I click on a context menu strip, after I have already caught the initial right click with the MouseUp event, it kind of looses or forgets what the object was that triggered the first MouseUp event with the initial right click.

MY QUESTION: Is there a better event I can use besided MouseUp to catch right click events? I have used Click and MouseClick, but they didnt seem to work right or at all compared to MouseUp.

If this is completely impossible to understand please let me know and I will try to reword it. It's kind of a strange UI problem. Thanks in advance for any help!!

Josh
 
tsmiScheduleTeacher.DropDownItemClicked.... this guy gets completely confused and doesnt handle anything.
"gets completely confused" - how??

"doesnt handle anything" - Each "panel" of dropdownitems has its own DropDownItemClicked event, if you're adding subitems to an item of tsmiScheduleTeacher you have to AddHandler the DropDownItemClicked of that tsmi too.

it kind of looses or forgets what the object was that triggered the first MouseUp event with the initial right click.
If you're talking about ContextMenuStrip.SourceControl it has a known bug of loosing the reference in some cases, the known fix is to cache it from ContextMenuStrip.Opening event.
 
Fixed...

That was pretty much spot on for my problem John. The source control object was getting set to NULL if I clicked on another object on the context menu. As long as I didnt click on anything and just moved my mouse over the drop down items list, the bug didnt trigger.

So like you suggested, on the opening event I stored the control to a swap variable and used that in my dropdownitemsclicked event function and voila...no more problems.

Thanks again!

Josh
 
Back
Top