美文网首页
C#判断登录是否成功

C#判断登录是否成功

作者: 幻凌风 | 来源:发表于2017-07-21 19:31 被阅读5次

    准备界面元素

    登录界面.jpg

    准备XML数据

    <?xml version="1.0" encoding="utf-8"?>
    <Person>
     <Student StudentID="1">
       <Name>Lucy</Name>
       <Age>18</Age>
       <Gender>男</Gender>
     </Student>
     <Student StudentID="2">
       <Name>Luck</Name>
       <Age>19</Age>
       <Gender>女</Gender>
     </Student>
     <Student StudentID="3">
       <Name>Jack</Name>
       <Age>20</Age>
       <Gender>男</Gender>
     </Student>
     <Student StudentID="4">
       <Name>Tom</Name>
       <Age>21</Age>
       <Gender>男</Gender>
     </Student>
     <Student StudentID="5">
       <Name>Marry</Name>
       <Age>22</Age>
       <Gender>女</Gender>
     </Student>
     <Student StudentID="6">
       <Name>Bill</Name>
       <Age>23</Age>
       <Gender>男</Gender>
     </Student>
    </Person>
    

    根据数据准备类

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace LoginExample
    {
        class Student
        {
            public int ID { get; set; }
            public string Name { get; set; }
            public int Age { get; set; }
            public Char Gender { get; set; }
    
            //重写Equals方法
            public override bool Equals(object obj)
            {
                Student s = obj as Student;
                if (this.ID==s.ID&&this.Name==s.Name&&this.Gender==s.Gender&&this.Age==s.Age)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }
    }
    

    用户输入数据与XML数据比较

    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;
    using System.Xml;
    
    namespace LoginExample
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            //Student对象集合
            List<Student> list = new List<Student>();
            //窗口加载
            private void Form1_Load(object sender, EventArgs e)
            {
               //创建XML对象
                XmlDocument xdoc = new XmlDocument();
                //载入XML数据
                xdoc.Load("Student.xml");
                //获取XML根节点
                XmlElement Person = xdoc.DocumentElement;
                //获取根节点下的子节点
                XmlNodeList xnl = Person.ChildNodes;
                //将子节点数据存入Student对象后放入List集合
                foreach (XmlNode item in xnl)
                {
                    int id = Convert.ToInt32(item.Attributes["StudentID"].Value);
                    String name = item["Name"].InnerText;
                    int age = Convert.ToInt32(item["Age"].InnerText);
                    Char gender = Convert.ToChar(item["Gender"].InnerText);
    
                    Student Stu = new Student();
                    Stu.ID = id;
                    Stu.Age = age;
                    Stu.Name = name;
                    Stu.Gender = gender;
                    list.Add(Stu);
                }
                Console.WriteLine();
            }
    
            private void LoginButton_Click(object sender, EventArgs e)
            {
                //获取登录界面数据
                string name = TexName.Text.Trim();
                int age = Convert.ToInt32(TexAge.Text.Trim());
                int id = Convert.ToInt32(TexID.Text.Trim());
                char gender = RbManButton.Checked ? '男' : '女';
                //将登录对象与List集合中的对象进行比较
                Student LoginStudent = new Student();
                LoginStudent.ID = id;
                LoginStudent.Name = name;
                LoginStudent.Age = age;
                LoginStudent.Gender = gender;
                bool isLogin = false;
                foreach (var item in list)
                {
                    if (item.Equals(LoginStudent))
                    {
                        MessageBox.Show("登录成功!");
                        isLogin = true;
                    }
                }
                if (isLogin==false)
                {
                    MessageBox.Show("登录失败");
                }
            }
        }
    }
    

    运行界面

    运行效果.jpg

    相关文章

      网友评论

          本文标题:C#判断登录是否成功

          本文链接:https://www.haomeiwen.com/subject/ynoekxtx.html