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

Join 132,360 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,261 people online right now. Registration is fast and FREE... Join Now!




open a second form?

 
Reply to this topicStart new topic

open a second form?

virusinfektion
post 3 Oct, 2008 - 01:25 PM
Post #1


New D.I.C Head

*
Joined: 11 Aug, 2008
Posts: 13

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:

CODE
        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: 3 Oct, 2008 - 01:27 PM
User is offlineProfile CardPM

Go to the top of the page

jacobjordan
post 3 Oct, 2008 - 01:38 PM
Post #2


class Me : Perfection

Group Icon
Joined: 11 Jun, 2008
Posts: 1,105



Thanked 22 times

Dream Kudos: 1575
My Contributions


You must first create an instance of your form to show it. Use this code:
csharp

private void button1_Click(object sender, EventArgs e)
{

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

}
}
User is offlineProfile CardPM

Go to the top of the page

virusinfektion
post 3 Oct, 2008 - 01:56 PM
Post #3


New D.I.C Head

*
Joined: 11 Aug, 2008
Posts: 13

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: 3 Oct, 2008 - 01:57 PM
User is offlineProfile CardPM

Go to the top of the page

jacobjordan
post 3 Oct, 2008 - 01:59 PM
Post #4


class Me : Perfection

Group Icon
Joined: 11 Jun, 2008
Posts: 1,105



Thanked 22 times

Dream Kudos: 1575
My Contributions


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.
User is offlineProfile CardPM

Go to the top of the page

virusinfektion
post 3 Oct, 2008 - 02:11 PM
Post #5


New D.I.C Head

*
Joined: 11 Aug, 2008
Posts: 13

I do have a form2. Its not working
User is offlineProfile CardPM

Go to the top of the page

jacobjordan
post 3 Oct, 2008 - 02:13 PM
Post #6


class Me : Perfection

Group Icon
Joined: 11 Jun, 2008
Posts: 1,105



Thanked 22 times

Dream Kudos: 1575
My Contributions


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.
User is offlineProfile CardPM

Go to the top of the page

virusinfektion
post 3 Oct, 2008 - 02:21 PM
Post #7


New D.I.C Head

*
Joined: 11 Aug, 2008
Posts: 13

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 smile.gif

This post has been edited by virusinfektion: 3 Oct, 2008 - 02:24 PM
User is offlineProfile CardPM

Go to the top of the page

jacobjordan
post 3 Oct, 2008 - 02:23 PM
Post #8


class Me : Perfection

Group Icon
Joined: 11 Jun, 2008
Posts: 1,105



Thanked 22 times

Dream Kudos: 1575
My Contributions


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?
User is offlineProfile CardPM

Go to the top of the page

eclipsed4utoo
post 4 Oct, 2008 - 03:55 PM
Post #9


D.I.C Regular

Group Icon
Joined: 21 Mar, 2008
Posts: 314



Thanked 17 times

Dream Kudos: 25
My Contributions


QUOTE(virusinfektion @ 3 Oct, 2008 - 05: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



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

CODE

private void button1_Click(object sender, EventArgs e)
        {

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

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/22/08 04:44AM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month