In WinForms I need to ensure that newly opened Form is on top of the stack and does not hide behind Form that instantiated it. I do not want to use TopMost property as it forces new form to stay on top of all Forms opened by all running processes. I just need to have my new Form to open on top of all forms in current application.
This method will check the current application OpenForms collection for a window with a title you pass in. It will activate the window if it is found, or have the window manager launch one.
Not sure how you plan on Z-Ordering your windows, but you could use the loop in the code and do something with each openform.
public void FindWindowOrMake(string theTitle)
{
var found = false;
foreach (var openForm in Application.OpenForms.Cast<Form>()
.Where(openForm => openForm.Text.Equals(theTitle)))
{
found = true;
openForm.Activate();
break;
}
if (found) return; // target found and activated
// create new instance
WinMgr.Create(theTitle);
}
Related
I have two forms in my project (a simple game for kids). The first one is the start menu and the second one is the game. Now, when I click on New Game, I want the second form to open within the first one. I did that by using the following code:
private Form activeForm = null;
private void openChildForm(Form childForm1)
{
if (activeForm!=null)
{
activeForm.Close();
}
activeForm = childForm1;
childForm1.TopLevel = false;
childForm1.FormBorderStyle = FormBorderStyle.None;
childForm1.Dock = DockStyle.Fill;
panel1.Controls.Add(childForm1);
panel1.Tag = childForm1;
childForm1.BringToFront();
childForm1.Show();
}
Now the second form opens within the first one, but it doesn't work properly. In my second form I have a picturebox, which is supposed to move when the user presses on one of the arrow keys. But it won't move.
Any suggestions what should I do?
P.S.
I'm a beginner and this is a school project. My teacher showed us only one way of opening a form:
Form2 objForm2 = new Form2();
objForm2.Show();
but since it is a very ugly method of getting the job done, I wanted to do it better.
I'm using Visual Studio 2019
I would suggest you to use a so called UserControl it's basically that what the name says: It's a custom windows forms control, witch has it's own child controls -> Just Like a form.
Our Application is displaying an "update-hint", if a new version is available.
This Update-hint should be "topmost" within the application, but, if the application is minimized, or send to the background, the update-hint should vanish as well..
Just using
this.TopMost = true;
will overlay "ANY" application that is currently running...
Is there a way to just "overlay" windows generated by the current application?
Desired:
application shows update-hint on top of every window, while application is in the foreground. Switching to another application will also send the update-hint to the background.
Desired: Update-Hint overlays ANY window of the current application:
Not desired: Update-Hint overlays FOREIGN applications as well:
Despite the name of the property, TopMost is actually your enemy here. To make a "floating form" stay above a main form, without obscuring other applications when they get focus, try it this way:
FormX f = new FormX();
f.Show(this);
"this" in this example, is the main form instance. It means the form you created is now owned by the main form, and will make it float above it. You get the added benefit of, when minimizing the main form, the floating forms will disappear, too.
I figured out a way to solve it.
Setting the owner of the UpdateHint is required, but in order to keep it on top of every application window, one has to change the owner, if a new Window is either shown, or activated.
Within our application, every Form is inheriting InterceptorForm, so all I had to do was to modify the InterceptorForm accordingly:
Change the Owner to this, except if there is no dialog, or this is the dialog itself:
public class InterceptorForm : Form
{
protected override void OnLoad(EventArgs e)
{
...
if (this.GetType() != typeof(UpdateHint) && MainWindow.updateHint != null)
{
Log.t("Setting owner on update hint during onload: " + this.GetType());
MainWindow.updateHint.Owner = this;
}
base.OnLoad(e);
}
and
protected override void OnActivated(EventArgs e)
{
if (this.GetType() != typeof(UpdateHint) && MainWindow.updateHint != null)
{
Log.t("Setting owner on update hint: " + this.GetType());
MainWindow.updateHint.Owner = this;
}
base.OnActivated(e);
}
}
The UpdateHint now stays on top of every window belonging to our application, but can be overlayed by any other application.
Does anyone know about Multiple Document Interfaces in C#? I'm having trouble with Form.Show(this) to keep one of the child forms perpetually on top of the rest. If the form already has an owner (set to MdiParent), then the program throws an exception when calling the show method.
Here's a switch case that's supposed to create and display a child form, but errors out due to the problem stated above. Are there any alternatives to this to keep the form on top of all the others?
case Page.Call:
if (call == null)
{
call = new CallForm();
call.MdiParent = this;
call.Dock = DockStyle.Fill;
}
call.Show(this);
break;
I have many popups in a custom GUI application. These popups are window objects, not popup objects. The popups do not show up in a screenshot when using the Print Screen button on the keyboard. Instead, the disabled mainwindow below is all that shows in the screenshot. The popup never flickers or disappears, it just doesn't show in the screenshot.
WindowInstance.IsEnabled = true;
WindowInstance.Refresh();
DisplayPopUpWindow(WindowInstance);
The code in DisplayPopupWindow:
private void DisplayPopUpWindow(Window theWindow)
{
if (theWindow != null)
{
if (theWindow is MessagePopup)
{
// Only show one popup at a time (queue is handled elsewhere)
RemovePopUpsCommand.Execute(true, null);
}
ActiveWindow.IsEnabled = false; // main screen disabled
foreach (KeyValuePair<Window, int> aPopup in popUpWindows)
{
if ((aPopup.Key.IsVisible) && (aPopup.Key.Opacity == 1) && (aPopup.Key != theWindow))
{
// if window is a lower priority then disable it
if (aPopup.Value > displayPriority)
aPopup.Key.IsEnabled = false;
}
}
theWindow.Show();
theWindow.Opacity = 1;
}
}
Is there some property in the XAML that affects whether the window is visible for screenshots? This is a large issue as this also affects some remoting software we use in that popups do not display on the shared screen. Also affects our combobox implementation.
The "popups" are actually their own standalone windows. Some have instances created once during application startup and simply shown/hidden when needed, however, most are created on demand to be displayed. This problem affects both types.
The remoting software used is axeda access remote.
If I remember correctly I had the same problem and I think it was related to setting the popup windows parent to the main window that fixed it, I'd have to look at my code at home to confirm.
So make sure this is correctly set.
EDIT:
Try using this code when you create the Window object:
MainWindowVariable.Owner = Window.GetWindow(this)
You can read more here:
http://msdn.microsoft.com/en-us/library/system.windows.window.owner(v=vs.110).aspx
I have three forms in C#, now when I want to show form2 I hide the main one and show the form and then when done working, hide the second form and show the main form again - I am doing this with the simple hide and show functions in winforms. Now the problem is every called form is placed on a different location on the screen, while I want all of them to stay on the same place. How to do it?
Try setting the owner of the form when you call .Show()
You can also set the start position before you call show with .StartPosition = FormStartPosition.CenterParent
Or set the form.Location property after you call show
See here and here for more details
You no doubt have a bug in your code, you are creating a new instance of the form instead of calling Show() again on the hidden form object. That's a bad kind of bug, it will make your program consume a lot of machine resources, ultimately it will crash when Windows refuses to allow your process to create more windows.
To make your scheme work, you have to write code that distinguishes between a closed form and a hidden one. Best way to do that is to explicitly keep track of its lifetime with the FormClosed event. Like this:
private Form2 form2Instance;
private void button1_Click(object sender, EventArgs e) {
if (form2Instance == null) {
// Doesn't exist yet, so create and show it
form2Instance = new Form2();
form2Instance.FormClosed += delegate { form2Instance = null; };
form2Instance.Show();
}
else {
// Already exists, unhide, restore and activate it
form2Instance.WindowState = FormWindowState.Normal;
form2Instance.Visible = true;
form2Instance.BringToFront();
}
}