Join 136,085 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,548 people online right now. Registration is fast and FREE... Join Now!
Ok, so I have a login form that is calling and instantiating a protected internal (friend in VB) instance of a form and then hiding itself. What I need to do is access this login form from the new form to close it when the new form is closing. In VB this works perfect and I can access it, however it does not work from C# unless I use a private instantiation. The problem is that I need to be able to access the new form from other forms that it will call. The code I use for closing it in VB is simply to call
CODE
Login.Close()
but from within C# if I try to call Login.Close() I'm getting the error that logIn.Login does not contain a definition for 'close'. logIn of course being the namespace of the project itself.
Which brings me to another question, if and if yes then how do you close all active forms attached to your project without accessing them specifically by name?
Once I'm in deptInfo, I attempt to call logIn.Close(); and intellisense doesn't recognize the close method within the logIn class for some reason.
It would work if I declare and instantiate a new instance of the logIn class but that's not what I want to do. I want to access the class that has already been instantiated, it's just hidden.
This post has been edited by nofear217: 18 Apr, 2008 - 08:49 AM
logIn.Close(); doesn't work because there is no class method called close. Please don't put one in...
First, who created and called your logIn object?
Rather than chaining: show logIn -> logIn -> show deptInfo -> deptInfo -> attempt to close ???
Perhaps you can do something like this:
csharp
logIn logInInstance = new logIn(); logInInstance.ShowDialog(); if (logInInstance.IsLoggedIn) { deptInfo deptInfoInstance = new deptInfo(); deptInfoInstance.user = logInInstance.user; deptInfoInstance.ShowDialog(); } else { MessageBox.Show("Better luck next time."); }
Obviously, you'll need to implement the properties user and IsLoggedIn in logIn.
Also, just cosmetic, but very handy, consider making all class, property, and method names start with upper case and all variables with lower. Not only is this more standard, but it allows for simple conventions like this:
csharp
LogIn logIn = new LogIn();
Now, there's no doubt as to what's an instance and what's a class.
Bah, perhaps I stated incorrectly what I meant, the class (LogIn) is indeed a form, and is the startup form, which does have a close method. So what I need to do is open the login form, if their login credentials are correct, hide the Login, and then, show the DeptInfo form. That's why I was declaring protected internal frm5 = new DeptInfo();. Because once the information on DeptInfo is completed, I will need to hide DeptInfo and then call another form, ProgramInfo. The only purpose of me accessing LogIn is to be able to close it if someone closes DeptInfo or ProgramInfo. So are you suggesting to open DeptInfo and then instantiate and call the ShowDialog method on LogIn?
This post has been edited by nofear217: 21 Apr, 2008 - 05:18 AM
2 AM' post='343814'] the class (LogIn) is indeed a form, and is the startup form, which does have a close method. [/quote]
The class Form, which any form based class you create should extend, has a Close method. So, all your forms have a Close method. A "startup form" is simply the first form called. The code I offered goes there, where the first call is made. I know VB.NET hides all this, but in C# there should be a static "main" method that launches the first form, usually found in Program.cs, but older versions of Visual Studio put it in the first form class it created. In truth, it can be anywhere, but there can only be one of them.
When you try to call LogIn.Close, what exactly are you trying to do? More specifically, what are you trying to close?
Here's what I mean:
csharp
LogIn logInA = new LogIn(); LogIn logInB = new LogIn();
logInA.Show(); logInB.Show(); MessageBox.Show("Now you see two login forms.");
// who am i closing? Doesn't matter, doesn't work. // LogIn.Close();
// you can close a form object, though logInA.Close(); MessageBox.Show("Now you see only one login form.");
QUOTE(nofear217 @ 21 Apr, 2008 - 09:12 AM)
So are you suggesting to open DeptInfo and then instantiate and call the ShowDialog method on LogIn?
Not exactly. If you're ultimately working your way toward ProgramInfo, same logic works fine:
csharp
LogIn logIn = new LogIn(); logIn.ShowDialog(); if (!logIn.IsLoggedIn) { MessageBox.Show("Not logged in, good bye."); return; }
DeptInfo deptInfo = new DeptInfo(); deptInfo.User = logIn.User; deptInfo.ShowDialog(); if (!deptInfo.IsSubmitted) { MessageBox.Show("No deparment chosen."); return; }
ProgramInfo progInfo = new ProgramInfo(); progInfo.Data = deptInfo.Data; // want to pass it user info too? you could //progInfo.User = logIn.User; progInfo.ShowDialog(); MessageBox.Show("We're done, was it good for you?");
When I call LogIn.Close(), it will either be from DeptInfo or ProgInfo. This is because when the user clicks OK on the log in and the credentials are accepted, I call this.Hide() on the LogIn form and then Show on the DeptInfo(). If I close DeptInfo(), LogIn stays hidden and keeps the application locked in Debug mode. If I try to call Show on DeptInfo and then close on LogIn, the entire application closes down.
And I guess I still don't understand the purpose behind the code you suggested and whether it should go in the startup form or the static void Main method in Program.cs.
And Hagrid, I really do appreciate your help.
This post has been edited by nofear217: 21 Apr, 2008 - 10:57 AM
Program pgm = new Program(); // if they don't login correctly, we can just stop. if (!pgm.ValidateUser()) { return; }
if (pgm.ShowDeptInfo()) { pgm.ShowProgramInfo(); } // THIS IS WHAT CLOSES THE APPLICATION!!! // when you reach the end of the entry block, you leave. // that prior return mean application done. reach here, done. // The default Application.Run(new Form1()); remains active until // the instance of Form1 is closed. Then it's done. // You control this block. You leave when you want to. } } }