Basic question about login form and passing success back to main form

R

Ronald S. Cook

It's been longer that I remember since writing windows (not web) apps.

1) I want to load a main form

2) User clicks login button which brings up login form (on top of main form)

3) Upon entering successful password and clicking ok, login form should go
away

4) Main form should then display admin controls

I'm not sure how this code should look. How should success be passewd back
to the main form? If the main form is already loaded (just behind the login
form), how then do I "reload" (or whatever) to let it know it should show
some admin controls?

Thanks for any help!
RC
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

Ronald S. Cook said:
It's been longer that I remember since writing windows (not web) apps.

1) I want to load a main form

2) User clicks login button which brings up login form (on top of main
form)

3) Upon entering successful password and clicking ok, login form should go
away

4) Main form should then display admin controls

I'm not sure how this code should look. How should success be passewd
back to the main form? If the main form is already loaded (just behind
the login form), how then do I "reload" (or whatever) to let it know it
should show some admin controls?


A very simple way of solving this is displaying the login as a modal
windows (which probably you use ) , set the DialogResult accordingly and if
the correct value is received show the controls:

//you can use any of the values of DialogResult
if ( theLoginForm.ShowDialog() == DialogResult.Yes )
SetAdminControlsVisible();


Btw, you use Form.DialogResult = ... inside the form. probably like

void button1_onclick(.... )
{
if( Islogincorrect( textbox1.Text, textbox2.Text) )
{
this.DialogResult = DialogResult.Yes;
this.Close();
}
}



How to make the controls visible depends of your form and the controls, you
can simple do a Control.Visible = true; to all the controls you need.
 
E

Eric Renken

Actually once you set the DialogResult of a form you don't need to call
this.Close(). Setting the DialoResult seems to take care of that from what
I have seen.

Eric Renken
 
J

Jon Skeet [C# MVP]

Eric Renken said:
Actually once you set the DialogResult of a form you don't need to call
this.Close(). Setting the DialoResult seems to take care of that from what
I have seen.

Fortunately it's more than "seems to" (which would be a dodgy thing to
rely on, IMO). It's nicely documented:

(From the DialogResult property docs.)

<quote>
If the form is displayed as a dialog box, setting this property with a
value from the DialogResult enumeration sets the value of the dialog
box result for the form, hides the modal dialog box, and returns
control to the calling form
</quote>
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

(From the DialogResult property docs.)

<quote>
If the form is displayed as a dialog box, setting this property with a
value from the DialogResult enumeration sets the value of the dialog
box result for the form, hides the modal dialog box, and returns
control to the calling form
</quote>


Yep, always RTFM first :)
 
E

Eric Renken

I guess to me it always works fine because when I display a modal dialog
like this I always have them wrapped in a using statement so when it is
finished I know it will be disposed of.

using ( Form myModal = new AskAQuestion() )
{
myModal.ShowDialog();
}

Eric Renken
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top