Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C# (http://www.programmingforums.org/forum16.html)
-   -   dealing with multiple forms (http://www.programmingforums.org/showthread.php?t=9721)

Booooze May 11th, 2006 12:51 AM

dealing with multiple forms
 
It's kind of pathetic really. I have done so muchwith C# lately on this project, that this is the problem holding me back. For some reason I have no problem executing and dealing with Mysql statements, but I can't hide and show forms! before you think that this is easy, hear me out.

I have 2 forms. One is called frmMain, and the other is frmPassword. I'm basicly putting a simple password on this program. frmMain loads on execution. It checks a text file for a password. if a password exists, it then creates a new instance of frmPassword form, and displays it.

:

frmPassword frmPass = new frmPassword();
frmPass.Show();


All god so far. But now, I need to hide frmMain. Although frmMain is the one doing all the execution, it seems like all the people on google, and even the books im using, simply say to use this.Hide(); (or change the visible property) I'm not going to lie. That makes complete sense. Crazy part is, it doesn't work. Note that this is all in the frmMain_Load event. When I put a button on the form, and having the click event execute the hide method, the form hides itself, but that is no use to me. Next crazy part it: When I execute this code:
:

MessageBox.Show(sender.ToString());

under the load event, it returns the frmMain! It makes no sense. This is the problem I am having. Although solve one at a time, the next thing I need to do, is when the user puts in the password, I have to have it close frmPassword, and Show FrmMain.

I have researched this problem, and can't find any answers. I honestly can't figure it out. I didn't post much code as there isn't much to post. Between hide and show, the visible property, and creating the frmPassword, there is no code related. I'm going to head to bed now, and hope for some answers in the morning. Any help greatly appreciated. Thanks. :)

Ooble May 11th, 2006 11:33 AM

You can't hide frmMain until it's shown. this.Show() is called by default at the beginning of the Load function, and I don't believe you can change this.

I would suggest starting your program with a neutral class (Visual Studio 2005 actually does this by default - you get a Program class, which runs your main form), which shows frmMain only when necessary.

Booooze May 11th, 2006 11:40 AM

Quote:

Originally Posted by Ooble
You can't hide frmMain until it's shown. this.Show() is called by default at the beginning of the Load function, and I don't believe you can change this.

I would suggest starting your program with a neutral class (Visual Studio 2005 actually does this by default - you get a Program class, which runs your main form), which shows frmMain only when necessary.

Ok, I think I might have been exploring with that a bit earlier. I beleive it's the app.config file if I remember correctly. I'll try it out and post back. Thanks Ooble.

[EDIT] not app.config, It's program.cs

Jason Isom May 11th, 2006 11:46 AM

Can you try doing something like this:

:

namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            if (File.Exists(@"C:\FOO.TXT"))
            {
                MessageBox.Show("File Exists!");
                DoSomething();
            }
            else
            {
                this.Hide();
                using (Form2 frm = new Form2())
                {
                    if (frm.ShowDialog() == DialogResult.OK)
                    {
                        DoSomethingElse();
                    }
                }
                this.Show();
            }
           
        }

        void DoSomething() { }
        void DoSomethingElse() { }
    }
}


I apologize for all the generic names, I'm on my lunch break from work. :)

Kilo May 11th, 2006 11:49 AM

I would suggest loading your main form.

Then in the main form load event load frmPassword

when you load frmPassword pass the main form to an overloaded constructor

create a form object on your frmPassword

ex.
Form mainFormInstance;

after the instance is passed to the contructor store it in your new variable

immedialty disable the main form -> mainFormInstance.Enabled = false;

after that create a closing event for frmPassword and within that
enable the main form.

Ooble May 11th, 2006 11:56 AM

Quote:

Originally Posted by Kilo
I would suggest loading your main form.

Then in the main form load event load frmPassword

when you load frmPassword pass the main form to an overloaded constructor

create a form object on your frmPassword

ex.
Form mainFormInstance;

after the instance is passed to the contructor store it in your new variable

immedialty disable the main form -> mainFormInstance.Enabled = false;

after that create a closing event for frmPassword and within that
enable the main form.

Ew. :p

:

class Program
{
        frmMain MainForm;
        frmPassword PasswordForm;
       
        public static void Main (string[] args)
        {
                if (something)
                {
                        PasswordForm = new frmPassword();
                        PasswordForm.Show();
                }
                else
                {
                        MainForm = new frmMain();
                        MainForm.Show();
                }
        }
}


Booooze May 11th, 2006 1:03 PM

Thanks for the replys guys.

@Jason- Yeah, I realised this after the fact. :rolleyes: I probabyl could have done it your way. It was last night so I hadn't thought about it yet, but since I just started throwing some more code in the public frmMain, I realised I probably could have done the form work in there too. Thanks for the reply. Much appreciated. ;)

@Kilo- I'm not great with overloaded constructors and destructors etc, as I come from a vb6 / some java background. Never really went far with C++, I would have to do some more research to figure it out your way.

I've got it working, and I've used Oobles idea. It seems to be working great, as i just use a conditional statement to figure out which form to load, and if its the frmPassword, it will create a new instance of frmMain, then hide itself. frMain.close contains Application.ExitThread(); as I only want the program to shut down if frmMain is closed. (and if the user decides to cancel becuas ethey can't break the password :p

My only concern now, is which way is most effiecient? Personally, I don't really know. The way I have now seems to work fine. Any thoughts?

Thanks again :)

Ooble May 12th, 2006 10:11 AM

The way I do it is the way Microsoft recommend. I don't know if it's the most efficient, but you can be sure it's the easiest to understand and debug.

Booooze May 12th, 2006 11:15 AM

Quote:

Originally Posted by Ooble
The way I do it is the way Microsoft recommend. I don't know if it's the most efficient, but you can be sure it's the easiest to understand and debug.

Good enough for me :)

Jason Isom May 12th, 2006 11:24 AM

Quote:

Originally Posted by Ooble
The way I do it is the way Microsoft recommend. I don't know if it's the most efficient, but you can be sure it's the easiest to understand and debug.


Link? I'm just curious why Microsoft would recommend your approach. I'm not saying it's a bad way to implement it because it obviously works.

I may be reading into the original post, but I thought the Password form was going to be implemented like a dialog, meaning that if there wasn't a password already "auto-saved" (ie in a text file) that the program should popup the Password Form and then Dispose(...) the password form and popup the main form (assuming authentication was successful)

With your approach, it'll popup the password form and then immediately exit because you haven't used Application.Run(...) so your program doesn't have a message pump.

Although, I'm sure your example can be modified to support that if that's what the original poster wanted.


All times are GMT -5. The time now is 7:36 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC