How to detect context menu disappearance? - c#

I need to make a control with popping out menu and button that causes it to be displayed and has changed (pressed) appearance during the time popped out menu is visible.
Currently i am using check box with Appearance property set to button and context menu. However detecting a moment when menu disappears from any reason other then click on one of its items (which generates Click event that can be handled) - for example, when the form is clicked - wasn't successful, as "Collapse" event didn't occur. As a result, "down-arrow button" didn't change its appearance back as intended.
How can disappearance of ContextMenu be detected? What are conditions for "Collapse" event to occur?
P.S. On related note, i was trying to implement the same thing with listbox set as top level window before; when form was clicked, it changed appearance but the loss of focus didn't occur. Any help with detecting the moment when it occurs would be greatly appreciated.

If you use the ContextMenuStrip you can handle the Closed event which is raised when the menu is closed.
"How can disappearance of ContextMenu be detected? What are conditions for "Collapse" event to occur?"
The Collapsed event is only raised of the ContextMenu is set as the ContextMenu of a control before you call Show

Related

How to disable mouse buttons in C# WPF?

During big files loading I want to disable mouse buttons to unable user to click on UI elements and triggers events.
Edit
When I am loading big size file in my WPF Caliburn Micro application I changed IsEnabled property of Listbox to false, because I want to disable a button during this process.
Next when file is loaded I changed IsEnabled property of Listbox to true.
After that every click on disabled button raised events and I did not want that.
I don't know how to remove this events, and where there are stored, so i thought that the easier way to solve this problem is to disable mouse buttons during file loading process. But it is also not easy...
Thank You in advance!
If you only want to disable mouse buttons, the user can still use the keyboard. So you need a different technique.
You can add a hidden Gird with Opacity="0.5" to your window. When you want to prevent the user from using the window, just call visible the grid.
What about overriding SelectionChanged event on ListBox and setting it to Handled = true when loading big data?

WinForms Button Click event and similar events depend on focus

I have the next situation:
there is a client app with a Form
the Form contains a few TabControl's
there are different controls on TabPage's of TabControl's
when the user clicks on any control, I need to activate the TabPage that is a parent of a control. For that I use a TabPage Enter event
when the TabPage gets activated, I need to make request to the server app, and I put focus to a hidden TextBox to disable UI
The problem is, when I click on a Button on another TabPage, in TabPage.Enter event handler I take focus to my hidden TextBox, and it seems like Button click event doesn't have enough time to be processed. When I put timer and handle TabPage.Enter event after 100 ms, the Button click event seems to be fired well. Same thing happens to all the controls: CheckBox doesn't get checked, RadioButton too. I wouldn't like to use timer, as that is not a stable solution.
Any ideas how could I make TabPage to process all mouse events before I take focus to hidden TextBox? I tried to use Application.DoEvents(), but that didn't help.
You are using a wrong event for a wrong control for what you are trying to do.
Enter event for TabPage is going to be fired when that page becomes an active control of the current form which might not happen under certain conditions. Instead, you need to use Selecting or Selected event of TabControl, depending on whether you want to cancel switching to a different tab or not (see TabControlCancelEventArgs parameter of Selecting event). In your case, Selecting event would be more appropriate since it won't allow switching to a selected tab until event is complete (unless you're doing an asynchronous request to the server). Additionally, you may no longer need to use the hidden TextBox.
UPDATE
Regarding comments to OP, when you have 2 (or more) TabControls on a form and you want to get notified when you press a button in any tab of a different TabControl, you can use Enter event for TabControl. Use a global variable to store which TabControl was activated in the Enter event and then send server request for a currently active tab of that activate TabControl.
If this doesn't work for your scenario, then you need to carefully examine your workflow and see if it can be improved in relation to what you want to accomplish.

WPF Prevent button from taking focus from any other control

I have an "On screen keypad" with some up/down/left/right/select buttons.
The select button is effectively a click and the arrow keys fire the associated up/down/left/right key.
The problem is that when selecting a combo box, I can't press the down/up buttons to navigate the items in the list. It is because the combo box auto closes when loosing focus. I can see similar problems happening with other controls, so I would like to see if there is a way to do the following.
For certain buttons (up/down/etc), when clicked, fire the click event, but don't take focus from w/e currently has the focus. This would allow the combox dropdown to stay open while pressing up/down to navigate through the items.
I have tried to set Focusable=False on the navigation buttons but the focus is still taken away from the combo box and the dropdown closes.
Any ideas/suggestions?
Thanks in advance
This isn't happening because of anything your Buttons are doing so changing their focus state won't make any difference. ComboBoxes close when you click anywhere outside of them, including empty space, non-interactive controls, other windows...

Windows Forms radio button click event is getting on start up

I have eight radio buttons on a user control. Each of them has or will have their own click handler. The first one has its click handler called when the user control is added to the main window. Is this normal behavior for Windows Forms (I am relatively new to .NET from a Java background)
This effect is likely the result of the default control selection rather than the control being added to the form. When the form finishes loading, one of the controls on the form will become the active control/have focus. If that control is a radio button, the button becomes checked, which will fire events like Click and CheckChanged (unless the radio button Checked property was already set to true). Depending on the Checked property value of the other buttons, you may see their CheckChanged events fire as well.
To test this out yourself, change the TabIndex property value in the designer so that some other control on the form will have the lowest index. This will make that control have focus on startup instead of the radio button(s). When this happens, you should not see the Click event being fired when the form is loaded.

ContextMenuStrip Behavior Problem

I am adding a label to a form dynamically, then dynamically adding a ContextMenuStrip control. Whether I use the label.ContextMenuStrip property to connect them, or add the event handler to the label manually to have it respond to the right-click and show the context menu, I get odd behavior:
1) The menu does not appear next to the mouse pointer, it is offset down and to the right. It appears that it is related to the position of the label in it's parent control (a picture control), rather than the form.
2) The menu does not disappear when I click on something other than the menu.
Any ideas what I am missing here?
Thanks,
Andy
I worked it out. First off, I was adding the control to the label control collection, changing it to the the form collection corrected the positioning problem. I never did figure out why the menu would not close.
Ultimately I restructured things by adding a static instance of the menu to the form, then just connected the label.ContextMenuStrip property to that stastic instance. All is well with that approach.
I used the tag of the label control to identify it to the click event handler.

Categories

Resources