Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 2005 > C# 2005
| Search | Today's Posts | Mark Forums Read
C# 2005 For discussion of Visual C# 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# 2005 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 19th, 2007, 02:12 AM
Authorized User
 
Join Date: Apr 2007
Location: , , .
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
Default Tricky : Don't want to load MDI form first

Hello,

I have a scenario in winforms where I have to load my main form (MDI) and then load login screen. I want to load only login screen and user authenticates then load main form ( MDI ).

My program.cs has main funtion. It contains Application.Run command. If I pass my login form for application.run as parameter. I cannot close (this.close) the login form when user successfully authenticates as it closes my application form. To avoid this I load my main form first with Application.Run followed by Login form in load event of main form

How can I load only login form without loading main form.

Thanks,
Navdeep


 
Old July 20th, 2007, 12:02 PM
Authorized User
 
Join Date: Sep 2004
Location: Edegem, Antwerpen, Belgium.
Posts: 40
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,

Here is a possible solution:
You need 2 forms:
- MainForm (IsMdiContainer = True)
- LogonForm (optionally: ControlBox = False; FormBorderStyle = FixedSingle; MaximizeBox = False)

In the LogonForm create a method that checks the validity of the credentials passed. If the credentials are good, set the DialogResult of the form to OK.

this.DialogResult = DialogResult.OK;

In the Program.cs set the following:
[STAThread]
static void Main()
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(fals e);

    LogonForm frm = new LogonForm();
    DialogResult result = frm.ShowDialog();

    if (result == DialogResult.OK)
        Application.Run(new MainForm());
}

This way, your logonform is first opened and only if the user entered valid credentials, the mainform (mdi) is opened.

Greetz,

Geert

http://geertverhoeven.blogspot.com
 
Old July 20th, 2007, 01:04 PM
Authorized User
 
Join Date: Apr 2007
Location: , , .
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Geertz,

Thanks, I will surely test this out.
It should work fine if I close login form.


Navdeep

 
Old July 20th, 2007, 01:48 PM
Authorized User
 
Join Date: Apr 2007
Location: , , .
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Originally posted by GeertVerhoeven



this.DialogResult = DialogResult.OK;


    LogonForm frm = new LogonForm();
    DialogResult result = frm.ShowDialog();

I think I am missing something here.
I new the form. Login screen appear. user enters credentials. CLicks ok. validations are checked. How does "result" gets value from frm.ShowDialog(). If I close the login it never reaches
    DialogResult result = frm.ShowDialog();


Navdeep

 
Old July 20th, 2007, 03:24 PM
Authorized User
 
Join Date: Sep 2004
Location: Edegem, Antwerpen, Belgium.
Posts: 40
Thanks: 0
Thanked 0 Times in 0 Posts
Default

The form class contains a field named DialogResult with type DialogResult.

Since the instance of the LogonForm is still available after the form is closed, you can request the value of the field by doing:

LogonForm frm = new LogonForm();
DialogResult result = frm.DialogResult;

If you check the signature of the ShowDialog method, you can see that it returns a value with type DialogResult. This value is the value that comes from the forms DialogResult property.

If it isn't clear, please let me know.

Geert

http://geertverhoeven.blogspot.com
 
Old July 20th, 2007, 09:46 PM
Authorized User
 
Join Date: Apr 2007
Location: , , .
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Geert,

Thanks for your inputs.
I have tested the code. this is the main() code
Quote:
quote:
static void Main()
        {
            //string mys = "c:\abc";
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(fals e);
            frmLogin frm = new frmLogin();
            frm.ShowDialog();
            DialogResult result = frm.DialogResult;
            if (result == DialogResult.OK)
            {
                Application.Run(new frmMain());
            }

        }
"result" is always "cancel". I am using this.close after I check credentials on logon form. So the logic does not work. Can you test at your end. Am I missing something. I understand what you have tried to convey but showdialog returns cancel.

-Navdeep


 
Old July 20th, 2007, 09:53 PM
Authorized User
 
Join Date: Apr 2007
Location: , , .
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
quote:Originally posted by GeertVerhoeven


If you check the signature of the ShowDialog method, you can see that it returns a value with type DialogResult. This value is the value that comes from the forms DialogResult property.
I got the missing link.
Apart from last code I mentioned in last post , I added this line of code in logon form Ok button after successful authentication

Quote:
quote:
this.DialogResult = DialogResult.OK;
It works now.
Thanks.



 
Old May 6th, 2008, 01:51 AM
Registered User
 
Join Date: May 2008
Location: chennai, tamilnadu, India.
Posts: 8
Thanks: 0
Thanked 3 Times in 3 Posts
Send a message via AIM to samdoss Send a message via Yahoo to samdoss
Default

Program.cs LoginForm and MainForm

The Code is Perfectly Working.

frmlogin.cs file

btnok_click()
{
this.DialogResult = DialogResult.OK;

}

program.cs file

static void main()
{

Application.--------;
Application.---------;
frmLogin login = new frmLogin();
DialogResult result = login.ShowDialog();
if (result == DialogResult.OK)
Application.Run(new MainForm());

}


Regards,

Samdoss.J:(

samdoss.j




Similar Threads
Thread Thread Starter Forum Replies Last Post
Displaying MDI Child Form Menus with MDI Parent ashu_from_india Pro VB 6 3 June 10th, 2008 11:01 PM
Controlling MDI child form from MDI parent panel LuxCoder Pro Visual Basic 2005 1 May 25th, 2007 12:20 PM
Controlling MDI child form from MDI parent panel LuxCoder VB.NET 2002/2003 Basics 7 April 11th, 2007 02:38 PM
Controlling MDI child form from MDI parent panel LuxCoder VB How-To 1 April 9th, 2007 03:25 PM
Controlling MDI child form from MDI parent panel LuxCoder Visual Basic 2005 Basics 2 April 9th, 2007 03:24 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.