![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
|
|
#1 |
|
Expert Programmer
|
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. ![]() |
|
|
|
|
|
#2 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
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. |
|
|
|
|
|
#3 | |
|
Expert Programmer
|
Quote:
[EDIT] not app.config, It's program.cs |
|
|
|
|
|
|
#4 |
|
Programmer
Join Date: Dec 2005
Posts: 53
Rep Power: 3
![]() |
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. ![]() |
|
|
|
|
|
#5 |
|
Expert Programmer
|
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 |
|
|
|
|
|
#6 | |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
Quote:
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();
}
}
} |
|
|
|
|
|
|
#7 |
|
Expert Programmer
|
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 ![]() |
|
|
|
|
|
#8 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
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.
|
|
|
|
|
|
#9 | |
|
Expert Programmer
|
Quote:
![]() |
|
|
|
|
|
|
#10 | |
|
Programmer
Join Date: Dec 2005
Posts: 53
Rep Power: 3
![]() |
Quote:
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. |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|