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.Linq;
namespace 第三題
{
public partial class form1 : Form
{
struct User
{
public string name;
public string password;
public string level;
}
User[] userinfo = new User[3];
public form1()
{
userinfo[0].name = "張三";
userinfo[0].password = "123";
userinfo[0].level = "0";
userinfo[1].name = "李四";
userinfo[1].password = "456";
userinfo[1].level = "1";
userinfo[2].name = "王五";
userinfo[2].password = "789";
userinfo[2].level = "1";
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
System.Environment.Exit(0);
}
private void button1_Click(object sender, EventArgs e)
{
String myuse= username.Text;//擷取目前輸入的使用者名
String myPwd = pwd.Text;//擷取目前輸入的密碼
if(myuse==""|| myPwd == "")
{
MessageBox.Show("使用者名和密碼不能為空");
return;//結束
}
int len=userinfo.Length;//目前的使用者數量
int index = 0;//下标
while (index< len)
{
if (myuse.Equals(userinfo[index].name))
{//使用者比對上了
if (myPwd.Equals(userinfo[index].password))
{
Form form2=new Form2();
Program.name = userinfo[index].name;//設定目前名字
//密碼比對上了
if (userinfo[index].level.Equals("0"))//0(為超級管理者使用者)
{
Program.myStatus=true;//顯示一般使用者使用按鈕
form2.Show();
}
if (userinfo[index].level.Equals("1"))//1(為一般管理者使用者)
{
Program.myStatus = false;
form2.Show();
}
return;
}
else
{
MessageBox.Show("密碼錯誤");
//清空密碼
pwd.Text = "";
return;
}
}
index++;
}
MessageBox.Show("沒有此使用者");
}
}
}
源代碼2:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace 第三題
{
internal static class Program
{
/// <summary>
/// 應用程式的主入口點。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new form1());
}
//多個視窗共享資料
public static String name = "";
public static Boolean myStatus = false;
}
}
源代碼3:
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 第三題
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
//設定第一個視窗傳來的資料
myname.Text = "歡迎" + Program.name + "使用";
button2.Visible = Program.myStatus;
}
}
}
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 第四題
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void richTextBox1_TextChanged(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
Program.Status = false;//設定狀态 防止第二個視窗 點×也會增加上一次的内容
Form f2 = new Form2();
f2.ShowDialog();
}
private void Form1_Activated(object sender, EventArgs e)
{
if (Program.Status)
{//當視窗活動時 狀态修改過才會添加
myText.Text += "\n" + Program.myText;
}
Program.Status = false;//設定狀态 防止調用時直至錄入
}
private void Form1_Load(object sender, EventArgs e)
{
myText.Text = "學号" + "\t" + "高數" + "\t" + "英語" + "\t" + "計算機" + "\t" + "平均分";
Program.Status = false;//設定狀态 防止調用時直至錄入
}
}
}
源代碼二:
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 第四題
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void label1_Click(object sender, EventArgs e)
{
}
private void butAge_Click(object sender, EventArgs e)
{
if (textBox1.Text == "") { MessageBox.Show("id不能為空");return; }
Program.myId = int.Parse(textBox1.Text);
if (textBox2.Text == "") { MessageBox.Show("數學成績不能為空"); return; }
Program.mathematics = int.Parse(textBox2.Text);
if (textBox3.Text == "") { MessageBox.Show("英語成績不能為空"); return; }
Program.English = int.Parse(textBox3.Text);
if (textBox4.Text == "") { MessageBox.Show("計算機成績不能為空"); return; }
Program.computer = int.Parse(textBox4.Text);
//計算平均分
Program.avg = (Program.mathematics + Program.English + Program.computer) / 3.0;
textBox5.Text=Program.avg.ToString();//顯示平均分
}
private void butAdd_Click(object sender, EventArgs e)
{
//錄入按鈕
if (textBox5.Text == "") { MessageBox.Show("請計算平均成績"); return; }
//拼接成績
Program.myText= Program.myId + "\t" + Program.mathematics + "\t" + Program.English + "\t" + Program.computer + "\t" + Program.avg;
//設定傳回狀态
Program.Status = true;
Form1 f1 = new Form1();
//退出
this.Close();
}
private void butExit_Click(object sender, EventArgs e)
{
//設定傳回狀态
Program.Status = false;
//退出
this.Close();
}
}
}
源代碼三:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace 第四題
{
internal static class Program
{
/// <summary>
/// 應用程式的主入口點。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
//設定共享資料
public static int myId;//學号
public static int mathematics;//高數
public static int English;//英語
public static int computer;//計算機
public static double avg;//平均分
public static String myText ;//資訊
public static Boolean Status = false;//傳回狀态
}
}