I have two windows form and both set the topmost property to true. But the form border style of the one form is Set to "None". The other form has border. When these two forms launch together, the one without the border is always on top and overlap the other form. How do i make the form with border always on top? thanks.
I suggest you to start the form with borders last or set other form TopMost property to false.
[Edit] I sugest you to look at the Forms Owner property - http://msdn.microsoft.com/en-us/library/system.windows.forms.form.owner.aspx.
formWithoutBorders.AddOwnedForm(borderForm);
borderForm.Show();
Show (make Visible) the window that is supposed to be on top, later as the other window.
Topmost only works between non-topmost and topmost windows. Between Topmost windows, the normal rules apply of what window to show.
To understand why this is happening, I recommend Raymond Chen's What if two programs did this article
Related
I have created an MDI application that I want to add a ToolStripContainer to. My first try was to just add one and dock it on Top. Problem I got here was moving ToolStrips inside the container's ToolStripPanels would create an extra Row which was invisible.
Then I saw this question and tried to implement my do-it-yourself MDI interface, using the ToolStripContainer's ContentPanel as where the windows go. This way, I can have my ToolStrips anywhere around the MDI.
The code I have is as follows:
Form f = new Form();
f.TopLevel = false;
tsContainer.ContentPanel.Controls.Add(f);
It all works remarkably well, but I am wondering if this is smart, would I not run into problems later on? Also, if this is possible, why have the entire MDI functionality in Windows?
By setting the TopLevel property to false, the form essentially turns into a UserControl. Do note that you have to explicitly set the Visible property to true, it isn't automatic.
But yes, there certainly are problems and it doesn't behave like an MDI child window at all. The biggest issue should be readily visible, the form can no longer be activated. Very noticeable from its caption bar, it will always be rendered with the colors for a non-active window. It however still works like a caption bar, the user can drag the window and move it around. Which has the clear failure mode that the window is going to get clipped without scrollbars. Not only that, the window can still be maximized and minimized by double-clicking the caption. An MDI child does this very differently. And the visual style for the frame will be wrong in Windows 8.x, looking like a Win7 frame instead, a quirk that has no known workaround.
Realistically, you have to set the FormBorderStyle property to None so it behaves more like a true child window. Or write a bunch of fairly nasty code in the WndProc() override to tame the window, you'll be doomed to re-invent an MDI child, imperfectly.
Which is otherwise a fine way to embed a form. The only remaining quirk after fixing the border is that the form still gets added to the Application.OpenForms collection.
I need to add a white layer on top of a Main form when a modal window appears on top of it so that it gives prominent to the modal window and kind of make the background transparent.
Add 75% Opaque white background for example...
Can it be done in Win forms?
Thanks!
The easiest way to do this might be to disable the main container control in your main form. That usually gives a faded look to everything in it.
You can set the parent form enabled property to false after you open the child form, which will make the parent form read only and darker, so it's obvious the parent form is not active.
form1.Enabed = false;
I want to prevent my desktop application from being maximized. It should not become maximized by any means - by double clicking on title bar, or by clicking Windows + Up arrow on the keyboard, etc.
-> I disable both the MaximizeBox and MinimizeBox.
-> I also set the MaximumSize and MinimumSize for my WinForm
Still when I press Windows + Up arrow, my win form Shifts to top left of the screen, I mean it gets maximized.
So please tell me any way to prevent this thing happening...
There are actually two completely separate issues at work here. Granted, they look basically the same from a functional perspective, but from an implementation-model perspective (as Windows itself would naturally use) they are unrelated.
As others have answered, you can prevent your form from being maximized by setting its MaximizeBox property to false. This removes the WS_MAXIMIZEBOX style on the form's underlying window, which prevents the window manager from allowing the window to be maximized. It disables the maximize box on the window's caption bar (the one between the minimize box and the close box), disables the "Maximize" option on the window/system menu, and any other methods of maximizing a window that I may have forgotten.
However, this has nothing to do with the Win+↑ keyboard shortcut, which invokes Aero Snap the same as would dragging the window to the the magic position sat the edges of the screen. The window manager, whether as a result of a bug or a feature of Aero Snap, still allows you to effectively "maximize" windows that should not otherwise be maximized. Setting the MaximizeBox property doesn't affect this, so if you truly want to prevent the user from changing the window's size this way, you will need to disable Aero Snap's effect on your window.
Unfortunately, there's no method (at least, not to my knowledge) of programmatically disabling Aero Snap on a per-window or per-process basis (also see this related question). If the user has Aero Snap enabled, you can assume that they want that behavior and applications aren't supposed to tamper with it. The only thing you can do is completely disable resizing your window. In WinForms, you do that by setting the FormBorderStyle property to one of the following: FormBorderStyle.FixedSingle, FormBorderStyle.Fixed3D, or FormBorderStyle.FixedDialog. If you still want your window to be resizable in other ways, you will need to write the code to handle that manually, which is not a particularly easy task.
Thus, I encourage you to very carefully consider whether this is really necessary. All other non-maximizable windows accomplish this simply by setting the MaximizeBox property (or doing the equivalent in their UI framework), which means that they can still be effectively maximized via Aero Snap. If this works for everyone else's windows, including those that are part of Windows itself, it should probably work for you.
The form has a property called MaximizeBox - set this to false.
In regard to your second question, check out this question and it's answers for the best ways to implement keyboard shortcuts in WinForms.
this.FormBorderStyle = FormBorderStyle.FixedSingle;
That line of code will prevent the user from re-sizing the Window.
In addition to that you hide/disable the maximize box and that should do what you asked.
To disable the maximize box use this
this.MaximizeBox = false;
To hide the maximize box use this as well
this.MinimizeBox = false;
If Maximize and Minimize are set to false the buttons disappear.
Setting the MaximumSize equal to the Size (or some size) at least stops the windows from going full-screen. It still snaps to the top left corner but it's still a window at least and looks right - like it's Windows being stupid instead of your program.
You can prevent the windows snapping to the upper left corner by setting:
private void toolbox_Move(object sender, EventArgs e)
{
this.WindowState = FormWindowState.Normal;
}
in the move event of the form.
There is a property of the Form class named "MaximumBox" you have to set False in the properties window of your form... This actually will disable the form from being maximized by any way... Also if you want to control your form sizes you can work with such properties as "MinimumSize, MaximumSize" setting their values at your discretion or creating an event handler for the MaximumSizeChanged and MinimumSizeChanged events...
You can try to RegisterHotKey Win+Up if your window or application is activated and unregister this hot key if it is deactivated. You must catch the hotkey and return appropriate value to prevent further processing.
Look at WM_ACTIVATEAPP, WM_ACTIVATE and WM_NCACTIVATE. The first can be used if you want to disable the Win+UP for all your windows.
Set formborderstyle to fixedsingle
Maximizebox=false
Set the maximumsize to the size of winform's default size
Ex: if size(646,385) then set maximumsize(646,385)
One thing you can do is set the MaximumSize and MinimumSize in the same value but not 0,0.
It's easy easy! Apply the folling code to maintain window's state to normal when a resize event is triggered.
protected override void OnResize(EventArgs e) {
base.OnResize(e);
WindowState = FormWindowState.Normal;
}
How can I create in C# a Windows Form without taskbar (where the minimize and maximize button are placed).
Thanks.
That's called a Title bar.
Set the FormBorderStyle property to None.
Set Form.ControlBox to false and Form.Text to "". This is not the same as the TaskBar (which is the strip along the bottom (typically) in Windows)
To completely remove the title bar you'll need to set the FormBorderStyle to "None" as indicated by SLaks; however, you'll loose all of the other properties of the form's border. If you want anything more customized than that I'd recommend you override the form's OnPaint method but if you don't know what you're doing there your in for a world of hurt.
I have a form which is a mdicontainer and has a menu strip at the top. I add a child form to my mdi container and when I maximize the child it maximizes over the menustrip. I want to know how to limit the child to maximize below the menustrip. Any help would be appreciated.
Your child form is being maximized in the way that child forms are supposed to be maximized in MDI. It's not really covering the menu strip of the parent form - it's actually merging its own menu strip with that of the parent form.
To make the child form take up only the available child area in the MDI parent (and not merge its menu with the parent's menu), put something like this code in the child form's Resize event:
if (this.WindowState == FormWindowState.Maximized)
{
this.WindowState = FormWindowState.Normal;
this.Size = this.MdiParent.ClientSize;
this.Location = new Point(0, 0);
}
which will prevent the child window from being actually maximized.
I say "something like this code" because this snippet doesn't work exactly right. The ClientSize property of the parent form gives the overall size of the form, whereas you want to use the size of just the MDI client area. I don't know how to get that, and apparently it's not super-easy. See this question:
Size/Location of Winforms MDI Client Area
You could set the MaximumSize property so that it doesn't fill up the entire container.
I know this an old question, but I just ran into this on an old project I'm working on, so here's an answer for anyone seeing this. Setting the Dock to DockStyle.Fill will give you the behaviour you want.
Just be aware that the window will act/look strange if you try to reposition or resize the window while it has that DockStyle.
To accomplish this, I subscribed to the MDI client window's resize event and if the window had just been maximized, I set its DockStyle to Fill, set the FormBorderStlye to FixedDialog(to prevent resizing), and set the window state to normal to prevent the maximization from occurring.
To prevent the user from moving the window while it is in this "maximized state" I simply overrode the WndProc method and handled when the window was being moved(SC_MOVE) and placed a return to prevent the action from taking effect.