C#如何實現用戶名與密碼登錄
更新時間:2023年07月12日 11:52:34 作者:不想學習只想玩
這篇文章主要介紹了C#如何實現用戶名與密碼登錄問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
C#實現用戶名與密碼登錄
編寫一個C#窗體應用程序,能夠輸入用戶名、密碼,并進行登錄、取消等操作。
登錄界面設計

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace _1_3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
textBox2.PasswordChar = '*';//密碼以星號顯示
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
textBox1.Text = "";//清空文本框內容,下同
textBox2.Text = "";
}
private void button1_Click(object sender, EventArgs e)
{
String st1 = textBox1.Text.Trim();//獲取文本框內容
String st2 = textBox2.Text.Trim();
if(st1=="1808080112"&&st2=="123456")
{
MessageBox.Show("登陸成功!");
}
if (st1!="1808080112"&&st2=="123456")
{
MessageBox.Show("用戶名錯誤!");
textBox1.Text = "";
}
if(st1=="1808080112"&&st2!="123456")
{
MessageBox.Show("密碼錯誤!");
textBox2.Text = "";
}
if(st1!="1808080112"&&st2!="123456")
{
MessageBox.Show("用戶名、密碼錯誤!");
textBox1.Text = "";
textBox2.Text = "";
}
}
}
}效果如下
1.用戶名與密碼均正確


2.用戶名錯誤,密碼正確

3.用戶名正確,密碼錯誤

4.用戶名密碼均錯誤

總結
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

