Close The Entry Form - c#

Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form_Login());
After Login I Wanna Make The New Form Which Shown up After Login Is The Main Form And Close Current Form
I Tried
This.Hide();
and It Just Hide The Form But It 's Still Running On Task Manager
And I Tried
This.Close();
It Close The Whole Application Because The Form_Login Is The Main Form

Return a DialogResult upon closing your Form1. Use that value in Main() to determine if you should open Form2 or not. Something like this.
In Form1, perhaps in a button click handler:
this.DialogResult = DialogResult.OK;
this.Close();
In Program.cs:
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
var form1 = new Form1();
Application.Run(form1);
if (form1.DialogResult == DialogResult.OK) {
Application.Run(new Form2());
}
If your Form1 closes without setting its DialogResult to DialogResult.OK, your application will exit. If your Form1 closes and its DialogResult is set to DialogResult.OK then Form2 will open.
Edit: Using this technique for simple logons
Here's one approach.
Create an enum which describes the result of your logon screen:
public enum LogonStatus { NoLogon, UserA, UserB };
On your logon screen create a property to store the logon result:
public LoggedOnUser User { get; private set; }
In the logon form assign a value to the logon operation and close the form:
LogonResult = LogonStatus.UserA; // UserA logged in, for example.
this.Close();
In Main run the Logon form, examine the form's LogonResult proeprty and process the result:
static void Main() {
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
var loginForm = new Form_Login();
Application.Run(loginForm);
if (loginForm.LogonResult == LogonStatus.NoLogon) {
// Do something because there was no logon, or do nothing here and let your app exit.
} else {
// Launch your application form, passing in the logged on user.
Application.Run(new AppForm(loginForm.LogonResult));
}
}
This example shows passing a LogonStatus to your AppForm's constructor to allow you to tailor it the user logged on.
With the above said though, you should know this isn't really the best way to do this. A more flexible and robust solution would involve taking advantage of Windows directory services.

Related

Parent form and Child form in Loop C#

I am trying to create a login for my tool and have sorted that but when they press login(Child form) the program loops and asks the user to login again. I notice when I press the 'Exit' button it then loads the Parent form fine but I dont want that. I want my users to press login and then go right to the Parent form.
To open the child form before the parent form inside of Form1_Load I have this:
Login Log = new Login();
Log.ShowDialog();
Inside the 'Login' button on the child form I have this:
this.Hide();
Form1 Main = new Form1();
Main.Show();
The best way to handle this would be to handle the login BEFORE your form starts (e.g. in the Program.cs but after the application initialization). Insert a variable into your login form (E.g. a bool that shows if they have passed your authorization test) and check after the form has closed to see what the status of authorization is. If the flag is set to false then you can do a return or Application.Exit() and the user will never see the main form.
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
SomeLoginForm Login = new SomeLoginForm();
Login.ShowDialog();
if (!Login.HasPassedAuthorization)
{
MessageBox.Show("Sorry you failed to pass the test! I'm kicking you out now!");
Application.Exit(); // or do a "return;"
}
Application.Run(new Form1());
}
}
static class SomeLoginForm()
{
internal bool HasPassedAuthorization;
}

C# app stays in task manager after closing [duplicate]

why The process still on Windows Task list manager after close programme ?
i use login Form.cs
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Login());
}
after the user succesuly login, i redirect to another Masterpage
this.Hide();
Main_Usr oMainUsr = new Main_Usr();
oMainUsr.Visible = true;
my pseudo master page like this:
public Main_Usr()
{
InitializeComponent();
this.IsMdiContainer = true;
}
when i close the masterpage, The process still on Windows Task list manager.
But when i close the login page, it kill the process on Windows Task list manager.
is that mean because i just hide le login page ?
must i close all window to realy quit/kill the process ?
Thanks you in advance,
Stev
In winforms process will be killed, when main application form is closed. Main application form is one specified in Application.Run call. In your case it is Login form:
Application.Run(new Login());
To close form you should call Close method. When you call Hide or set Visibility to false, form stays in memory. It just becomes hidden from user.
So, to achieve desired functionality you should change main application form to Main_Usr:
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Main_Usr()); // change main form
}
Then subscribe to Load event of Main_User form. And in the event handler do following:
private void Main_User_Load(object sender, EventArgs e)
{
using (var loginForm = new Login())
{
Hide(); // hide main form
if (loginForm.ShowDialog() != System.Windows.Forms.DialogResult.OK)
{
Close(); // close main form and kill process
return;
}
Show(); // show main form if user logged in successfully
}
}
UPDATE: You can do this all in Main method, like this way
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
using(var loginForm = new Login())
if (loginForm.ShowDialog() != System.Windows.Forms.DialogResult.OK)
return;
Application.Run(new Main_Usr()); // change main form
}
but usually I don't hide main form and show it below login form. So, in this case you should use Load event handler. It's up to you.
BTW there is no masterpages and pages in winforms. This all is for ASP.NET. Here you have forms :)
Also consider naming like LoginForm, MainForm etc.
This is because the application message loop is associated with your Login form (Application.Run(new Login()) does this), so you need to close the form which started the application to end the process.
Alternatively, you could just call LoginForm.Show(), before Application.Run, store credentials somewhere and then call Application.Run(new Main_Usr)
Because Login is the last form of the application to close, you load Main_User only after that - even if Login is hidden it's still actually there. Windows Forms applications are by default configured to exit when the last form closes.
this.Hide()
doesn`t kill the window.
So it remains hidden and process remains in memory.
this.Close() closes the window and removes its object from memory.
It is better to do something like this:
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
var l = new Login();
l.ShowDialog();
if(l.Passed)
Application.Run(new Login());
}
And implement Passed property inside login window.
By the way, do you have any multithreading inside?
It is another source of errors of this type.
i found it, i just use the dizlog.
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Login oLogin = new Login();
oLogin.ShowDialog();
Application.Run(new Main_Usr());
}
i follow code the #lazyberezovsky and add this on my Login.cs
private void simpleButton_Valider_Click(object sender, EventArgs e)
{
.....
DialogResult = DialogResult.OK;
return;
.....
}

C# - Manually closing a forms application

I would like to know the appropriate way to completely terminate a Forms application. I open the form in the standard way using the code:
namespace History
{
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new DisplayHistory());
}
}
}
...and here is a barebones version of the DisplayHistory function with the various things I've tried to terminate the application commented out. The first two cause an unhandled exception stating that I cannot access a disposed object. The third one has no effect at all, and the fourth one won't compile at all because I get an error saying that "Application does not contain a definition for Current":
public DisplayHistory()
{
InitializeComponent();
// this.Close();
// Close();
// Application.Exit();
// Application.Current.Shutdown();
}
Instead of trying to close it in constructor, it's better not to open the form.
But if you really want to close the form in constructor based on some condition, you can subscribe for Load event before InitializeComponent in code and close the form using this.Close()
:
public Form1()
{
if (some criteria)
{
this.Load += (sender, e) => { this.Close(); };
}
return;
InitializeComponent();
}
To close an application normally, you can call below codes anywhere except constructor of form:
this.Close();
Application.Exit();
To force the application to stop, or close it abnormally, you can call Environment.Exit anywhere including the form constructor:
Environment.Exit(1);

How can I run a second application from another one (C#)

Can you tell me how can I close a first application and immediately run a second application?
First Step: (Login validation)
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Login());
}
Second Step: (Run main program)
If the user succesfully logins to program, I need to close the login application and run the new application named "Main".
The check for the login is the following:
if (access.access == true)
{
Application.Run(new Main());
Close();
}
else
MessageBox.Show("Přihlašovací jméno nebo heslo neni správné");
Debug Error:
Create a second message loop on a single thread is an invalid operation. Instead, use the Application.RunDialog or form.ShowDialog.
I think that the best answer for my problem is to use ShowDialog and DialogResult, but I don't know how can I use them for my settings.
You could run a modal dialog asking for credentials before entering the main loop
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
bool validated = false;
using(frmLogin fLogin = new frmLogin())
{
if(fLogin.ShowDialog() == DialogResult.OK)
validated = true;
}
if(validated)
Application.Run(new Main());
else
MessageBox.Show("Bye");
}
Have a look at Creating a Windows Forms Application With Login
Here the difference is that the Main still runs frmMain
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new frmMain());
}
The main form handles the frmLogin
public partial class frmMain : Form
{
frmLogin _login = new frmLogin();
public frmMain()
{
InitializeComponent();
_login.ShowDialog();
if (_login.Authenticated)
{
MessageBox.Show("You have logged in successfully " + _login.Username);
}
else
{
MessageBox.Show("You failed to login or register - bye bye","Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
Application.Exit();
}
}
}

Hide or Dispose Form

I have a MainForm, from the MainForm i call the ConfirmationForm,
using (var f = new ConfirmationForm())
f.ShowDialog();
Then in the ConfirmationForm, i want to show the another UsersListForm
if (ConfirmSuccess)
{
this.Hide; //or this.Close
using (var f = new UsersListForm())
f.ShowDialog();
}
Now, when the ConfirmSuccess is equal to true the MainForm will Hide or Close too. How to prevent that the MainForm will not to Hide or Close? any idea? Thanks in advance.
UPDATE: My problem is solve. I call first the UsersListForm and from the load event of UsersListForm I call the ConfirmationForm then I use DialogResult == System.Windows.Forms.DialogResult.OK and everythings is fine now :)
If your intention is to request user confirmation before opening the MainForm, the best way to do this would include you call and confirmation form after creating it and call MainForm.
If your intention is to seek confirmation at the beginning of the application, place the call ConfirmationForm within the Program class before the Application.Run (new MainForm ());
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
ConfirmationForm confForm = new ConfirmationForm();
confForm.ShowDialog();
Application.Run(new MainForm());
}
}
But if the intention is to request verification within the application in a separate call point, you should call the ConfirmationForm with ShowDialog and after that call the desired form.
But if your intention really is to verify the request with the open form, and hiding it, you can use the DialogResult property of ConfirmationForm to return the success or failure of the validation by comparing (ConfirmationForm.ShowDialog () == DialogResult.OK). See this example

Categories