Closed Thread
Results 1 to 4 of 4

Thread: C# noob question 2

  1. #1
    Siten0308's Avatar
    Siten0308 is offline Programming Professional
    Join Date
    Jun 2008
    Location
    California, USA
    Posts
    302
    Rep Power
    16

    Question C# noob question 2

    Hello,

    I have a question, if i created form 1 first, and then created form 2, sad to say when you launch the program, form 1 comes up, but what if since you created form 2 later on but want form 2 to come up first before form 1, how do you do that in Visual studio, what i tried doind to get around is form2 is the login screen, and if successfully logged in using certain credentails, close form2 then open form1, but that never happens, i am usiing anonymouse method, but anyone know how i can do either or? i have the code below:

    Code:
    login lg = new login();
                lg.ShowDialog();
                lg.btn_login.Click += new EventHandler(delegate
                    {
                        if (lg.textBox1.Text == "admin" && lg.textBox2.Text == "pass")
                        {
                            login logging = new login();
                            logging.Close();
                        }
                        else
                        {
                            MessageBox.Show("Incorrect username and/or password");
                            
                        }
                    });
                lg.btn_cancel.Click += new EventHandler(delegate
                    {
                        login lg1 = new login();
                        lg1.Close();
                    });
    thank you
    Its only funny till someone gets hurt.... THEN ITS HILARIOUS

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    Darkco's Avatar
    Darkco is offline Newbie
    Join Date
    Feb 2008
    Location
    Holland
    Posts
    13
    Rep Power
    0

    Re: C# noob question 2

    Well if I did understand your question your making a login script which will open a new form and close the old one (login_frm) yeh ?

    For this you need to make use of the:
    Code:
    Using system.threading;
    After that make a method to say where and what form you want to open
    Code:
    public static void NewForm()
    {
        Application.Run(new Form1());
    }
    Once you done that all you need to do is call the form
    Code:
    private void button1_Click(object sender, EventArgs e)
    
    {
    Thread s = new Thread(new ThreadStart(NewForm));
    
    s.Start();
    this.close();
    }
    I called here the thread s but you can give it any name you want ofc.
    Last edited by Darkco; 01-18-2010 at 05:51 AM. Reason: forgot this.close :P

  4. #3
    lobo521 is offline Learning Programmer
    Join Date
    Jan 2010
    Posts
    57
    Rep Power
    8

    Re: C# noob question 2

    Application.Run(new MdiForm());


    and in constructor

    MdiForm()
    {
    .......

    var loginForm = new LoginForm();
    loginForm.MdiParent = this;
    loginForm.Show();

    }

    When you succesfuly login LoginForm closes and you can use your main form.
    This way you don't need threads. Threads are not for noobs

  5. #4
    Davide's Avatar
    Davide is offline Programming God
    Join Date
    Dec 2009
    Location
    Manchester, UK
    Posts
    507
    Blog Entries
    8
    Rep Power
    11

    Re: C# noob question 2

    Good method for starters

    I made an app that uses "Log in", and this is how:

    When the main form was opened, in the main method i did:

    Code:
    public Main()
            {
                InitializeComponent();
    
                Login loginForm = new Login();
                loginForm.ShowDialog();
            }
    "ShowDialog" will make the log in form appear first. What you need to do now is to check if the username and passes are correct, you do that, and if the username and pass is correct, you make a new temporary text file where you write:

    True //meaning that login was correct
    Username //in case you need it in main form.

    To do this:

    Code:
    using System.IO;
    
    StreamWriter Temporary= new StreamWriter("Path");
    Temporary.WriteLine("Log in Correct");
    Temporary.WriteLine("John");
    Temporary.Close(); //dont forget this!
    Now, after you close the dialog box, in the main for you open the text file and check if the log in was successful:

    Code:
    public Main()
            {
                InitializeComponent();
    
                Login loginForm = new Login();
                loginForm.ShowDialog();
    
                StreamReader Temp=File.OpenText('path');
                string ok=Temp.ReadLine();
                string Username=Temp.ReadLine();
    
               if (ok=="Log in Correct")  //proceed to app
    else Application.Exit(); //This means that the user clicked Cancel instead of Log In, and he didn't inserted any password
            }
    Also, don't forget to initialize the text file so that it won't let people log in on the previous accout by pressing Cancel.

    That's how i did it when i was new to C#, not the best way to do it, but it works, and it is understandable.
    Are you a newbie programmer trying to learn C#? Check out my small tutorial: Visual C# Programming Basics

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. C# noob question 3, list question
    By Siten0308 in forum C# Programming
    Replies: 3
    Last Post: 01-21-2010, 07:41 AM
  2. A question from a noob
    By fiddilydee in forum General Programming
    Replies: 5
    Last Post: 06-23-2009, 07:47 PM
  3. Question from a noob...
    By mcg_999 in forum General Programming
    Replies: 1
    Last Post: 10-29-2008, 02:29 PM
  4. A noob question?
    By MPax in forum C and C++
    Replies: 6
    Last Post: 04-03-2008, 08:27 AM
  5. Noob C question
    By Nousferatou in forum C and C++
    Replies: 4
    Last Post: 09-24-2007, 08:02 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts