ToolStripSplitButton behavior override - winforms

I'm trying to understand what do I have to do to override the behavior of the ToolStripDropDown control on System.Windows.Forms where if you use this constructor:
var button = new ToolStripSplitButton("text","path to image", clickEventHandler)
then the drop down will only show if I keep the mouse pressed and if I use this other
var button = new ToolStripSplitButton("text","path to image")
then the drop down will show when I click.
It is clear to me that supplying a click event handler is very explicit in saying "hey, when I click, execute this" but in the case of a ToolStripSplitButton the distinction blurs a bit because of the split nature of the control itself.
So, what I like to do is
a) When the user clicks on the button part of the ToolStripSplitButton, the click event handler executes as normal
b) When I click OR press the mouse on the arrow part of the ToolStripSplitButton then the drop down shows
Is there any OOB property/method to do just this?
Thanks

The ToolStripSplitButton has two click handlers. One is called "Click" and the other is called "ButtonClick". The one from the constructor is the "Click" handler and fires no matter where on the control you click. The "ButtonClick" handler only fires when you click the button itself, not the arrow.
Try this:
var button = new ToolStripSplitButton("text","path to image");
button.ButtonClick += clickEventHandler;

Related

Handling/capturing a button click event as a priority in WinForms

This is the mock-up of a child window that I have in a Windows Forms application.
This will be shown when a button is clicked on the parent window.
When the child form opens up, the focus is in Text box1.
The Leave event on the Text box1 fires when the user tabs out of it or clicks on any button.
This triggers a validation:
if(String.IsNullOrEmpty(textBox1.Text)
{
MessageBox.Show("Please enter a value of Id.");
}
else {
... other logic omitted for brevity
}
Now the user can also click on the Cancel button to exit the form without doing any action.
Here, on the first try, the Leave event fires because the focus is in Text box1 when the Form is loaded so when the user clicks on Cancel for the first time the form does not close, instead the message box with validation message is shown.
On the second click, since the focus is no longer on Text box1, the Leave event does not fire and the form closes.
I can put the focus on any other control to handle this but for the sake of user experience I had to put the focus on Textbox1 since it's value decides the further logic of what is shown in Textbox2(Textbox1 can be left empty in which case nothing is shown in Textbox2 but that is not related to this question).
Tried this to see if I can get the Cancel button inside the Leave event of Textbox1:
Button b = sender as Button
b is NULL when Cancel is clicked.
What can I do to make the Cancel click work, without triggering the Leave event initially?
Thanks in advance,
Regards.
If you only want to supress the error message when the cancel button recieves the focus, simply check for the cancelButton.Focused flag in the leave event, then block the message if the cancel button got the focus.
Nevertheless, it is usually considered good practice to avoid modal dialog boxes if they are not required. So depending on your particular situation, you may decide to replace the MessageBox with a ToolTip instead, which will not entirely block the UI thread.

How to Create an "Apply" Button on a Modal Dialog Window

I have a vb.net WPF application that has a modal dialog window that has an "Ok", "Cancel" and "Apply" button. The "Apply" button does the same work as the "Ok" button but when the "Apply" is clicked the modal dialog box should stay open. Normally I like to call ShowDialog to display a modal but that results in the modal closing when it returns so I can't use that with a modal the has an "Apply" button. Can someone provide me with an easy work around? The alternative is to display the modal dialog window using .Show but in this case I'm not sure how to properly return values when the user clicks on one of the buttons.
Thanks!
The closing of the dialog is handled by the code that executes when the user clicks the OK button. So you could move all of that code to a function with the exception of the Close() call and the call that sets the DialogResult. Then you can call that function from both the OK handler (or ICommand) and the Apply handler (or ICommand). Then just don't call Close() from the Apply button.
Dialogs with an "Apply" button aren't generally modal, but one approach would be to instantiate your dialog and supply it with a ViewModel via DataContext where your dialog data can be shared between the function calling the dialog and and the dialog itself. Then, wire your "Apply" button to be handled by the modal dialog and whatever ViewModel code you have, perhaps via an ICommand binding.
Let us know if a sample would be useful.

WPF Popup Activation and Deactivation

Is there a possibility to detect if a Popup is activated or deactivated? Such as the Window-events Window.Activated and Window.Deactivated.
I want to detect if the user has clicked outside of the popup (I can not use the Popup.StaysOpen=false option).
Update
I have forgotten to remark, that I dont want to add any handler or code to a parent-control of the popup.
Capture mouse clicks for your main application window (MousePreview type event) and for popup - when user clicks into popup, main window event doesn't fire and vice versa.
Sounds like you could just use the Popup.Opened and Popup.Closed events.

Activating main window when button is clicked in a user control

I have a WPF user control with some buttons inside it. I need to intimate the main window when button click happens inside the WPF control. The main window has to initiate some actions upon receiving this message from the button click event inside the user control.
You can add a Click handler to all your Buttons, and in that Click handler, call the UserControl's OnClick method to generate a click event coming from the user control.
Check out Commands:
http://msdn.microsoft.com/en-us/library/ms752308.aspx

Target x button of a window title to an Button.Click event handler?

I have in my window a button called btnExit which I handle its event.
I want that when I click the exit button (the red X in the right side) of the form, the event should be handled by the btnExit event.
Is this possible whithout need to use the OnExit or Closing etc.?
I guess that's impossible.
I reported a suggestion here, please vote.

Resources