enable my parent main window(parent window) after closing my chlid window - c#

I want enable my parent window on closing of child window. When i use enable property it doesn't work for me. form parent to child it works, my parent window disabled.

Try this:
In parent Window:
childWindow.Closed += ChildWindowClosed;
...
private void ChildWindowClosed(object sender, EventArgs e)
{
IsEnabled = true;
}
Now, when the child Window closes, the parent Window.IsEnabled property will be set to true.
However, you probably shouldn't do this anyway... there could be negative consequences to disabling the main Window. If you just want to temporarily 'lock' the parent Window while the child Window is open, then all you have to do is to open the child Window as a dialog, like this instead:
childWindow.ShowDialog();

Related

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

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();
}

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

Hide a window until it's needed again

I'm working on this WPF project. The main window has 8 buttons which all open another window. What I'd like to do, is when the user clicks one of the buttons, the main window is hidden(not closed), and the secondary window is open. Now, when the secondary window is closed, I want to unhide my main window. Here's the code I've got.
public WndwProjectSetup(Window mainWindow)
{
InitializeComponent();
_mainWindow = mainWindow;
_mainWindow.Visibility = Visibility.Hidden; // hides main window
}
private void WindowClosing(object sender, CancelEventArgs e)
{
_mainWindow.Visibility = Visibility.Visible; // unhides main window
Close(); // close Project Setup window
}
This seems simple and straight forward to me. Yet, I get this error:
Cannot set Visibility to Visible or call Show, ShowDialog, Close, or
WindowInteropHelper.EnsureHandle while a Window is closing.
My question is, why is this not acceptable? What do I need to look into to figure out how to do this?
You don't need to call Close, it's already closing. (If anything you can cancel it by setting e.Cancel to true)

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.

child form minimize goes to task bar

I have an MDI form which displays a treeView control, when the user clicks on the tree node child form, it opens:
private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
frmPartMaster frm = new frmPartMaster();
frm.Show();
}
Here frm is displaying the backside of the tree control but I want it to show form in front of parent, not back. So I changed the code to:
private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
frmPartMaster frm = new frmPartMaster();
frm.Show(this);
}
Here the child form is displayed in front of the tree, but when I minimize the child form it goes to task bar.How do I get it to go to the left corner of of parent form?
You have to set the parent form's IsMdiContainer property to true. Then while opening the child form add the following code:
frm.MdiParent = this;
Are you using MDI? Then it won't come to Task bar. You can set the ShowInTaskbar option to false, which will not display for in Taskbar.
Real MDI child forms do not minimize to the taskbar. Therefore I can only conclude that you are not using an MDI child form and that your solution is to start doing so.

Categories