8 Replies - 53413 Views - Last Post: 04 October 2008 - 04:55 PM Rate Topic: -----

#1 virusinfektion  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 11-August 08

open a second form?

Posted 03 October 2008 - 02:25 PM

In my application it has a user login and when a user logs in i want it to bring up the second form. Ive tried several differnt methods from which I have found on google search but none have seem to worked. This is an example of the code below:

		private void button1_Click(object sender, EventArgs e)
		{

			if (textBox1.Text == ("admin")) // username
			{
				if (textBox2.Text == ("admin")) // password
				{
					Form2.Show();
					   
				}
			}


This post has been edited by virusinfektion: 03 October 2008 - 02:27 PM


Is This A Good Question/Topic? 0
  • +

Replies To: open a second form?

#2 jacobjordan  Icon User is offline

  • class Me : Perfection
  • member icon

Reputation: 113
  • View blog
  • Posts: 1,499
  • Joined: 11-June 08

Re: open a second form?

Posted 03 October 2008 - 02:38 PM

You must first create an instance of your form to show it. Use this code:
        private void button1_Click(object sender, EventArgs e)
        {

            if (textBox1.Text == ("admin")) // username
            {
                if (textBox2.Text == ("admin")) // password
                {
                    Form2 f2 = new Form2();
                    f2.Show();
                      
                }
            }


Was This Post Helpful? 1
  • +
  • -

#3 virusinfektion  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 11-August 08

Re: open a second form?

Posted 03 October 2008 - 02:56 PM

Thanks for the reply, but now im getting this error and cannont seem to fix it. Any tips or hints?

Quote

Error 1 The type or namespace name 'Form2' could not be found (are you missing a using directive or an assembly reference?) C:\Users\Paul\AppData\Local\Temporary Projects\myapp\Form1.cs 28 21 myapp

This post has been edited by virusinfektion: 03 October 2008 - 02:57 PM

Was This Post Helpful? 0
  • +
  • -

#4 jacobjordan  Icon User is offline

  • class Me : Perfection
  • member icon

Reputation: 113
  • View blog
  • Posts: 1,499
  • Joined: 11-June 08

Re: open a second form?

Posted 03 October 2008 - 02:59 PM

Then you must not have created another form called Form2. To add another form to your project, go to Project->Add New Item. Select a Form, and design it however you want. Then, if your form is called Form2 that code should work.
Was This Post Helpful? 0
  • +
  • -

#5 virusinfektion  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 11-August 08

Re: open a second form?

Posted 03 October 2008 - 03:11 PM

I do have a form2. Its not working
Was This Post Helpful? 0
  • +
  • -

#6 jacobjordan  Icon User is offline

  • class Me : Perfection
  • member icon

Reputation: 113
  • View blog
  • Posts: 1,499
  • Joined: 11-June 08

Re: open a second form?

Posted 03 October 2008 - 03:13 PM

Hmm... this is weird. Is the form2 in your application, or is it in a DLL or something? Also, make sure the Name of the form is Form2 exactly. If not, you can either change the name of the form or change the name of the form in the code i gave you.
Was This Post Helpful? 0
  • +
  • -

#7 virusinfektion  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 11-August 08

Re: open a second form?

Posted 03 October 2008 - 03:21 PM

Its name is exactly Form2 and no its not a Dll and I have tried changing the name several times but I still get the same error.

EDIT: Ive fixed my problem but thanks for your help :)

This post has been edited by virusinfektion: 03 October 2008 - 03:24 PM

Was This Post Helpful? 0
  • +
  • -

#8 jacobjordan  Icon User is offline

  • class Me : Perfection
  • member icon

Reputation: 113
  • View blog
  • Posts: 1,499
  • Joined: 11-June 08

Re: open a second form?

Posted 03 October 2008 - 03:23 PM

Then there must be something i'm missing. If the form is in the same namespace as the code you are accessing it from, it should work. Is it in the same namespace?
Was This Post Helpful? 0
  • +
  • -

#9 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1526
  • View blog
  • Posts: 5,961
  • Joined: 21-March 08

Re: open a second form?

Posted 04 October 2008 - 04:55 PM

View Postvirusinfektion, on 3 Oct, 2008 - 05:56 PM, said:

Thanks for the reply, but now im getting this error and cannont seem to fix it. Any tips or hints?

Quote

Error 1 The type or namespace name 'Form2' could not be found (are you missing a using directive or an assembly reference?) C:\Users\Paul\AppData\Local\Temporary Projects\myapp\Form1.cs 28 21 myapp


the "Form2" in JacobJordan's code is a generic name given to a newly added form if you don't give it a meaningful name. if you named your second form something else, then you will put the name of YOUR second form in the place of "Form2" in the code example.

for example, if you named your second form "frmMainAfterLogin", then your code would look like this...

private void button1_Click(object sender, EventArgs e)
		{

			if (textBox1.Text == ("admin")) // username
			{
				if (textBox2.Text == ("admin")) // password
				{
					frmMainAfterLogin f2 = new frmMainAfterLogin();
					f2.Show();
					  
				}
			}
		}


Was This Post Helpful? 1
  • +
  • -

Page 1 of 1