您的位置:首页 > 其它

在txt文件里进行查询(winform案例简单)

2010-12-20 17:04 253 查看


如上图所示布局,打开成绩单按钮必须先点击一下才能查询某个人的成绩,其功能主要是为了获取openfiledialog的filename即打开文件的地址。

打开成绩单按钮代码:

if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
string filename = openFileDialog1.FileName;
}


查询按钮代码:

//用户在点击成绩单ok的情况下
string filename = openFileDialog1.FileName;
//判断用户有没有打开查询文件
if (string.IsNullOrEmpty(filename))
{
MessageBox.Show("没有打开查询文件");
}
else
{
//判断用户是否输入空字段
if (string.IsNullOrEmpty(txtName.Text.Trim()))
{
MessageBox.Show("请输入姓名");
}
else
{
string[] lines = File.ReadAllLines(filename);
//判断是否查到结果
bool find = false;
foreach (var line in lines)
{
string[] strs = line.Split('|');
string name = strs[0];
string scores = strs[1];
if (name == txtName.Text.Trim())
{
MessageBox.Show(name + "的成绩为:" + scores);
find = true;
//之后还有代码要执行,所以不能return
break;
}
}
if (find == false)
{
MessageBox.Show("没有查到此人分数");
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐