Goal:
Login interface, login error three times to exit the program. Suppose the username password is admin, 888888,
Case-insensitive, (Error-prone: Local variables and class variables)
The value of the variable is destroyed each time the local variable is run, and is reinitialized the next time it is run.
The class field, as long as it is an object, keeps the field values of the object as long as the object is not destroyed.
Exit procedure: This.close (); or application.exit ();
Centralized mode for text boxes: Mutiline (multiple lines), PasswordChar (password)
String Datatime (). ToString () (+3 overloads)
Converts the value of the current System.datatime object to its valid string representation
The source code is as follows:
usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;namespaceLogin Error three times program exit { Public Partial classForm1:form {Private intErrortimes =0; PublicForm1 () {InitializeComponent (); } Private voidButton1_Click (Objectsender, EventArgs e) { stringUsername =TextBox1.Text; stringPassword =TextBox2.Text; if(username. Equals ("Admin", stringcomparison.ordinalignorecase) && Password = ="888888") {MessageBox.Show ("Landing Success! "); } Else { /*Error method ******************************* int timeerrors = 0; timeerrors++; if (timeerrors>= 3) {MessageBox.Show ("Too many errors, the program is about to exit!") "); Application.exit (); } **********************************/ /************************************* * The value of the variable is destroyed each time the local variable is run, and is reinitialized the next time it is run. * And the Class field, as long as it is an object, keeps the field values of the object as long as the object is not destroyed. * ************************************/Errortimes++; if(Errortimes >=3) {MessageBox.Show ("too many errors, the program is about to quit! "); Application.exit ();//Exit Procedure: This.close (); or application.exit ();} MessageBox.Show ("account or password is incorrect, login failed! "); } } Private voidButton2_Click (Objectsender, EventArgs e) { /*string Datatime (). ToString () (+3 overloads) * Converts the value of the current System.datatime object to its valid string representation*/Textbox3.appendtext (DateTime.Now.ToString ()+"\ n"); /** TextBox3.Text = TextBox3.Text + DateTime.Now.ToString () + "\ n"; * Although this may also be used, it is not recommended. */ } }}
Program run: