Hide a window until it's needed again - c#

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)

Related

how to close a WPF Dialog Window when user try to touch past window screen

Please look at image,
It has two windows, red one opens after green one.
How could I close red windows when user touch green screen?
Also I have using
protected override void OnDeactivated(EventArgs e)
{
base.OnDeactivated(e);
Close();
}
But this works only when user open another application
here is my green window code, that opens red window
MainWindow_OnLoaded(object sender, RoutedEventArgs e)
{
Window1 w=new Window1();
w.ShowDialog();
}
I cannot give you a direct answer since I do not do WPF programming, but an approach I use on IOS is to have one big form, the outer part is transparent and
it actually contains one huge button, which is also transparent, only the Window1 is visible. On the button press event close window1.
But I am pretty sure you should have a way to detect click events on the green part and then close the window1. Maybe add some event listener to some component you place on the green part.
See this answer:
how to close a WPF Dialog Window when the user clicks outside it
Here is a answer I copied:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
protected override void OnDeactivated(EventArgs e)
{
base.OnDeactivated(e);
Close();
}
}

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.

How do I open my closed form through the notifyicon in the taskbar?

Currently I am developing a windows form application in c# that has several forms.
I am running a background form that operates the notifyicon property that allows the icon to appear in the taskbar.
When I launch the program, it will launch a loginForm, after which logging in it will go into a mainForm. After closing the mainForm, the application does not close yet, which in this case works like Windows Live Messenger.
How do I make my program in a way that after I the mainForm, through double clicking it will bring the form back up? (Like how MSN works.)
Or is it a better solution for me to close the whole application when I press the X button in the title bar. Which brings up another problem for me as I cant seem to exit the application when I close other forms other than the main form.
Probably you have NotifyIcon on your main form. Subscribe on the DoubleClick event of this control and change state of your main form in the handler:
private void notifyIcon1_DoubleClick(object sender, EventArgs e)
{
this.Show();
this.Visible = true;
this.WindowState = FormWindowState.Normal;
}
Just set the Visible property of the form to true/false. Or you could call Show()/Hide().

C# WPF child window (about window)

I have an application on C#/WPF that I`m developing ... I have a btn named About which should open a new window that contains details about the application or whatever I will put in it.
When I click the btn, a new window (about) opens, when I click again, while the new window (about) is opened another one is opened, how can I prevent this from happening, I also want the application to be disabled when the About window is opened and to be enabled when the About window is closed, Like most of the applications when about is clicked.
You should use the ShowDialog Method: http://msdn.microsoft.com/en-us/library/system.windows.window.showdialog.aspx
Code Sample:
// Instantiate window
AboutWindow aboutWindow = new AboutWindow();
// Show window modally
// NOTE: Returns only when window is closed
Nullable<bool> dialogResult = aboutWindow.ShowDialog();
Just use ShowDialog() method instead of Show()
AboutWindow aboutWindow = new AboutWindow();
aboutWindow.ShowDialog();

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