I have a WPF program that I want to be minimized most of the time to the system tray..
However when it is minimized to the system tray, the child Windows that open when a message is received does not open in normal window state but opens minimized in the system tray..
I have tried
msgWindow.WindowState = System.Windows.WindowState.Normal;
msgWindow.Show();
But this does not work.. I can fix it by doing
this.WindowState = System.Windows.WindowState.Normal;
msgWindow.Show();
However I do not want the main window to be opened every time a message pops up..
Please try this it will help you.
Window2 win2 = new Window2();//here i established the child window named as Window2
win2.Show();
The above code diplay both window(main and child) concurrently.
Window2 win2 = new Window2();//here i established the child window named as Window2
win2.Show();
this.Close();
The above code diplay child window only.
Related
When I click on my Winforms app then its forms are opened in separate section in Taskbar with default C# icon. How can I solve this issue?
Update:
At first I open my login form as bellow:
Application.Run(new frmLogin());
Then I open my main form from the login form as bellow:
this.Hide();
frmMain frmMain = new frmMain();
frmMain.Show();
This sounds like a system problem. Here are 3 solutions:
Restart your computer.
Restart explorer.exe or File Explorer. How to do it.
Unpin then re-pin your program on the taskbar.
first solution: you can set this property of the form
and another solution :
Did you try parent form and child form?
Set Parent of the forms, So they will be open as child of your main form
For Example(In the main form when You want to open another form) :
FrmChild frmChild= new FrmChild ();
frmChild.Parent = this;
FrmChild.Show();
I'm working on a small project where I have a login window and when the user is autheticated it will open a new window, that works but after the user logged in I want to close the Login window how ever, the main window doesn't close.
MainWindow mainWindow = new MainWindow();
mainWindow.Close();
Loader loader = new Loader();
loader.Show();
You are initializing a new instance of a Window and close it immediateley afterwards.
It looks like your mainWindowis not the login window you want to close. If you want to close the current Main Window of you application, you can use:
App.Current.MainWindow.Close();
Make sure you also set a new Main Window. I assume you wanted to do something similar to this, also assuming "loader" is your new Window:
private void Login(){
App.Current.MainWindow.Close();
Loader loader = new Loader();
App.Current.MainWindow = loader;
}
I have a main Window.Then if you touch settings button a new modal Window is shown with some options..Then you click an option and a new Window (lets call it "Settings") is shown.Both windows have as owner the main Window. Our new window(Settings) may open new window having itself as owner for example if you want to add a new staff member.And here is the problem. When you close the new window and then close Settings window the main Window is minimized... However this doesnt happen if Settings window dont open any other window...
this is how i show the forms
UserForm f = new UserForm();
f.Owner = this;
f.Show();
Use MainWindow.Activate() on close of Child Window.
it should work
I ran into the same problem and found an apt solution here.
Set the window owner to null before the Settings window closes.
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
Owner = null;
}
Can't you catch when the window is closed (OnFormClose I think) and then unminimise your main window?
I'm using VS 2010, WPF 4, C#
I have a parent window and some child windows. When I minimize the parent window all child windows go minimize too. And when I maximize the parent window all child windows go maximize too but I want to restore the child windows in their previous position. How I can do this?
// This code snippet is in parent window
ChildWindow1 objChildWindow1 = new ChildWindow1();
-------
-------
objChildWindow1.Owner = this;
objChildWindow2.Owner = this;
objChildWindow3.Owner = this;
private void buttonShow_Click(object sender, RoutedEventArgs e)
{
objChildWindow1.Show();
objChildWindow2.Show();
objChildWindow3.Show();
}
1) Now consider I've minimize ChildWindow1 and ChildWindow2 only. ChildWindow3 is still maximized.
2) Now if I minimized the parent window all three ChildWindow will be minimized and it's ok.
3) Now if I maximize the parent window all three ChildWindow is also maximized but I want ChildWindow1 and ChildWindow2 will be minimized and ChildWindow3 will be maximized.
How can I do this? Any other Idea?
I'm creating a wpf application in c#, I know to close/open a window you have to use the .Close() and .Show() methods but for some reason the home screen, the first window that appears when I launch the application, won't close.
Home window1 = new Home();
window1.Close();
Name window2 = new Name();
window2.Show();
Window2 appears, but window1 won't close. What's the problem.
Where is your code for showing window1? If you show your home window somewhere else in your code, you need to use that reference in order to close it. Making a new Home object and calling its Close method will not close a window shown using another Home object.
Presumably because if you close the window you'll close the application.
If you just want to hide the main window use the window.Hide() method.
This from the help on Window.Close:
A Window can be closed using one of
several, well-known, system-provided
mechanisms located in its title bar,
including:
ALT+F4.
System menu | Close.
Close button.
A Window can also be closed using one
of several well-known mechanisms
within the client area that are
provided by developers, including:
File | Exit on a main window.
File | Close or a Close button on a
child window.
UPDATE
Tormod Fjeldskår has a good point in his answer. I assumed that the code was given as an example rather than being what was actually being used.
This is a bug in WPF. Window.Close will fail silently if the SourceInitialized event has not yet occurred. Subsequent calls to Window.Close will also fail.
https://connect.microsoft.com/WPF/feedback/ViewFeedback.aspx?FeedbackID=299100
For a workaround, add this to your Window:
protected override void OnSourceInitialized(EventArgs e)
{
base.OnSourceInitialized(e);
// check if we've already been closed
if (m_bClosed)
{
// close the window now
Close();
}
}
protected override void OnClosing(CancelEventArgs e)
{
base.OnClosing(e);
// make sure close wasn't cancelled
if (!e.Cancel)
{
// mark window as closed
m_bClosed = true;
// if our source isn't initialized yet, Close won't actually work,
// so we cancel this close and rely on SourceInitialized to close
// the window
if (new WindowInteropHelper(this).Handle == IntPtr.Zero)
e.Cancel = true;
}
}
bool m_bClosed;
Or you could have Window2 be the main window (you can change this in app.xaml in the StartUpUri property) and either have Window2 show and close Window1 or not show Window1 at all.
<Application x:Class="Invitrogen.TheGadget.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="Window2.xaml">
</Application>