I try to show the authentication window, then open the main window,
but when you close the authorization window, application is stopped
private void App_OnStartup(object sender, StartupEventArgs e)
{
new LoginWindow().ShowDialog();
new MainWindow().Show();
// Then application stopped
}
BUT!
If the display window authentication by using method Show(), the application does not close after closing the authorization window
private void App_OnStartup(object sender, StartupEventArgs e)
{
new LoginWindow().Show();
new MainWindow().Show();
// Then application running
}
Why is this behavior???
Thanks to Eran Otzap!
Application.Current.ShutdownMode = ShutdownMode.OnExplicitShutdown;
Is working!
By default, when the main windows of the application is closed, then the application is closed.
According to the documentation, "Application.MainWindow is automatically set with a reference to the first Window object to be instantiated in the AppDomain."
To get around this, you can try to create first a MainWindow object (without calling Show()),
then create and show the login dialog, and then show the main window.
Related
This question already has answers here:
How do I prevent the app from terminating when I close the startup form?
(4 answers)
Closed 2 years ago.
private void Student_btn(object sender, EventArgs e)
{
SignUp signup = new SignUp();
signup.Show();
this.Hide();
}
First-time works successfully, but when I run the second time it does not work
I get this error:
Unable to copy file "obj\Debug\FinalProject.exe" to "bin\Debug\FinalProject.exe". The process cannot access the file 'bin\Debug\FinalProject.exe' because it is being used by another process.
The reason why you have this issue is because your previous version is still running in the background. And because it's still running Visual Studio can't remove it's .exe in order to replace it with the new version.
Why is you previous version still running?
You've provided this code:
private void Student_btn(object sender, EventArgs e)
{
SignUp signup = new SignUp();
signup.Show();
this.Hide();
}
Let's assume that this method is part of your MainWindow. When the user clicks the button, the MainWindow opens a new window signup.Show()and hides itself this.Hide().
But that means, that your MainWindow is still running, you just can't see it and it will still be running after you close the new window.
To resolve the problem you have to close the MainWindow properly. And you can do it like that:
private void Student_btn(object sender, EventArgs e)
{
SignUp signup = new SignUp();
this.Hide(); //Your MainWindow hides itself and gets invisible
signup.ShowDialog(); //You code will "stop" here until the new window is closed
this.Close() //You MainWindow closes itself and your program will stop
}
If you want to resume to your MainWindow when the new window closes. Use this.Show()instead of this.Close()
I have been searching everywhere for the answer to my problem but i cant seem to locate anything.
I am trying to open 2 different windows, that i have already created, after a button press. On the button press, i go to open the new window, but it just opens a blank window, not the window i want.
This is my function that i am trying to open my new window with, and it is inside my MainWindow, and i want to open my window i have created in Jobs (see image below).
void Jobs_Clicked(object sender, RoutedEventArgs e)
{
Window Jobs_Window = new Window();
App.Current.Jobs = Jobs_Window;
Jobs_Window.Show();
this.Close();
}
Solution Explorer
You are creating an instance of the standard Window class rather than your custom Window that you have made.
If you have added a Window, called JobsWindow for example, then create an instance of that rather than just the .NET built-in Window:
void Jobs_Clicked(object sender, RoutedEventArgs e)
{
Window Jobs_Window = new JobsWindow();
App.Current.Jobs = Jobs_Window;
Jobs_Window.Show();
this.Close();
}
I'm implementing a window form in C#. I want to close current window and open a new window. (Just like File->New function in applications)
This is my code
private void newToolStripMenuItem_Click(object sender, EventArgs e)
{
Image_Editor IE = new Image_Editor(); //Image_Editor is the name of window
IE.Show();
this.Close();
}
Images of what I want to achieve:
(source: infosight.com)
I want to implement the "new" function as shown in the above link. When executing given code, new window just appeared and both windows close at the same time.
What should I do to solve this?
if you mean that you want to close this windows and open a windows like your current windows use this
Myform frm; //thats name of the class of your form like Form1
frm = new Myform();
frm.Show(); // show the secend one
this.Close(); // and close the first one
I want to hide my form while keeping my application running in background.
I've used notifyIcon and it remains always visible.
I've used "this.Hide();" to hide my form but unfortunately my application gets close (no exception).
I am also using threading and this form is on second thread.
Please tell me how can I solve it.
I am also using threading and this form is on second thread.
My crystal ball says that you've used ShowDialog() to show the form. Yes, calling Hide() on a modal dialog will close it. Necessarily so, a modal dialog normally disables all of the windows in the application. If you hide it then there's no way for the user to get back to the program, there are no windows left to activate. That this form runs on another thread otherwise doesn't factor into the behavior.
You'll need to call Application.Run(new SomeForm()) to avoid this. Now it isn't modal and you can hide it without trouble. But really, do avoid showing forms on non-UI threads. There's no reason for it, your main thread is already quite capable.
add the following event handlers for form resize and notify icon click event
private void Form_Resize(object sender, EventArgs e)
{
if (WindowState == FormWindowState.Minimized)
{
this.Hide();
}
}
private void notifyIcon_Click(object sender, EventArgs e)
{
this.Show();
this.WindowState = FormWindowState.Normal;
}
but this is not close you application
Okay firstly I just started C# so I'm not exactly the most skilled programmer out there. Okay so here's my problem that may seem stupid to you guys ;)
I have a simple enough app that a friend asked me to do. So far I have managed with a bit of Google but I'm stuck with this. The app runs fine and minimizes to the system tray and maximizes from the system tray which is good. However, when I open a second form from that application it creates another icon in the system tray and starts duplicating every time I open another form. So eventually I have lots of icons and all of them are seperate instances of the main form. System Tray events
private void notifyIcon_systemTray_MouseDoubleClick(object sender, MouseEventArgs e)
{
if (FormWindowState.Minimized == WindowState)
{
Show();
WindowState = FormWindowState.Normal;
}
}
private void CronNecessityForm_Resize(object sender, EventArgs e)
{
notifyIcon_systemTray.Visible = true;
if (FormWindowState.Minimized == WindowState)
Hide();
}
private void restoreContextMenuItem_Click(object sender, EventArgs e)
{
Show();
WindowState = FormWindowState.Normal;
}
To open the Form:
private void preferencesToolStripMenuItem_Click(object sender, EventArgs e)
{
CronPreferences.formPreferences CronPreferences = new CronPreferences.formPreferences();
CronPreferences.Show();
}
Close it:
private void button2_Click(object sender, EventArgs e)
{
this.Hide();
}
How can I have all Forms map to the same icon in the System Tray?
You will need a single global tray icon that they all access. Do this by using a static variable that stays the same throughout different instances of the class.
Then, if you want to:
Open one form: keep a reference to the latest form in a variable and open it.
Open all minimised forms: iterate through each form and open them again.
If I got it right, you want to keep only a single instance of your application running. In that case, your title is a bit misleading since your problem has nothing to do with tray icons or multiple forms.
Code Project: A Single Instance Application which Minimizes to the System Tray when Closed
On the other hand, if you really have a main form in your app, which opens the second form (which creates a tray icon), in that case you simply need to make sure your second form is instantiated only once:
public class MainForm
{
private SecondForm _secondForm;
public void OpenSecondForm()
{
// create it only once
if (_secondForm == null)
_secondForm = new SecondForm();
// otherwise just show it
_secondForm.Show();
}
}