8 Replies - 2174 Views - Last Post: 14 December 2011 - 07:58 AM Rate Topic: -----

#1 BlackPanthaa  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 13-December 11

Opening a form using another + using variables in different forms

Posted 14 December 2011 - 06:25 AM

Hey guys. I basically have a login form and when the user enters "test" as the username and password a box pops up and another form should open. But it just closes the whole program.

             if ((this.usernamebox1.Text == "test") && (this.passwordbox1.Text == "test"))
            {
                MessageBox.Show("Login complete.");
                new Form1();
                Close(); 


Also how would I access a variable thats in one form in another, such as setting a bool to false and a window pops up, then when the user fills out the form it sets it to true from that window.

Thanks!

Noticed I forgot to copy the } but it is in the actual code.

Is This A Good Question/Topic? 0
  • +

Replies To: Opening a form using another + using variables in different forms

#2 negligible  Icon User is offline

  • D.I.C Regular

Reputation: 62
  • View blog
  • Posts: 302
  • Joined: 02-December 10

Re: Opening a form using another + using variables in different forms

Posted 14 December 2011 - 07:06 AM

You can access static members of a class from outside with MyClassName.myStaticString

If you type a class name followed by a dot "." in Visual Studio IntelliSense will show you the available members/methods available to use.

Take a look at the example in the Static Members section of the MSDN page.
http://msdn.microsof...y/79b3xss3.aspx
Was This Post Helpful? 0
  • +
  • -

#3 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: Opening a form using another + using variables in different forms

Posted 14 December 2011 - 07:16 AM

Instead of using a static property, just make a normal property. In the code for the form that you want to pass data to, create a property like..

public bool IsLoginSuccessful { get; set;}



Then, in your code from the form where the data is coming from...

MessageBox.Show("Login complete.");
Form1 f1 = new Form1();
f1.IsLoginSuccessful = true;
f1.Show();
this.Hide();


Was This Post Helpful? 0
  • +
  • -

#4 BlackPanthaa  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 13-December 11

Re: Opening a form using another + using variables in different forms

Posted 14 December 2011 - 07:23 AM

View Posteclipsed4utoo, on 14 December 2011 - 07:16 AM, said:

Instead of using a static property, just make a normal property. In the code for the form that you want to pass data to, create a property like..

public bool IsLoginSuccessful { get; set;}



Then, in your code from the form where the data is coming from...

MessageBox.Show("Login complete.");
Form1 f1 = new Form1();
f1.IsLoginSuccessful = true;
f1.Show();
this.Hide();



This works well thanks, but when I close the box after the login box it is still running hidden. I wish to get rid of the login box once the next box is open.

Thanks.
Was This Post Helpful? 0
  • +
  • -

#5 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: Opening a form using another + using variables in different forms

Posted 14 December 2011 - 07:33 AM

In .Net, the form that starts the application(your login form) has to stay open. If you close it, then the entire application closes.

To get around this, you would make your main form(the form AFTER the login) show first, then in the Load for the main form, you would show the login form. When the login completes, simply close the Login form, and now your main form is waiting.
Was This Post Helpful? 0
  • +
  • -

#6 BlackPanthaa  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 13-December 11

Re: Opening a form using another + using variables in different forms

Posted 14 December 2011 - 07:40 AM

View Posteclipsed4utoo, on 14 December 2011 - 07:33 AM, said:

In .Net, the form that starts the application(your login form) has to stay open. If you close it, then the entire application closes.

To get around this, you would make your main form(the form AFTER the login) show first, then in the Load for the main form, you would show the login form. When the login completes, simply close the Login form, and now your main form is waiting.


When doing this I get:
The name 'lf1' does not exist in the current context

        public void button1_Click(object sender, EventArgs e)
        {
            if ((this.usernamebox1.Text == "test") && (this.passwordbox1.Text == "test"))
            {
                MessageBox.Show("Login complete.");
                lf1.IsLoginSuccessful = true;
                this.Close();
            }

Was This Post Helpful? 0
  • +
  • -

#7 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: Opening a form using another + using variables in different forms

Posted 14 December 2011 - 07:42 AM

The code I posted was just some sample code. It wasn't meant for you to copy and paste. It was meant to show you HOW it COULD be done, not how to put it in your code. This should have been evident from the property I created. IsLoginSuccessful will always be true, because the only way to open the form is to login successfully. So it's redundant. I was simply showing you how to create a property and use it in code. YOU need to determine what data you need to move between the forms.

Where do you declare lf1? I don't see it's declaration.
Was This Post Helpful? 0
  • +
  • -

#8 BlackPanthaa  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 13-December 11

Re: Opening a form using another + using variables in different forms

Posted 14 December 2011 - 07:45 AM

It is declared in Form 1. The main form now, LoginForm1 is my login form

        public void Form1_Load(object sender, EventArgs e)
        {
            LoginForm1 lf1 = new LoginForm1();
            lf1.IsLoginSuccessful = false;
            lf1.Show();
        }

Was This Post Helpful? 0
  • +
  • -

#9 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5522
  • View blog
  • Posts: 11,830
  • Joined: 02-June 10

Re: Opening a form using another + using variables in different forms

Posted 14 December 2011 - 07:58 AM

One form reaching into another for things like this is just plain wrong most of the time.

The clean way to do it is to have the log-in form raise an event with log-in arguments. Those arguments could be the user name and access level for example.

The simplistic "IsSuccessful" has no scalablity to it. It doesn't account for the desire to have one person log in as a User, the next as a manager and the next as a SuperUser for example.


See FAQ #2 for some tutorials on doing this the right way. (That's how often this comes up)


FAQ (Frequently Asked Questions - Updated Nov 2011

Spoiler

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1