When we use C # to do projects, basically need to make login interface, then today we will take a step-by-step look, if the simple implementation of the login interface, this article gives 2 examples, from Jane into difficult, I hope you can like.
First, let's look at a simple fabrication process.
Open Visual 2010, create a new form, since it is the login window, so that it does not appear to maximize, minimize and drag size function (the previous section has already mentioned how to set the size), even the form's Text property value is "Login window", size arbitrary.
After the form is created, the detailed component layout of the interface begins, mainly by dragging the control on the left and then putting it into the form to define the property value. These are relatively simple.
In the Code response phase, double-click the Login button to enter the code view:
private void Button1_Click (object sender, EventArgs e) { string name = This.textBox1.Text;//Gets the value inside the string p Assword = This.textBox1.Text; if (name. Equals ("admin") && password. Equals ("admin"))//Determine if the account password equals admin { MessageBox.Show ("Login succeeded"); } else { MessageBox.Show ("Login failed! "); } }
Next, let's take a more complicated example.
Requirements:
1. The user name must be a letter.
The qualified user name must be the letter private void Txtname_keypress (object sender, KeyPressEventArgs e) { if (E.keychar >= ' a ' & amp;& E.keychar <= ' z ') | | (E.keychar >= ' A ' && e.keychar <= ' Z ')) { e.handled = false; } else { MessageBox.Show ("username can only be a letter!") "); E.handled = true; } }
2. The cursor enters the text box when the background is blue, the text is white, the cursor leaves the text box, the background is white, the text is black.
Interface:
When the cursor enters the text box, the background is blue and the font is white; //When the cursor leaves the text box, the background is white and the font is black. private void Txtname_enter (object sender, EventArgs e) { txtname.forecolor = Color.White; Txtname.backcolor = Color.Blue; } private void Txtname_leave (object sender, EventArgs e) { txtname.backcolor = Color.White; Txtname.forecolor = Color.Black; }
3. When you enter the username "admin" and password "123", click the "OK" button, the system will pop up a message box to display the correct input, otherwise display the user name or password error message.
private void Btnlogin_click (object sender, EventArgs e) { string userName = txtName.Text; string password = Txtpwd.text; if (UserName = = "Admin" && password = = "123") { MessageBox.Show ("Welcome to the personal billing system! "," landed successfully! ", MessageBoxButtons.OK, MessageBoxIcon.Information); } else { MessageBox.Show ("The user name or password you entered is wrong! "," Login failed! ", MessageBoxButtons.OK, messageboxicon.exclamation); } }
4. Click the Cancel button to clear the input information and position the cursor in the txtname text box.
private void Btncancel_click (object sender, EventArgs e) { txtname.text = ""; Txtpwd.text = ""; Txtname.focus (); }
5. Final interface:
Tip: To set the Image property for a label, to make the picture appear intact, you need to set the label's AutoSize property to False, and then pull the size of the label appropriately. Also note that the ImageAlign property is set to the Middleleft,textalign property set to MiddleRight.
Notice:
(1) ICO: An icon file format for Windows that can store an icon file for a single pattern, multi-size, and multi-color board.
(2) MessageBox: A message box that displays a modal dialog box that contains a system icon, a set of buttons, and a brief application-specific message, such as status or error information. The
(3) button's shortcut key is implemented by setting the Text property to cancel (&c).
(4) The software used for this exercise is visual Studio 2012, and the graphics resources are provided by VS, which is said to be found in the installation folder common7\imagelibrary of VS, which can be downloaded to the official website.