ToolStripButton have different behavior on showing Modal Less Dialog - c#

I have a main window and a toolstrip on it with different command buttons. In these commands, I've a 'Print' button too (See Below). When I click on 'Print' button , I need to show sub-form as Modal Less Dialog. Because, I've few option on sub-form. If user select them then he/she can interact with Main Form too.
Meanwhile, on show() method I disable all controls on Main Form (see below) as it will be done if I use ShowDialog() method to show sub-form. When I click the Print Button, it's color changed which shows it is focused/selected.
On click sub-form is show like below pic.
Logically, it should return to previous mode when I close sub-form. But, even sub-form is showing... that 'Print' button on Main-Form is still focused/selected. When I close the sub-form, that 'Print' Button still focused/selected like below.
What Event/ Property needs to be changed to make this 'Print' Button to show like as it is in initial state.
I've tried Invalidate(), change BackColor but didn't meet the requirement yet. Any Guidelines ?

Set the CheckOnClick property of your button to false if you don't want it to appear "selected" at all, otherwise toggle the CheckState property on the button when the subform is closed.

Well, Selected Property in ToolStripButton is read only. Anyone, needs to clear the selection of toolstrip buttons can use below method which is invoked via reflections.
MethodInfo method = typeof(ToolStrip).GetMethod("ClearAllSelections", BindingFlags.NonPublic | BindingFlags.Instance);
method.Invoke(yourToolStripName, null);
This comes from : How to Deselect ToolStripItems
Happy Programming.

Related

C# Prism how to change the default DialogResult passed when closing a dialog window

I have created a _dialogService and I am showing a dialog in prism.
I am handling all the button presses in the ViewModel. I have handeled the Esc key.
But I can not find a way to handle the Close window button [X].
Yes it calls the CanCloseDialog method but I would need to pass a DialogResult there but cant find a solution to do that. On default it passes DialogResult.None but I would need it to change depending on circumstances.
What is the best or any approach to change the default DialogResult passed when closing the window using the [X] button?

In C# Windows Forms, does setting a form's CancelButton property to a button automatically close the form on the button click event?

Or by setting the CancelButton property of the form, we just ensure that the button gets enabled on pressing Esc key on the form, but the click event needs to be handled by writing a separate event handler?
In C# Windows Forms, does setting a form's CancelButton property to a button automatically close the form on the button click event?
Yes, because doing so sets the DialogResult property of that button to DialogResult.Cancel. As the documentation for that property states:
If the DialogResult for this property is set to anything other than None, and if the parent form was displayed through the ShowDialog method, clicking the button closes the parent form without your having to hook up any events. The form's DialogResult property is then set to the DialogResult of the button when the button is clicked.
Do note that important caveat: "if the parent form was displayed through the ShowDialog method". The only way your form will automatically close is if you displayed it using frm.ShowDialog(). If you used the Show method, it won't automatically close. You need to write code to manage that yourself.
Only if the form is a dialog. Test it yourself

How do I make a custom modal popup window in WPF and have it return a value?

I'm taking a whack at WPF and trying to learn as I go. I'd appreciate any advice offered.
I've got a Window that has a Page attached to it (through a Frame on the Window). When you press a button on the Page, I want a custom window to pop up to present several custom options and be displayed in a manner of my choosing (I'm thinking right now I want it to be a grid but that may change as I go on). When selected, the modal window will disappear and return to the calling method (button press from the Page) the value of the selected choice.
I don't want the standard windows dialog box with the options of yes, no, okay, cancel, or anything like that. This is truly just a custom popup that returns a value to the caller when the user makes their selection on the popup.
Create a new Window subclass, which you can layout however you like. Then in your button click event handler, display it modally using myModalWindow.ShowDialog();. You can then have a property on the window class which you can access after it closes in order to access result data, i.e.:
myModalWindow.ShowDialog();
var data = myModalWindow.SomeResultProperty;
If you really want to have something returned from a method, I suppose you could create your own public method on your window class which internally calls ShowDialog() and then returns a value.

How to select a button as default button of the form

I have a form that takes user input and then let the user get connected to the SQL-Server.
This is happening on Button.Click. But where can I set the property Default button so that when the user clicks Enter it does the work of that button.
It is called AcceptButton now on the form; set that to the button that will be the default button.
Refer to Form.AcceptButton Property
I think you want the "AcceptButton" property at the FORM level... That will expose a combobox of available controls on your form, then select your "button" you want to use as the "Default" button on enter.
In addition to Form.AcceptButton property the "OK" button must have the TabOrder property set to 0 and all other controls within the form should have a TabOrder >0.
This can be done using a form resouce contruction kit or by code eg.
buttonOK.TabOrder = 0;
I have noticed severally how there is a mix up when it comes to an active button and an accept button. I just came out of it. So, I just thought I add a little option to the answers already given. Obviously, the best answer is;
this.AcceptButton = AcceptButton;
However, if you wish to have the button as an active control, this is what you do;
this.ActiveControl = OkButton;
details: https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.containercontrol.activecontrol?view=netcore-3.1
I hope it is helpful to anyone searching.

Interacting with a form without it activating

I'm trying to implement code-completion popup window in my project. The window is derived from Form. It contains two controls: custom list derived from UserControl (it shows completion possibilities with icons) and a VScrollBar.
When the popup appears, it doesn't steal focus from the editor (form's ShowWithoutActivation is overriden to return true) and the editor sends certain keystrokes to the popup so the user can interact with it using keyboard. So far it works like a charm.
The problem is, I want to allow the user to use mouse as well. But, when the user clicks into the popup window, its form activates and steals focus from the editor. I can react to this by giving the focus back to the editor, I have even set up a Timer to do this regularly, but apart from being a poor solution, the title bar of the editor always flickers when this happens (when the popup is clicked).
Is there any way to interact with the popup form (using mouse) that doesn't make the form activate?
The ShowWithoutActivation's documentation reads: "If your non-activated window needs to use UI controls, you should consider using the ToolStrip controls, such as ToolStripDropDown. These controls are windowless, and will not cause a window to activate when they are selected." This seems exactly like the thing I need, but I want to use a custom control and a scroll bar.
The same problem would be with a tooltip that shows these two arrows to switch method overloads (known from VS) - the whole form would use no controls at all (only render the text and the arrows), but when clicked, it should not activate. The problem could be summarized up to "How to create a form that would never activate, but allow the user to interact with certail controls inside?".
Thanks.
Just override the onFocus event...
public partial class myListBox:ListBox
{
protected override void OnGotFocus(EventArgs e)
{
}
}
The issue is that you're using a Form for this rather than building some custom control that doesn't run in its' own UI thread like a Form does.
The flashing and highlighting is handled by windows whenever a Form activates/focuses. The only thing I cay think of is to make your Form borderless and create/draw/handle your own title bar that doesn't flash when focused.
OK, I may have found a solution. The key seems to be WM_MOUSEACTIVATE message, which the popup form must intercept and respond with MA_NOACTIVATE. But there's a catch - the control derived from UserControl still grabs focus when clicked (the scrollbar luckily doesn't anymore). The problem seems to be in the UserControl.OnMouseDown method, which internally puts focus on the control. There are some ways to fix this:
derive the control from Control instead of UserControl
override the OnMouseDown method and not call base.OnMouseDown there
make the control's CanFocus property return false, but this seems not possible, because that means to make the control either not visible or not enabled, which is both undesirable
The last case when the popup form steals focus seems to be when its resizing (using mouse) ends. But it is safe here to call Owner.Activate() as a result to Activated event...

Categories