题目:
上面这个题目很长,设计的内容包括如何判断有没有存在的文件、单选的使用、多选的使用、信息框框起来很大程度也是进行单选的分割,比如A框里面有两个选项,B框里面有三个选项,这样就不会出现一个窗体上只能选一个的现象,而是在每个框中都能选择一个单选。
还有的控件就是comboBox,这个下拉的样式设置。
界面:
代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Text;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.Button;
namespace _2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//在组合框中添加内容
comboBox1.Items.Clear();
comboBox1.Items.Add("群众");
comboBox1.Items.Add("共青团员");
comboBox1.Items.Add("预备党员");
comboBox1.Items.Add("党员");
}
private void textBox3_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar != 8 && !Char.IsDigit(e.KeyChar))//设置只能输入数字
{
e.Handled = true;
}
}
private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar != 8 && !Char.IsDigit(e.KeyChar))//设置只能输入数字
{
e.Handled = true;
}
}
//读取文件时以:进行分割,获取内容
private string GetValue(string line)
{
string[] parts = line.Split(':');
return parts.Length > 1 ? parts[1].Trim() : string.Empty;
//当数组长度大于1时,则返回parts数组里的第二个元素,否则为空
}
private void buttonsearch_Click(object sender, EventArgs e)//检索按钮
{
string name=textBox1.Text.Trim();
string xuehao=textBox2.Text.Trim();
//string A=name+xuehao; //字符串可以用这个方式进行相加
if(string.IsNullOrEmpty(name)||string.IsNullOrEmpty(xuehao))
{
MessageBox.Show("请输入姓名和学号!","提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
//创建、复制、删除、移动、打开文件的类FileInfo
FileInfo myfileinfo = new FileInfo(@"C:\Users\Lin\Desktop\" + textBox1.Text + textBox2.Text + ".txt");
Console.WriteLine(myfileinfo.FullName); // 输出 filePath 的值
if (myfileinfo.Exists)
{
string[] lines = File.ReadAllLines(myfileinfo.FullName);
textBox年龄.Text = GetValue(lines[2]);
comboBox1.SelectedItem = GetValue(lines[3]); // 政治面貌
textBox地址.Text = GetValue(lines[4]); // 家庭住址
radioButton男生.Checked = GetValue(lines[5]) == "男";
radioButton女生.Checked = GetValue(lines[5]) == "女";
//if (lines[1].Trim() == "群众")//用lines[1]读不到是因为一行后面有空格,现在可以读到了,但是这种方法不太适合多选项
// comboBox1.SelectedItem = "群众";
//else if (lines[1].Trim() == "共青团员")
// comboBox1.SelectedItem = "共青团员";
//else if (lines[1].Trim() == "预备党员")
// comboBox1.SelectedItem = "预备党员";
//else if (lines[1].Trim() == "党员")
// comboBox1.SelectedItem = "党员";
//话说这里是不是不需要这么麻烦,可以直接用 comboBox1.SelectedItem =lines[1].Trim();
string filecontent =File.ReadAllText(myfileinfo.FullName);//这个方法挺好的,单选多选都能用
//if(filecontent.Contains("群众"))
// comboBox1.SelectedItem = "群众";
//else if(filecontent.Contains("共青团员"))
// comboBox1.SelectedItem = "共青团员";
//else if (filecontent.Contains("预备党员"))
// comboBox1.SelectedItem = "预备党员";
//else
// comboBox1.SelectedItem = "党员";
//textBox地址.Text = lines[2];
//if (lines[3] == "男")
// radioButton男生.Checked = true;
//else
// radioButton女生.Checked = true;
if (filecontent.Contains("唱歌"))//这种方法确实好用,但是需要注意对于上下文重复的文本没有办法用
{
checkBox唱歌.Checked = true;
}
else if (filecontent.Contains("绘画"))
{
checkBox绘画.Checked = true;
}
else if (filecontent.Contains("跳舞"))
{
checkBox跳舞.Checked = true;
}
else if (filecontent.Contains("足球"))
{
checkBox足球.Checked = true;
}
else if (filecontent.Contains("排球"))
{
checkBox排球.Checked = true;
}
else if (filecontent.Contains("其他"))
{
checkBox其他.Checked = true;
}
button信息确认.Enabled = true;
}
else
{
if (!string.IsNullOrEmpty(name) && !string.IsNullOrEmpty(xuehao))
{
MessageBox.Show("没有检索到信息,请继续填写", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
button信息确认.Enabled = true;
}
}
}
StringBuilder hobbies = new StringBuilder();
public void hobby()
{
hobbies.Clear();
if (checkBox唱歌.Checked == true)
hobbies.Append("唱歌 ");
if (checkBox绘画.Checked == true)
hobbies.Append("绘画 ");
if (checkBox跳舞.Checked == true)
hobbies.Append("跳舞 ");
if (checkBox足球.Checked == true)
hobbies.Append("足球 ");
if (checkBox排球.Checked == true)
hobbies.Append("排球 ");
if (checkBox其他.Checked == true)
hobbies.Append("其他 ");
}
private void button信息确认_Click(object sender, EventArgs e)
{
bool ifentire = true;//判断是否全部填写完成
//Parse指的是将数字的字符串转为int类型
if (int.Parse(textBox年龄.Text.Trim()) <= 0 || int.Parse(textBox年龄.Text.Trim()) >= 150)
{
MessageBox.Show("请先完成信息填写!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
ifentire =false;
}
if (comboBox1.SelectedItem == null)
{
MessageBox.Show("请先完成信息填写!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
ifentire = false;
}
if (textBox地址.Text == null)
{
MessageBox.Show("请先完成信息填写!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
ifentire = false;
}
if (radioButton男生.Checked == false && radioButton女生.Checked == false)
{
MessageBox.Show("请先完成信息填写!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
ifentire = false;
}
hobby();
//也可以用string.IsNullOrWhiteSpace(hobbies)
if (checkBox唱歌.Checked == false && checkBox绘画.Checked == false && checkBox跳舞.Checked == false
&& checkBox足球.Checked == false && checkBox排球.Checked == false && checkBox其他.Checked == false)
{
MessageBox.Show("请先完成信息填写!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
ifentire = false;
}
if (ifentire)
{
//$允许字符串中插入变量的值 //\r\n是回车换行
textBox信息汇总文本.Text = $"姓名:{textBox1.Text.Trim()}\r\n" +
$"学号:{textBox2.Text.Trim()}\r\n" +
$"年龄:{textBox年龄.Text.Trim()}\r\n" +
$"政治面貌:{comboBox1.SelectedItem}\r\n" +//.ToString()
$"家庭住址:{textBox地址.Text.Trim()}\r\n" +
$"性别:{(radioButton男生.Checked ? "男" : "女")}\r\n" +//这个男女的输出需要注意
$"爱好:{hobbies}";
button信息保存.Enabled = true;
}
}
private void button信息保存_Click(object sender, EventArgs e)
{
string name = textBox1.Text.Trim();
string xuehao = textBox2.Text.Trim();
FileInfo myfileinfo = new FileInfo(@"C:\Users\Lin\Desktop\" + textBox1.Text + textBox2.Text + ".txt");
// 判断文件是否存在
bool fileExists = File.Exists(myfileinfo.FullName);
if (fileExists)
{
StreamWriter writer = new StreamWriter(myfileinfo.FullName);
//writer.WriteLine(textBox年龄.Text.Trim());
//writer.WriteLine(comboBox1.SelectedItem);
//writer.WriteLine(textBox地址.Text.Trim());
//writer.WriteLine(radioButton男生.Checked ? "男" : "女");
//writer.WriteLine(hobbies);
writer.WriteLine(textBox信息汇总文本.Text);
writer.Close(); // 或者调用 writer.Dispose();
//这里一定要关闭文件,不然写不进去。
//与使用 using(StreamWriter writer = new StreamWriter(myfileinfo.FullName))是一样的作用,用了using就不需要写代码关闭了
MessageBox.Show("信息已更改!","提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
StreamWriter writer = new StreamWriter(myfileinfo.FullName);
writer.WriteLine(textBox信息汇总文本.Text);
writer.Close(); // 或者调用 writer.Dispose();
MessageBox.Show("信息已保存!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
button信息确认.Enabled = false;
button信息保存.Enabled = false;
}
}
}
上面的代码中有一些个人不同的尝试,大家也可以看看有没有更好的办法。
代码能直接使用,持续更新,欢迎关注,共勉!