1 登陆界面
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace 登陆
{
public partial class Form1 : Form
{
int misscount = 0; //使用类的字段,不能再委托函数中使用局部变量。
public Form1()
{
InitializeComponent();
}
private void btnLogin_Click(object sender, EventArgs e)
{
//int misscount = 0; //局部变量每次运行完毕变量的值会被销毁,而类的字段只要对象不销毁就一直存在。
string username = textUserName.Text.Trim();
string password = textPwd.Text;
if (misscount > 2)
{
MessageBox.Show("错误次数过多,程序自动退出");
Application.Exit();
}
if (username.Equals("admin",StringComparison.OrdinalIgnoreCase) && password == "8888")
{
MessageBox.Show("登陆成功");
}
else
{
MessageBox.Show("登陆失败");
misscount++;
}
}
}
}
2 输出最高成绩
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace 输出最高成绩
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnShow_Click(object sender, EventArgs e)
{
int max = 0 ;
int score = 0;
string maxName = "";
string maxScore = "";
string[] lines = textChengji.Lines;
try
{
foreach (string temp in lines)
{
string[] strs = temp.Split('=');
string name = strs[0];
string strScore = strs[1];
score = Convert.ToInt32(strScore);
if (score > max)
{
max = score;
maxName = name;
maxScore = strScore;
}
}
}
catch (Exception ex)
{
MessageBox.Show("输入有误");
return;
}
textChengji.Text = "最高成绩获得者为" + maxName;
textChengji.AppendText("\r\n最高成绩为" + maxScore);
}
}
}
3 combobox
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace comboBox示例
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//comboBox 有三种dropdownStyle : dropDown 可以下拉与编辑,dropDownList 只可以下拉(常用)
MessageBox.Show(Convert.ToString(comboBox1.SelectedIndex)); //序号
MessageBox.Show(Convert.ToString(comboBox1.SelectedItem)); //选中的字
//combox 在设计模式中点右键编辑项,可以编写下拉菜单
comboBox1.Items.Clear();//清空旧数据
if (!comboBox1.Items.Contains("长沙"))
{
comboBox1.Items.Add("长沙");
}
}
}
}