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:
thank youCode: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(); });
Its only funny till someone gets hurt.... THEN ITS HILARIOUS
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:
After that make a method to say where and what form you want to openCode:Using system.threading;
Once you done that all you need to do is call the formCode:public static void NewForm() { Application.Run(new Form1()); }
I called here the thread s but you can give it any name you want ofc.Code:private void button1_Click(object sender, EventArgs e) { Thread s = new Thread(new ThreadStart(NewForm)); s.Start(); this.close(); }
Last edited by Darkco; 01-18-2010 at 05:51 AM. Reason: forgot this.close :P
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![]()
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:
"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:Code:public Main() { InitializeComponent(); Login loginForm = new Login(); loginForm.ShowDialog(); }
True //meaning that login was correct
Username //in case you need it in main form.
To do 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:using System.IO; StreamWriter Temporary= new StreamWriter("Path"); Temporary.WriteLine("Log in Correct"); Temporary.WriteLine("John"); Temporary.Close(); //dont forget this!
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.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 }
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
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks