【发布时间】:2017-03-05 12:00:43
【问题描述】:
我要做一个客户信息系统,应该有以下菜单项:
- 添加新客户
- 编辑
- 删除
- 获取客户信息
应该有两种形式:注册表单必须包含四个文本框 - 用于客户的姓名、姓氏、地址和电子邮件,以及一个列表框,我们应该在选择添加菜单栏工具后输出客户的姓名和姓氏。此外,当我们添加更多信息时,我们应该将其保存到文本文件中。
第二种形式应该有一个标签,我们应该通过从文件中读取客户信息来输出它。 这是我的代码..
private void addToolStripMenuItem_Click(object sender, EventArgs e)
{
using (FileStream fs = new FileStream("clients.txt", FileMode.Append))
{
using (StreamWriter sw = new StreamWriter(fs))
{
string name = textBox1.Text.Trim();
string lastname = textBox2.Text.Trim();
string address = textBox5.Text.Trim();
string email = textBox7.Text.Trim();
sw.WriteLine(name + "," + lastname + "," +address+"," + email);
}
}
}
List<string> names = new List<string>();
List<string> lastnames = new List<string>();
List<string> adresses = new List<string>();
List<string> emails = new List<string>();
private void editToolStripMenuItem_Click(object sender, EventArgs e)
{
string newname = textBox1.Text;
string newlastname = textBox2.Text;
string newaddress = textBox5.Text;
string newemail = textBox7.Text;
names[listBox1.SelectedIndex] = newname;
lastnames[listBox1.SelectedIndex] = newlastname;
adresses[listBox1.SelectedIndex] = newaddress;
emails[listBox1.SelectedIndex] = newemail;
File.Delete("clients.txt");
using (FileStream fs = new FileStream("clients.txt", FileMode.Append))
{
using (StreamWriter sw = new StreamWriter(fs))
{
for (int i = 0; i < names.Count; i++)
{
sw.WriteLine(names[i] + "," + lastnames[i]+ "," + adresses[i] + ","+ emails[i]);
}
}
}
}
private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
{
names.RemoveAt(listBox1.SelectedIndex);
lastnames.RemoveAt(listBox1.SelectedIndex);
adresses.RemoveAt(listBox1.SelectedIndex);
emails.RemoveAt(listBox1.SelectedIndex);
File.Move("clients.txt", "../../clients1.txt");
using (FileStream fs = new FileStream("clients.txt", FileMode.Append))
{
using (StreamWriter sw = new StreamWriter(fs))
{
for (int i = 0; i < names.Count; i++)
{
sw.WriteLine(names[i] + "," + lastnames[i] + "," + adresses[i] + "," + emails[i]);
}
}
}
}
public static string namec;
public static string lnamec;
public static string addrc;
public static string emailc;
private void getinfoToolStripMenuItem_Click(object sender, EventArgs e)
{
using (FileStream fs = new FileStream("clients.txt", FileMode.OpenOrCreate))
{
Form2 f2 = new Form2();
using (StreamReader sr = new StreamReader(fs))
{
int br = 0;
while (!sr.EndOfStream)
{
string line = sr.ReadLine();
string[] splitted = line.Split(',');
names.Add(splitted[0]); br++;
lastnames.Add(splitted[1]);
adresses.Add(splitted[2]);
emails.Add(splitted[3]);
for (int i = 0; i < br; i++)
{
namec = splitted[i];
lnamec = splitted[i + 1];
addrc = splitted[i + 2];
emailc = splitted[i + 3];
f2.Show();
}
}
}
}
}
我遇到的问题如下:
- 我不能只编辑一个文本框。例如,我想编辑客户名称,我记下了新名称,但它删除了其他信息。
- 删除 - 我只能删除一次。然后它显示错误。
- 获取信息- 我只能获取第一个客户信息。当我选择其他客户端时,它会再次输出第一个客户端信息。
【问题讨论】:
-
@UweKeim:我认为现在不鼓励从标题中删除“标签”,因为它会使热门网络问题侧边栏中的标题变得毫无意义。只需重写标题,以便“自制标签”,例如
[Winforms] [C#]被合并到标题中作为自然英语的问题。这个很好,除了请帮忙乞讨。