WinForms Closing problem

  • Thread starter Thread starter Tarun Mangla
  • Start date Start date
T

Tarun Mangla

Hi

I am facing a problem. I've made an application in which two forms are
there. One if Login form and another is main form. The problem is that,
after getting authenticated from the database the main form is opened. But
the login form is not getting closed. I am using the following code:

It is in the load of the main form.
this.ParentForm.Close();

Giving some error.



Regards

Tarun Mangla
 
Hi,
One option is to hide the login form (after successful logon) and show the
main form using ShowDialog() method (assuming logon form is the startup
form).
Alternative is to show the logon and main forms indepenantly (from Main()).
That is, show the logon form using ShowDialog and based on the dialog result
(eg: DialogResult.OK for successful logon), close the logon form and show
the main form using Application.Run().


Hi

I am facing a problem. I've made an application in which two forms are
there. One if Login form and another is main form. The problem is that,
after getting authenticated from the database the main form is opened. But
the login form is not getting closed. I am using the following code:

It is in the load of the main form.
this.ParentForm.Close();

Giving some error.



Regards

Tarun Mangla
 
Hi Tarun
You shouldn't make the logon form the parent form as when you do so . you
will not be able to close the logon form ( the parent) and leave the main
form ( the child) .
One thing you can do; however, is to make the parent form the main form (
it is the one that is passed as a pram to the application.run() function .
Then , on the load event handler of the main form you hide it and open
the logon form
this.Hide() // or this.Visible = false ; then open your login
from as dialog
then get the result from the login form in a dialogResult Object ( in the
main form).
You can read about DialogResult on this link

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
frlrfsystemwindowsformsdialogresultclasstopic.asp
If the result shows that the user is authenticated , then close the login
and make the main visible, if not however , you can do whatever action you
want event close the application completely , display error message or
whatever action that match your requirements
hope this helps
Mohamed Mahfouz
MEA Developer Support Center
ITworx on behalf of Microsoft EMEA GTSC
 
Shiva said:
One option is to hide the login form (after successful logon) and show the
main form using ShowDialog() method (assuming logon form is the startup
form).
Alternative is to show the logon and main forms indepenantly (from Main()).
That is, show the logon form using ShowDialog and based on the dialog result
(eg: DialogResult.OK for successful logon), close the logon form and show
the main form using Application.Run().

Usually, I open login views as Modal Dialogs im my main forms
Loaded-Eventhandler:

private void MainForm_Load(object sender, System.EventArgs e)
{
LoginView login = new LoginView();
login.ShowDialog(this);
// Do somethin to check if login was successful,
// maybe quit if it wasn't...
}

Thus, the login dialog will be closed automatically before the main view is
opened.

Exactly what error do you encounter ?
 
Hi

When I try to hide the main form on the load event, it is not hiding at
all. I don;t know why. I am just sending you the code....

Lets suppose Form1 is the main form and Form2 is the login form and on
the load of form1 I've writtent the code below:

private void Form1_Load(object sender, System.EventArgs e)

{

this.Hide();

Form2 frm2=new Form2(this);

frm2.Show();

}


But the main form is not hiding. When I run this application both the forms
appears on the screen.

Waiting for your reply.

Regards

Tarun Mangla
 
Hi!

Tarun Mangla wrote:
[...snip...]
When I try to hide the main form on the load event, it is not hiding at
all. I don;t know why. I am just sending you the code....

Lets suppose Form1 is the main form and Form2 is the login form and on
the load of form1 I've writtent the code below:
[...snip...]

My last application with a login dialog I coded like this (Form1 is the main
form, Form2 the login form class):

private void Form1_Load(object sender, System.EventArgs e)
{
while (this.login() == false) {};
// whatever...
}

private bool login()
{
Form2 loginDialog = new Form2();
loginDialog.ShowDialog(this);
// get credentials from login dialog
// check if credentials are valid, return false otherwise
// login to the database, return true if login succeeds, false otherwise
}

My main form does not show until the program returns from login()...
 
Back
Top