I want to set focus to an MDI Parent Form when I click on the background of the form. However, the only way I can get it to set focus is when I resize the form.
I have tried using mouse click event, click event, key press event etc to manually set the focus when you click on the MDI Parent but none of these events fire. Is there ANY way to set the focus to the MDI Parent when you click on the background of the form?
That background is a separate control, try to find it in MainForm.Controls and assign it's click event.
You may want to look at the Win32 WM_MDIACTIVATE message. Now that we've discussed a possible solution, the real question can begin:
I think you should look long and hard at what your trying to accomplish. You risk (not necessarily will, but risk) creating a behavior that is abnormal and confusing to users. Why do you want to move the focus? What will you once it gets moved? How will you indicate to the user that this has been done? How will then get out of this state?
Related
Just as an example, say I have a form with a listbox. From this form I open a dialog window using dialog.ShowDialog(this), which has another listbox. Normally, the user would double-click items in the dialog's listbox to add it to the owner form's listbox, then close the dialog when they are done. I want to know if I could enable drag and drop so the user could instead drag an item from the dialog's listbox to the owner form's listbox. From what I can tell, at least on my Windows 7 computer (using .Net Framework 4.0), this is not possible.
An additional feature I'd like, but is not necessary, is if the owner form could be brought in front of the dialog window while user is dragging item over owner form. (This is to enable the user to better see the listbox on the owner form while dragging.)
I dont think this is possible, more accurately not advisable. Clarify if I am wrong. What you are trying to do is drag an item from a modal that appears super-imposed on the form ( Instead of double-clicking and bubbling the data back up to the parent). In order to achieve a drag and drop, somehow you will have to, upon clicking to drag, trigger a loss of focus on the modal, and then drop, after which you want to regain focus on the modal. To me this seems like a circuitous way of going about a minor quality of life improvement.
This can be done by having the owner form handle the drag event directly rather than having the child control on the form handle the drag event. Set AllowDrop = true and handle DragDrop and DragOver events for the form. You can use control.ClientRectangle.Contains(control.PointToClient(new Point(e.X, e.Y))) in the form's drag events to determine if the mouse is in the desired control's client region.
You cannot bring the owner window in front of the dialog (and probably shouldn't try), but the user can easily move the dialog out of their way if necessary before beginning the drag.
Note: You may want to add if (!this.CanFocus) e.Effect = DragDropEffects.None; to your form's DragOver event to disable drag events while dialog is being shown, unless the drag is from the dialog window.
In a WPF application I'm working on, I have a MenuItem with two items in it. When the MenuItem is clicked, it takes keyboard focus and opens its submenu. When the MenuItem is clicked again, the submenu is closed and for some reason, the main window takes keyboard focus. I need the MenuItem to keep focus.
I have been told that in certain other situations, the main window may be given keyboard focus - for instance, if a control has keyboard focus and IsEnabled or IsVisible becomes false. In which situations does this happen? I've been Googling like crazy but haven't found any information about this.
As far as I can tell, this is the expected behavior. WPF Menus are focus scopes by default, so any control within the menu that receives focus won't change the main logical focus of the window.
Also, certain WPF controls will sometimes call Keyboard.Focus(null); (e.g. Button does this when clicked). This call has the effect of returning the keyboard focus to the main logical focus. I suspect this is also happening when a menu is closed.
Try disabling the focus scope on the menu: <Menu FocusManager.IsFocusScope="False">
When the menu item receives the keyboard focus, and it's not in any focus scope, it will gets the main logical focus. This means the Keyboard.Focus(null) call will keep the focus on the menu item. Jowever, this will also prevent commands in the submenu from returning the focus to the non-menu window content, so routed commands won't be able to find their target.
See "For What was FocusScope Designed?" in Using the WPF FocusScope.
I have a form that requires a long operation (expansion of a treeview node searches the network for additional items to create more tree nodes) - so I plan on using a BackgroundWorker for this task. During the long operation I want the cursor to be the wait cursor and I want the entire form to be unclickable except for the Cancel button. I know I could use Enabled=false but this turns the treeview grey which looks pretty lame imo.
I could just NOT use a BW but that means I have to use DoEvents to get the cursor to change and that possibly "Not Responding" would show up, which I hate.
I thought of handling all the mouse click events and keyboard events so that they are cancelled if the BW is busy... so that is my current plan. I just wondered if I am missing something, if there is another way.
Thanks.
There is no easy way to do that. It is better to fix your treeview and use Enabled property. You can also show your progressbar in Modal dialog - that will block UI
You could use a Panel as an overlay over the form, wholly or partly transparent, which only propagates clicks when over the cancel button - similar to the way browsers simulate modal windows by 'graying' the background with an overlay.
When you are in processing mode, set the Z-Order of the mask to be in front of all other controls, and when that finishes set it behind them.
You could use Background worker and pop up another dialog with progressbar and that shows current progress and a cancel button. Where you can user
popup = new ProgressWindow();
popup.Owner=this;
popup.show();
And the cancel button will cancel the background worker. In this way your back form will not be clickable and popup will remain on top with cancel button.
I would like to remove the original event behavior of controls within a form (similar to design mode).
So, when the user clicks on the button, i only want to capture that event. I do not want the original button event to be fired. Is this somehow possible?
I am looking for a generic solution. So it should work with any form and any control within the form.
Reason: I wrote a form validation rules designer. It uses reflection to enumerate all form-types in the entry assembly. The user can then select a form type, the designer creates that form, enumerates the controls, and embedds the form in the designer panel.
clicking on a control, opens a formular designer panel, and the user can now create a formular for that control and saves the formular to a DB.
When the form is then opened in the normal "runtime" mode, it loads its validation formulars.
Events are not in fact disabled in the Winforms designer. The designer executes the constructor of the form through Reflection, everything in the InitializeComponent() method executes, including the event subscriptions. Wherever this might cause a problem, the controls check the DesignMode property (prevents a Timer from starting for example) or by custom designers. The form is displayed underneath a transparent layered window on top of which the selection rectangle and drag handles are painted. Which prevents issues with mouse clicks and keyboard focus.
You probably ought to look at this magazine article to get this working for you.
From what I understand from your question, I guess, you can still use the "DesignMode" property for this as well. In your event handling routine, you may want to bypass execution by checking on this property:
if (this.DesignMode) return;
as the first statement in your event handling block of code.
I have a custom UserControl. I want to use it in a few different products, so I want something that can be implemented inside of the UserControl itself. I want to know when the user has clicked outside of the bounds of the UserControl so that I can hide it, similar to a ComboBox. How can I do that?
I tried handling the click event, but it only seems to fire if the click occured within the bounds of the control.
That's what the Capture property is designed to do. Set it to true and all mouse messages are routed to your control, even if it moves out of the window bounds. Check the e.Location property in the MouseDown event.
Hm, you may be able to accomplish what you want by listening to the GotFocus/LostFocus events. ComboBoxes give the drop downs focus when they open and close them when they lose focus.
do this
Select all controls on your form including form
In Property Window select MouseClick event
Now enter below Code in Common_MouseClick
Code:
if (!sender.Equals(yourControl))
{
yourControl.Visible=false;
}