How to tell if a ChildWindow is the 'top most' window - c#

I'm working in Silverlight, but potentially a WPF solution would work as well.
My problem is very simple. I have lots of modal Child Windows that can be open, and in their generic menu is a home button. This button is supposed to close all of the child windows and return to the base screen. I have a few different types of 'generic child windows' that host lots of different UserControls, so by far the easiest way to implement this is to, when the window comes into focus, check if the global ReturnToHome bool is true, and if it is, just close it.
I've tried all of these
private void ChildWindow_GotFocus(object sender, RoutedEventArgs e)
{
if (CommonResources.ReturnToHome) DialogResult = false;
}
private void ChildWindow_MouseEnter(object sender, MouseEventArgs e)
{
if (CommonResources.ReturnToHome) DialogResult = false;
}
private void ChildWindow_IsEnabledChanged(object sender, DependencyPropertyChangedEventArgs e)
{
if (CommonResources.ReturnToHome) DialogResult = false;
}
The issue is, GotFocus doesn't fire until I actually click on the window. MouseEnter is a little better, but doesn't fire until I move the mouse. IsEnabledChanged never fires because the Child Window doesn't disable anything. Checking every child window when it closes to see if Home has been clicked isn't easy because of the sheer number of places where you can open child windows, and several of them are nested within User Controls where I couldn't even easily access DialogResult. Any idea how I could do this?
Also I should note that I want each of the windows to close one by one, from top down, because each window that closes does its own verification to see if it should warn the user before closing (giving the user the option to cancel closing)

TopMost is a bool property which is either set to true or false and as far as I'm aware, there is no public property like Z-Index that will tell you the order in which your Windows were set to TopMost. However, there is a simple solution... just maintain a static int variable that will register this order. Each time you add a new Window, set the number into its Tag property:
Window childWindow = new Window();
childWindow.Tag = currentWindowNumber++;
...
childWindow.ShowDialog();
Then, when you want to close them in order, you can just do something like this:
foreach (Window window in Application.Current.Windows.OfType<YourWindowType>()
.OrderBy(w => (int)w.Tag))
{
((AnimationWindow)window).CloseWindow();
}

Related

Making a new form window appear in c#

I'm trying to make a card game using Windows Application Forms.
The thing is that I don't know how to do the following- for example if i'd have 3 buttons- one of them named, for example, "Play", if i'd click on it, it would open the actual game, but in the same window, it would only make the buttons dissapear, and when i'd click back, it would open the window with buttons again. I don't really know how to explain my problem better, hopefully someone can tell me how to do that.
You don't have to hide / show the buttons. What you can do instead is to make a new form with the cards on it. That Form will pop up after you click the play button.
private void PlayButton_Click(object sender, EventArgs e)
{
// You other functionality goes here
GameForm GF = new GameForm();
GF.Show();
//Or - try this and see the difference
GF.ShowDialog();
}
Good Luck!
In addition to Leez's answer, in your situation, you should think about using container controls rather than handling the visible states of individual controls.
You could put related controls in a Panel, GroupBox or TabControl and set the visible properties of those containers instead.
you can use Visible property of button to do that as follows.
private void button1_Click(object sender, EventArgs e)
{
// You other functionality goes here
button1.Visible = false;
}

new window appears under main window

I am writing an application using WPF. I would like such an action: when I press down mouse button on a button, another window appears, when I release mouse button wherever, this window hides. That is my code so far:
XAML:
<Button Margin="0,0,0,0" Name="button_wykres" PreviewMouseUp="button_wykres_PreviewMouseUp" PreviewMouseDown="button_wykres_PreviewMouseDown">
C#:
private void button_wykres_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
omww.Top = this.Top+50;
omww.Left = this.Left +180;
omww.Show();
}
private void button_wykres_PreviewMouseUp(object sender, MouseButtonEventArgs e)
{
omww.Hide();
}
The problem is that new window (omww) appears under the main one, but I need it on top. When I tried other events, i.e. button.Click window is showed above the old one, as I want, and that confuses me. However, Click event doesn't meet my needs. I'd be grateful if anybody help me.
As I see it, you have two possible options for making the child Window appear on top of the parent Window. The first was mentioned by #Viv in a comment and that is to set the TopMost property on the child Window to true:
omww.Topmost = true;
The second option would be to set the Owner property of the child Window to the parent Window (if there is a direct relationship between them):
omww.Owner = this; // if called from the parent Window code behind

Bring parent window to front when child window selected from taskbar

The title pretty much says it all, but basically I have a main parent window, which occasionally opens child windows. Right now it's possible to select one of the child windows from the Windows 7 taskbar, and only that window will be brought to the front. What I'd like it to find a way to link the parent window to this command, so that any time a child window is selected the parent is automatically brought to the front as well.
I tried to use both the Focus() and Topmost = true commands from within the child windows 'GotFocusevent handler, but neither seemed to make a difference. I also tried theBringIntoView()` method, but again, no joy. Has anyone seen this before or know a way to implement this?
This is what I've tried so far. The logic in setting mainWindow first and then immediately setting the child window is that I do still want the child window to have focus, but I want mainWindow to be above any other programs running (ie-Excel, VS, etc).
private void Window_GotFocus(object sender, RoutedEventArgs e)
{
var mainWindow = Application.Current.Windows.Cast<Window>().FirstOrDefault(window => window is MainWindow) as MainWindow;
mainWindow.Topmost = true;
this.Topmost = true;
}
I think you're listening to the wrong event to be notified when your window is selected. Subscribe to the Activated event on your child window which should let you know when your window is selected in the taskbar. From there you can Activate() your MainWindow.
Additionally, I think if you set the parent window to be the Owner of the child window you'll get this behavior automatically.

MDI application issues

General description of application:
Main form as MDI Container. On application start, if there is no xml file for database configuration (it is checked in Main form) Main form i call another form as showdialog() to fill all database info to build connection string. Then i close form and open another for login, then i get back to Main form, which has Split Container (2 panels: 1-menu on top, 2-content from child forms).
I open forms with:
private void PlanButton_Click(object sender, EventArgs e)
{
plan.TopLevel = false;
KontenerMenu.Panel2.Controls.Add(plan);
plan.Dock = DockStyle.Fill;
plan.Show();
}
and close form with:
private void Plan_FormClosing(object sender, FormClosingEventArgs e)
{
e.Cancel = false;
this.Hide();
}
Problems i have with app:
1. When i hit Cancel button when i open ShowDialog() form for database app crashes. Cancel button is simply:
private void cancelButton_Click(object sender, EventArgs e)
{
Application.Exit();
}
2. I have problem with clicking button to open/close/open again child forms. When i hit 'X' and want o open, app crashes with exception that it cannot refer to non-existing object
3. I have several buttons when i hit one and then another one it is always below the first one and not on the top
4. For example my form is 200x200 and in right down corner i have button (so location let's say 190x190) and i hit maximize button. My button is still on 190x190 and i would like to have it on down right corner. I couldn't find any property for that. Is there any or i have to write some code for that.
I'm not sure I understood your questions. Please make them clear.
But as an answer to question #4, there's an anchor property that does what you want.
Instead of trying to exit the application from within the dialog form itself you should return a DialogResult value and test that in the main form. The cancel button on the dialog doesn't need any code, just set its DialogResult property to 'Cancel' and if you have an Ok button set its DialogResult to 'OK'.
DialogForm f = new DialogForm();
DialogResult r = f.ShowDialog();
if (r == DialogResult.Cancel)
{
Close();
}
I can immediately see a number of problems with you code, including:
If you're going to add controls dynamically using Controls.Add, you should make sure the controls you're adding are dynamically created using new(). I get a sense that you don't have a clear understanding of object lifetimes and the WindowForms control life cycle.
The Application.Exit method should be used only in unusual cases. It's purpose is to achieve exactly the result you're observing - to immediately "crash" the application. The easiest way to have a button close a modal dialog is the set the DialogResult property of the button.
Winforms has a very elegant system for placement of control on a variable sized window. In order to use this system, you should familiarize yourself with the Anchor and Dock properties that are available on all controls.
It looks like what you're doing is attempting to learn WinForms by trial and error. You can do this, but it will take much longer and be much more painful that getting a hold of a good tutorial, book, or perhaps even attending a class if you can manage it. That will allow you to take these issues one at a time and have a much more enjoyable learning experience.

Closing dialog form closes parent form

I have been dealing with strange problem. I am using KryptonForm in a project. I have a form (say form1) and I need to open another form on a button click from this form. Here is the code:
void btn_click(object sender, EventArgs e)
{
Visible = false;
ShowInTaskbar = false;
var f = new Form2();
f.ShowDialog();
Visible = true;
ShowInTaskbar = true;
}
The problem is that when the Form2 closes it closes the Form1 also. I have tried setting DialogResult = DialogResult.None from Form2 but of no avail. Please help me.
I am always using this technique and this thing has never happened.
Yes, this code is troublesome. It goes wrong when the user closes the dialog. Windows must then find another window to give the focus to. There isn't any left in your app, your main window is invisible. It then picks a window of another app. Odds are good, for example, that this will be a window inside Visual Studio. A big one. Your main form now disappears behind it.
You need to make sure that your main window is visible again before the dialog closes. You can do so by subscribing to the dialog's FormClosing event handler. For example:
private void button1_Click(object sender, EventArgs e) {
using (var dlg = new Form2()) {
dlg.StartPosition = FormStartPosition.Manual;
dlg.Location = this.Location;
dlg.FormClosing += (s, ea) => this.Show(); // <=== Here
this.Hide();
if (dlg.ShowDialog() == DialogResult.OK) {
// etc...
}
}
}
Bugged me for days!! Found this: https://bytes.com/topic/net/answers/769433-c-showdialog-inside-showdialog-closing-both-return
The result was being passed down and I dont know why. But if after the .ShowDialog() I just put this.DialogResult = DialogResult.None, it will fix it. This shouldnt happen in the first place, but this fixes it, so I am not too bothered.
You can also try changing the dialogResult on the button itself to "None" or deleting the this.Btn1.DialogResult... from the designer which worked for some people.
https://social.msdn.microsoft.com/Forums/vstudio/en-US/543093ad-1860-4428-bae1-b0d4f112e04b/showdialog-closes-parent?forum=csharpgeneral
I know this is an old post, but I ran into this, and in my case the accepted answer (at the time I am writing this) is not helpful at all. The answer by #blind Skwirl led me to the culprit.
After 20 years of .Net programming (since it was introduced), I never noticed that BUTTONS have a "dialogresult" property. I always just set the forms "cancelbutton" and "acceptbutton" properties. What I found in my case was that (because I was doing a lot of copy-pasting of buttons), I had a bunch of buttons (not forms) that themselves had their "dialogresult" property set to "cancel", which meant that I would click a button on a dialog that would open another dialog, the "ok" button on the dialog had its result set to "cancel", and the button on the parent form ALSO had its result set to "cancel", so the dialog would close (with a result of cancel) and then the PARENT form would close with a result of cancel, confusing the heck out of me... so...
Just make sure all of your buttons have their dialogresult property set to NONE (or whatever the actual proper setting is that you want).
Bottom line, if a BUTTON (not the form) has its dialogresult property set to anything other than NONE, the form will close with that result when it is clicked (after any click event code has completed).
I hope that helps someone out there.

Categories