Welcome to Dream.In.Code
Getting C# Help is Easy!

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!




Another VB to C# question

 
Reply to this topicStart new topic

Another VB to C# question

nofear217
18 Apr, 2008 - 06:32 AM
Post #1

D.I.C Head
Group Icon

Joined: 8 Nov, 2007
Posts: 163



Thanked: 1 times
Dream Kudos: 175
My Contributions
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?

Thanks again in advance for any and all advice.
User is offlineProfile CardPM
+Quote Post

zakary
RE: Another VB To C# Question
18 Apr, 2008 - 07:47 AM
Post #2

D.I.C Regular
Group Icon

Joined: 15 Feb, 2005
Posts: 404



Thanked: 6 times
Dream Kudos: 175
My Contributions
C# forms have a Dispose(); method you may want to use Login.Dispose();
User is offlineProfile CardPM
+Quote Post

nofear217
RE: Another VB To C# Question
18 Apr, 2008 - 07:54 AM
Post #3

D.I.C Head
Group Icon

Joined: 8 Nov, 2007
Posts: 163



Thanked: 1 times
Dream Kudos: 175
My Contributions
Nope, it's almost like it doesn't recognize that Login is an active form.
User is offlineProfile CardPM
+Quote Post

zakary
RE: Another VB To C# Question
18 Apr, 2008 - 08:31 AM
Post #4

D.I.C Regular
Group Icon

Joined: 15 Feb, 2005
Posts: 404



Thanked: 6 times
Dream Kudos: 175
My Contributions
it should look like this
csharp


Login.Dispose(true);



as long as Login is declared and initialize.
User is offlineProfile CardPM
+Quote Post

nofear217
RE: Another VB To C# Question
18 Apr, 2008 - 08:47 AM
Post #5

D.I.C Head
Group Icon

Joined: 8 Nov, 2007
Posts: 163



Thanked: 1 times
Dream Kudos: 175
My Contributions
Here's the code
CODE

using System;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace logIn
{
    public partial class logIn : Form
    {
        #region Variables
        authenticateDataContext db = new authenticateDataContext();
        Stuff fr = new Stuff();
        protected internal string salting = "****";
        protected internal deptInfo frm5 = new deptInfo();
        #endregion

        public logIn()
        {
            InitializeComponent();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (txtPassword.Text == "Password")
            {
                frmChangePassword frm1 = new frmChangePassword();
                frm1.Show();
            }
            else
            {
                string source = salting + txtPassword.Text;
                string hash = fr.getMd5Hash(source);
                string hashed = fr.getMd5Hash(hash);

                var verify = (from ve in db.Logins
                              where ve.userName == txtUserName.Text && ve.password == hashed
                              select ve).Count();

                if (verify != 0)
                {
                    
                    frm5.user = txtUserName.Text;
                    frm5.Show();
                    this.Hide();
                }
                else
                {
                    MessageBox.Show("Unknown Username/Password, please try again.");
                }
            }
        }

        private void logIn_FormClosing(object sender, FormClosingEventArgs e)
        {
            frm5.Close();
        }

        private void btnChange_Click(object sender, EventArgs e)
        {
            frmChangePassword frm1 = new frmChangePassword();
            frm1.Show();
        }

        private void logIn_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (txtPassword.Text == "Password")
                {
                    frmChangePassword frm1 = new frmChangePassword();
                    frm1.Show();
                }
                else
                {
                    string source = salting + txtPassword.Text;
                    string hash = fr.getMd5Hash(source);
                    string hashed = fr.getMd5Hash(hash);

                    var verify = (from ve in db.Logins
                                  where ve.userName == txtUserName.Text && ve.password == hashed
                                  select ve).Count();

                    if (verify != 0)
                    {
                        frm5.user = txtUserName.Text;
                        frm5.Show();
                        this.Hide();
                    }
                    else
                    {
                        MessageBox.Show("Unknown Username/Password, please try again.");
                    }
                }
            }
        }
    }
}



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
User is offlineProfile CardPM
+Quote Post

baavgai
RE: Another VB To C# Question
18 Apr, 2008 - 09:38 AM
Post #6

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 2,019



Thanked: 105 times
Dream Kudos: 475
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua

My Contributions
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.

Hope this helps.

User is offlineProfile CardPM
+Quote Post

nofear217
RE: Another VB To C# Question
21 Apr, 2008 - 05:12 AM
Post #7

D.I.C Head
Group Icon

Joined: 8 Nov, 2007
Posts: 163



Thanked: 1 times
Dream Kudos: 175
My Contributions
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
User is offlineProfile CardPM
+Quote Post

baavgai
RE: Another VB To C# Question
21 Apr, 2008 - 06:08 AM
Post #8

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 2,019



Thanked: 105 times
Dream Kudos: 475
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua

My Contributions
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?");


Hope this helps.

User is offlineProfile CardPM
+Quote Post

nofear217
RE: Another VB To C# Question
21 Apr, 2008 - 10:57 AM
Post #9

D.I.C Head
Group Icon

Joined: 8 Nov, 2007
Posts: 163



Thanked: 1 times
Dream Kudos: 175
My Contributions
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. biggrin.gif

This post has been edited by nofear217: 21 Apr, 2008 - 10:57 AM
User is offlineProfile CardPM
+Quote Post

baavgai
RE: Another VB To C# Question
21 Apr, 2008 - 03:01 PM
Post #10

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 2,019



Thanked: 105 times
Dream Kudos: 475
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua

My Contributions
Ok, I must admit I'm at a little bit of a loss as to what more I can say. However, we'll give it one last go.

Here's the Program.cs file created when I start a new windows project called SimpleFormCall:
csharp

using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace SimpleFormCall {
class Program {
[STAThread]
static void Main() {
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}


Here's a new Program.cs:
csharp

using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace SimpleFormCall {
class Program {
private string userName;
private string departmentName;
public Program() { }

public bool ValidateUser() {
this.userName = null;
try {
LogIn logIn = new LogIn();
logIn.ShowDialog();
if (logIn.IsLoggedIn) {
this.userName = logIn.user;
return true;
}
} catch (Exception ex) {
MessageBox.Show(ex.Message, "Login Error");
return false;
}
MessageBox.Show("Not logged in, good bye.", "Login Error");
return false;
}

public bool ShowDeptInfo() {
DeptInfo deptInfo = new DeptInfo();
deptInfo.User = this.User;
deptInfo.ShowDialog();
if (deptInfo.IsSubmitted) {
this.departmentName = deptInfo.departmentName;
}
return deptInfo.IsSubmitted;
}

public void ShowProgramInfo() {
ProgramInfo progInfo = new ProgramInfo();
progInfo.departmentName = this.departmentName;
progInfo.User = this.User;
progInfo.ShowDialog();
}


public string User { get { return userName; } }


[STAThread]
static void Main() {
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
// forget this, we'll handle it ourselves
//Application.Run(new Form1());

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.
}
}
}


Hope this makes sense.

User is offlineProfile CardPM
+Quote Post

nofear217
RE: Another VB To C# Question
22 Apr, 2008 - 05:13 AM
Post #11

D.I.C Head
Group Icon

Joined: 8 Nov, 2007
Posts: 163



Thanked: 1 times
Dream Kudos: 175
My Contributions
Aha, there we go. I'd gotten lost following you earlier about putting the code in Program.cs. Thanks for the help biggrin.gif
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/1/08 08:05PM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month