Menu click causes form to "lose focus" and menu dropdown disappears - c#

I have noticed some strange form interactions while using a ContextMenuStrip or a MenuStrip. I don't really know what is causing it so the following should create a repeatable test for anyone looking into this:
I've created two Windows Forms: Form1 and Form2. Both have a MenuStrip added to them. Both are set to StartPosition = CenterScreen.
Form1 has a simple menu like this: Form2 > Open. Clicking "Open" will launch Form2.
Form2 has a simple menu like this: Try to open > anotherTestMenuItem
Start the program. Form1 opens.
Open Form2 from Form1 (i.e. click Form2 > Open).
Form2 appears.
On Form2, try to open the MenuStrip (click Try to open). Form1 will reappear over Form2, although Form2 still has focus (you can see this if you move Form2 a little before trying to open the menu).
If I set Form2's owner to Form1, Form2 remains visible when you try Step 4, but the menu doesn't display the first time. All subsequent clicks seem fine.
I noticed this when I tried opening a context menu (on a form opened by another form) and it would disappear immediately, but only the first time. Every time thereafter it would open normally.
Does anyone have any ideas as to what is going on?

Yes, this is a known bug in the RTM version of .NET 4.5. This KB article mentions it:
When you click a menu item to open a child window in the application, interactions with the menu and child windows behave incorrectly.
For example, you may experience the following:
- When you open a shortcut menu in the child window, the main window form takes the focus.
- You cannot use mnemonics to access a menu item.
As you can tell from the KB article, this bug was fixed quite a while ago. The bug fix was incorporated in a maintenance release first made available on January 8th of 2013. Be sure to allow Windows Update to deploy that update on your machine. Or download it from here.

Check to see that you have:
"AutoValidate" set to "EnablePreventFocusChange"

Make sure you do not call the Hide method for a menu or contextmenu during its item-click handler. I was in the habit of doing this during development to get a menu off the screen while I stepped through debugging code. However when I launched a secondary form from such a menu, the first right-click in the new form would cause the focus to revert to the primary form, sometimes even if I had left-clicked or typed something in the new form. I don't recall if I found this solution online somewhere or discovered it myself. This may not be the same bug mentioned above, but this behavior still occurs in .NET 4.7.

Related

VSIX and VS SHELL : problems with window show()

If I make non-modal window I get different problems. Firts of all I open the Experimental version of Visual Studio and open a Solution where I navigate to a C# page. Then I open my Extension's WPF window where I can happily type into TextBoxes in that WPF window. However, whenever I click the backspace [<-] or the [delete] keys, this doesn't affect the current WPF textbox but the previously opened C# code window in the current solution. I open my window in this way:
window = new MyWindow()
window.show();
I know that I could resolve my problem with :
uiShell.EnableModeless(0);
window = new MyWindow()
window.ShowDialog();
uiShell.EnableModeless(1);
But I don't want this solution. I want to navigate file in visual studio shell and at the same time using my addin without having problem with input. How Can I resolve this problem using Show() ?
If you want to display a "non-modal" window, it needs to be a toolwindow, or a document window. Otherwise, VS will not know about it, and you'll encounter all sorts of problems with it not getting messages, events, accelerators, etc...
If you want a modal window, use the DialogWindow class as previously mentioned to ensure the IDE can properly disable/enable it's windows when the dialog is displayed or dismissed.
Sincerely,

Visual Studio C# 2010 adding/editing new additional forms to a program

So I am new to actually building GUI and I am creating a project where when you click on one button you open a new form. With this new form I want to be able to add and edit things to it.
In my solution explorer it is only showing the first form that I originally create, not the second one. I went online and used one of those little microsoft blurbs about creating a new form when a button is clicked, and I am a little lost from there.
My program is going to have multiple buttons that you click and they open certain forms. I understand that I have the button_click set to create a new form. I now want to know how to edit these forms that are created.
I tried refreshing my solution explorer and it wouldn't show my newly created design. (I thought that could be one of my possible errors, haha) Could someone please let me know what I should be doing?
First add a new form. Right click on your project (my project is called Test) and give it a name (I called it TestForm):
Then, add the following code to your button:
TestForm MyForm = new TestForm();
MyForm.Show();

How can I prevent my C# application from opening new windows during context menu?

I'm working on some C# application to help myself learn C# a bit. I've been searching for a solution, but I can't seem to find one.
When my application minimizes, it shows a notifyicon in the system tray. When I right click it, a context menu pops up on the mouse. However, for each level of the menu and submenu, a blank window pops up on the taskbar until I exit that submenu or the contextmenu.
I don't know why, and I'd like to. I'd also like to know how to prevent that from happening.
Edit
Also, in Visual C# Studio 2010, when debugging, is there a way to determine exactly which form/part of the code I've focused on, so that I could click one of those windows to see what exactly it is?
How are you showing the ContextMenu? I suspect you are calling ContextMenu/ContextMenuStrip.Show(x,y) directly. That will cause it to show in the taskbar.
Instead, assign your ContextMenu/ContextMenuStrip to the ContextMenu/ContextMenuStrip property of your NotifyIcon rather than manually showing it in the mouse click handler. If it is used as part of ContextMenu/ContextMenuStrip property, it won't show in the taskbar.

Dialog Box loses focus in Windows XP

I am having a strange problem. I have a C# .NET 2.0 application and a context menu. When you right click on the application, the context menu shows up and one of the items on the menu is Options. When you click options, I have an OptionsDialog form that shows up (using ShowDialog() method). The problem is, on Windows XP, the focus keeps going away. If I click on the options form, its gains focus for a second, but then loses it again! I have tried everything. On the Deactivate event, I call this.Focus() but that doesn't seem to make a difference. I also tried making the window topmost, but still doesn't work.
Another wierd thing is that this problem only seems to happen on XP. I tried it with Vista and Windows 7 and it works perfect. XP is doing something wierd though. Anyone have any thoughts on how to solve this?
A person here seems to have the same problem: http://forums.devshed.com/c-programming-42/c-form-won-t-focus-on-control-535596.html
Is that anything like what's going wrong with you? If you have a default button or a control that is the first tabStop value that also happens to be disabled when the form starts up, it could be the cause of the problem.
Check any disabled controls on the form, and make sure the default button and the first tab stop controls are both enabled. (Hint: Default button should usually be 'Cancel' on a modal form, and it should always be enabled.)
If you find out which window has the focus afterwards, maybe it gives you a clue what's going on.
This little demo app from 1997 will help you: http://www.microsoft.com/msj/0397/Win32/Win320397.aspx

Controlling many forms at the same time

Suppose, I have the following task.
There is a main application form with numerous buttons; when you click on each of these buttons, you get one more form. The second click on the button should close the opened form.
These forms should not be shown in taskbar, because they are auxiliary. They should not be dialog, because that will block the main application form and prevent user from performing some other operations with the main window or with other forms like the one that is opened. They should be shown above the main form, but not above other windows (so TopMost doesn't suite). When such forms are closed, the main form should be notified, and when the main form is minimized, they should be minimized as well.
How would you solve this problem?
The current solution is based on using hooks. It's not very easy to understand and very difficult to maintain (mainly because I am not experienced in win32). It works fine for about 90% of situations, but in 10% it doesn't.
Maybe, the requirements to forms behavior are to strict?
I would recomment using some window-manager, so each button will tell the window-manager to toggle visibility of form X, if X is not there it will be shown, if it is, it will be closed.
You should be able to configure the windows such that they will not show up in the taskbar.
For the visibility, I am not sure what you mean. When you open a form via a button, it will usually go to front, which should be ok. If you switch the window then, what do you expect? Should the main window always stay in the background or is it allowed to come to the front when focused?
Whenever I have seen applications change such standard behaviors, they failed in some way. Either wrong windows showed up or some were not accessible anymore or the user was just confused because these windows didn't act like all other windows.
It's not a problem. Use the Show(owner) overload to display the form.
An owned form is always on top of its owner. It minimizes automatically when you minimize the main window. No need for ShowInTaskbar. Another window model supported by winforms is MDI, check out the MdiParent property. The child windows are confined inside the bounds of the main window. Also consider using UserControls instead of a form, you can swap them in and out as needed. Or a tabbed interface, using TabControl. Weifenluo's DockPanel suite is a very popular implementation of the Visual Studio style windowing model, supporting windows that can be docked and floating within the main window. Plenty of choices here :)
Here's how most of your requirements can be accomplished:
Set the Owner of each child Form to be the Main Form.
MSDN Quote:
When a form is owned by another form,
it is closed or hidden with the owner
form. For example, consider a form
named Form2 that is owned by a form
named Form1. If Form1 is closed or
minimized, Form2 is also closed or
hidden. Owned forms are also never
displayed behind their owner form. You
can use owned forms for windows such
as find and replace windows, which
should not disappear when the owner
form is selected. To determine the
forms that are owned by a parent form,
use the OwnedForms property.
For stoping a form from appearing in task bar you need to set the ShowInTaskBar property

Categories

Resources