Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old May 11th, 2006, 12:51 AM   #1
Booooze
Expert Programmer
 
Booooze's Avatar
 
Join Date: Mar 2006
Location: Igloo
Posts: 710
Rep Power: 3 Booooze is on a distinguished road
Send a message via MSN to Booooze
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.
Booooze is offline   Reply With Quote
Old May 11th, 2006, 11:33 AM   #2
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
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.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old May 11th, 2006, 11:40 AM   #3
Booooze
Expert Programmer
 
Booooze's Avatar
 
Join Date: Mar 2006
Location: Igloo
Posts: 710
Rep Power: 3 Booooze is on a distinguished road
Send a message via MSN to Booooze
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
Booooze is offline   Reply With Quote
Old May 11th, 2006, 11:46 AM   #4
Jason Isom
Programmer
 
Join Date: Dec 2005
Posts: 53
Rep Power: 3 Jason Isom is on a distinguished road
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.
Jason Isom is offline   Reply With Quote
Old May 11th, 2006, 11:49 AM   #5
Kilo
Expert Programmer
 
Kilo's Avatar
 
Join Date: Nov 2005
Location: In Pink Clam?
Posts: 542
Rep Power: 0 Kilo is an unknown quantity at this point
Send a message via AIM to 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.
__________________
"When in Rome, Do as the Romans Do"
"Beauty is in the eye of the BEER holder"
"Save your breath your going to need it for your blow up doll later"

SearchLores.org
Kilo is offline   Reply With Quote
Old May 11th, 2006, 11:56 AM   #6
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
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();
		}
	}
}
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old May 11th, 2006, 1:03 PM   #7
Booooze
Expert Programmer
 
Booooze's Avatar
 
Join Date: Mar 2006
Location: Igloo
Posts: 710
Rep Power: 3 Booooze is on a distinguished road
Send a message via MSN to Booooze
Thanks for the replys guys.

@Jason- Yeah, I realised this after the fact. 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
Booooze is offline   Reply With Quote
Old May 12th, 2006, 10:11 AM   #8
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
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.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old May 12th, 2006, 11:15 AM   #9
Booooze
Expert Programmer
 
Booooze's Avatar
 
Join Date: Mar 2006
Location: Igloo
Posts: 710
Rep Power: 3 Booooze is on a distinguished road
Send a message via MSN to Booooze
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
Booooze is offline   Reply With Quote
Old May 12th, 2006, 11:24 AM   #10
Jason Isom
Programmer
 
Join Date: Dec 2005
Posts: 53
Rep Power: 3 Jason Isom is on a distinguished road
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.
Jason Isom is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 3:09 AM.

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