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.IO;
namespace 课程设计
{
public partial class LoginForm : Form
{
public LoginForm()
{
InitializeComponent();
}
/// <summary>
/// 各文件路径编写成字段
/// </summary>
private string currentCode; //验证码字段
string stuUserPath = @"usersS.txt";
string teaUserPath = @"usersT.txt";
string stuInfoPath = @"StudentInf\";
string teaInfoPath = @"TeacherInf\";
/// <summary>
/// 读取用户信息
/// </summary>
private void LoadUsers()
{
//学生用户读取
string[] readStuTxt = File.ReadAllLines(stuUserPath); //读取学生用户文件
{
if (readStuTxt.Length != 0)
{
string[] stuUsernames = readStuTxt[0].Split(','); //按照规定储存,第一行储存用户名即学号
string[] stuPasswords = readStuTxt[1].Split(','); //第二行储存密码
for (int i = 0; i <= stuUsernames.Length; i++)
{
Users tempUser = new Users(stuUsernames[i], stuPasswords[i]); //实例化学生对象,每个用户实例化一个User类对象
StaticUsers.usersStu.Add(tempUser); //储存到相应的静态变量中
}
}
}
//教师用户读取
string[] readTeaTxt = File.ReadAllLines(teaUserPath); //读取教师用户文件
{
if (readTeaTxt.Length != 0)
{
string[] teaUsernames = readTeaTxt[0].Split(','); //按照规定储存,第一行储存用户名即工号
string[] teaPasswords = readTeaTxt[1].Split(','); //第二行储存密码
for (int i = 0; i <= teaUsernames.Length; i++)
{
Users tempUser = new Users(teaUsernames[i], teaPasswords[i]); //实例化教师对象,每个用户实例化一个User类对象
StaticUsers.usersTea.Add(tempUser); //储存到相应的静态变量中
}
}
}
}
/// <summary>
/// 读取学生信息
/// </summary>
/// <param name="username">用户名</param>
/// <returns>学生信息实体</returns>
private StudentInfor LoadStuInfo(string username)
{
string infoPath = stuInfoPath + username + ".txt"; //根据用户名,找到相应学生个人信息文件即路径+username
string[] readStuTxt = File.ReadAllLines(infoPath); //将其读取成字符串数组
if (readStuTxt.Length != 0) //判断数组长度是否为0
{
//如果不为0,则读取后数组每一项即为一种信息,获取所有的信息,利用StudentInfor类的构造函数实例化学生对象,并将其返回
string stuNum = readStuTxt[0];
string name = readStuTxt[1];
DateTime birthDay = DateTime.Parse(readStuTxt[2]); //将其字符串数据类型转换为DataTime类型
string people = readStuTxt[3];
int age = int.Parse(readStuTxt[4]); //将其字符串数据类型转换为int整数类型
int gender = int.Parse(readStuTxt[5]);
int polit = int.Parse(readStuTxt[6]);
int college = int.Parse(readStuTxt[7]);
string mager = readStuTxt[8];
int mFeat = int.Parse(readStuTxt[9]);
int enSchYear = int.Parse(readStuTxt[10]);
int gradYear = int.Parse(readStuTxt[11]);
int grade = int.Parse(readStuTxt[12]);
string introduce = readStuTxt[13];
string[] isChecked = readStuTxt[14].Split(',');
List<bool> courseChecked = new List<bool>();
foreach (string isChe in isChecked)
{
bool tempCheck = bool.Parse(isChe);
courseChecked.Add(tempCheck);
}
StudentInfor stuInfo = new StudentInfor(stuNum, mager, mFeat, enSchYear, gradYear, grade, name, birthDay, people, age, gender, polit, college, introduce, courseChecked);
return stuInfo;
}
else
{
return null;
}
}
/// <summary>
/// 读取教师信息
/// </summary>
/// <param name="username">用户名</param>
/// <returns>教师信息实体</returns>
private TeacherInfor LoadTeaInfo(string username)
{
string infoPath = teaInfoPath + username + ".txt"; //根据用户名,找到相应教师个人信息文件即路径+username
string[] readTeaTxt = File.ReadAllLines(infoPath); //将其读取成字符串数组
if (readTeaTxt.Length != 0)
{
//如果不为0,则读取后数组每一项即为一种信息,获取所有的信息,利用StudentInfor类的构造函数实例化学生对象,并将其返回
string teaNum = readTeaTxt[0];
string name = readTeaTxt[1];
DateTime birthDay = DateTime.Parse(readTeaTxt[2]);
string people = readTeaTxt[3];
int age = int.Parse(readTeaTxt[4]);
int gender = int.Parse(readTeaTxt[5]);
int polit = int.Parse(readTeaTxt[6]);
int college = int.Parse(readTeaTxt[7]);
string group = readTeaTxt[8];
int enSchYer = int.Parse(readTeaTxt[9]);
int degree = int.Parse(readTeaTxt[10]);
string manger = readTeaTxt[11];
string gradSchool = readTeaTxt[12];
string introduce = readTeaTxt[13];
string[] isChecked = readTeaTxt[14].Split(',');
List<bool> courseChecked = new List<bool>();
foreach (string isChe in isChecked)
{
bool tempCheck = bool.Parse(isChe);
courseChecked.Add(tempCheck);
}
TeacherInfor teaInfo = new TeacherInfor(teaNum, group, degree, enSchYer, gradSchool, manger, name, birthDay, people, age, gender, polit, college, introduce, courseChecked);
return teaInfo;
}
else
{
return null;
}
}
/// <summary>
/// 私有方法,即绘制验证码算法DrawCode()
/// </summary>
private void DrawCode()
{
Random r = new Random();
string str = null;
for (int i = 0; i < 5; i++)
{
int rnumber = r.Next(0, 10);
str += rnumber;
}
this.currentCode = str;
//创建GDI
Bitmap map = new Bitmap(150, 40);
Graphics gp = Graphics.FromImage(map);
//字体样式和颜色的数组
string[] fonts = { "微软雅黑", "黑体", "仿宋", "隶书" };
Color[] colors = { Color.Yellow, Color.Black, Color.Red, Color.Green };
//验证码
for (int i = 0; i < 5; i++)
{
Point p = new Point(i * 17, 0);
Font fon = new Font(fonts[r.Next(0, 4)], 20, FontStyle.Bold);
Brush br = new SolidBrush(colors[r.Next(0, 4)]);
gp.DrawString(str[i].ToString(), fon, br, p);
}
//划线
for (int i = 0; i < 20; i++)
{
Point p1 = new Point(r.Next(0, map.Width), r.Next(0, map.Height));
Point p2 = new Point(r.Next(0, map.Width), r.Next(0, map.Height));
gp.DrawLine(new Pen(Brushes.Green), p1, p2);
}
//画像素颗粒
for (int i = 0; i < 400; i++)
{
Point p1 = new Point(r.Next(0, map.Width), r.Next(0, map.Height));
map.SetPixel(p1.X, p1.Y, Color.Black);
}
//镶嵌到picturebox
pictureBox.Image = map;
}
private void reClear()
{
textBoxUsername.Clear(); //清空用户名文本框
textBoxPassword.Clear(); //清空密码文本框
textBoxTestCode.Clear(); //清空验证码文本框
radioBtnStu.Checked = false; //老师和学生的身份也均不选中
radioBtnTea.Checked = false;
}
/// <summary>
/// 窗体加载事件
/// </summary>
private void LoginForm_Load(object sender, EventArgs e)
{
StaticTools.loginform = this;
DrawCode(); //窗体加载的时候,则更新验证码信息
LoadUsers(); //调用已经实现的载入用户方法载入已有的用户名,密码信息
}
/// <summary>
/// PictureBox点击事件
/// </summary>
private void pictureBox_Click(object sender, EventArgs e)
{
DrawCode();
}
/// <summary>
/// 注册按钮点击事件
/// </summary>
private void btnRegister_Click(object sender, EventArgs e)
{
RegisterForm registerForm = new RegisterForm();
registerForm.ShowDialog();
}
/// <summary>
/// 登陆按钮单机事件
/// </summary>
private void btnEnter_Click(object sender, EventArgs e)
{
if (radioBtnStu.Checked||radioBtnTea.Checked) //首先判断用户是否选择身份
{
//其次判断用户填写的验证码是否正确
if(textBoxTestCode.Text!=currentCode)
{
MessageBox.Show("验证码输入错误");
reClear();
}
else
{
//最后获取用户填写的用户名和密码,并判断其是否正确
string username = textBoxUsername.Text;
string password = textBoxPassword.Text;
if(radioBtnStu.Checked) //判断用户身份选择的身份信息
{
//如果是学生
int flag = 0; //用户名是否存在的标识变量
foreach(Users users in StaticUsers.usersStu)
{
if(users.UserName==username)
{
flag = 1; //标明学生信息库里存在该学生的相关信息
if(users.Password==password)
{
MessageBox.Show("登陆成功");
StudentInfor stuInfo = new LoadStuInfo(username);//调用读取学生个人信息方法之前实现的LoadStuInfo方法,传入username返回学生对象
MainFormS main = new MainFormS(stuInfo, username); //学生对象作为参数实例化学生信息管理窗体
main.Show(); //显示窗口
this.Visible = false; //登陆窗口隐藏
}
else
{
MessageBox.Show("密码不正确");
}
}
}
if(flag==0)
{
MessageBox.Show("用户名不存在");
}
}
else
{
//如果是老师
int flag = 0; //用户名是否存在的标识变量
foreach (Users users in StaticUsers.usersTea)
{
if (users.UserName == username)
{
flag = 1; //标明学生信息库里存在该教师的相关信息
if (users.Password == password)
{
MessageBox.Show("登陆成功");
TeacherInfor teaInfo = new LoadTeaInfo(username);//调用读取教师个人信息方法之前实现的LoadTeaInfo方法,传入username返回学生对象
MainFormT main = new MainFormT(teaInfo, username); //教师对象作为参数实例化学生信息管理窗体
main.Show(); //显示窗口
this.Visible = false; //登陆窗口隐藏
}
else
{
MessageBox.Show("密码不正确");
}
}
}
if (flag == 0)
{
MessageBox.Show("用户名不存在");
}
}
}
}
else
{
MessageBox.Show("请选择你的身份");
}
}
}
}
这段代码会报未能找到类型或命名空间名的错误,具体在登陆按钮的单机事件中,请问是哪里出了问题?
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
1条回答 默认 最新
- CrisYoung 2021-12-27 20:53关注
256行和284行的new删了,你这里是直接调用私有方法LoadStuInfo()和LoadTeaInfo(),不能用new
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥15 QQ邮箱过期怎么恢复?
- ¥15 登录他人的vue项目显示服务器错误
- ¥15 (标签-android|关键词-app)
- ¥60 如何批量获取json的url
- ¥15 comsol仿真压阻传感器
- ¥15 Python线性规划函数optimize.linprog求解为整数
- ¥15 llama3中文版微调
- ¥15 pg数据库导入数据序列重复
- ¥15 三分类机器学习模型可视化分析
- ¥15 本地测试网站127.0.0.1 已拒绝连接,如何解决?(标签-ubuntu)