如何调试C#Windows窗体应用程序中的问题?
我必须制作一个客户信息系统,该系统应具有以下菜单项:如何调试C#Windows窗体应用程序中的问题?,c#,winforms,add,edit,C#,Winforms,Add,Edit,我必须制作一个客户信息系统,该系统应具有以下菜单项: 添加新客户 编辑 删除 获取客户的信息 应该有两个表单:注册表单必须包含四个文本框-用于客户的姓名、姓氏、地址和电子邮件,以及一个列表框,在选择add menustriptool后,我们应该在其中输出客户的姓名和姓氏。此外,当我们添加更多信息时,我们应该将其保存到文本文件中 第二个表单应该有一个标签,我们应该通过从文件中读取来输出客户机信息。 这是我的密码 private void addToolStripMenuItem_Click(o
- 添加新客户
- 编辑
- 删除
- 获取客户的信息
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();
}
}
}
}
}
private void addToolStripMenuItem\u单击(对象发送方,事件参数e)
{
使用(FileStream fs=newfilestream(“clients.txt”,FileMode.Append))
{
使用(StreamWriter sw=新StreamWriter(fs))
{
string name=textBox1.Text.Trim();
string lastname=textBox2.Text.Trim();
字符串地址=textBox5.Text.Trim();
string email=textBox7.Text.Trim();
sw.WriteLine(姓名+”、“+姓氏+”、“+地址+”、“+电子邮件);
}
}
}
列表名称=新列表();
List lastnames=新列表();
列表地址=新列表();
列表电子邮件=新列表();
私有void editToolStripMenuItem\u单击(对象发送方,事件参数e)
{
字符串newname=textBox1.Text;
字符串newlastname=textBox2.Text;
字符串newaddress=textBox5.Text;
字符串newemail=textBox7.Text;
名称[listBox1.SelectedIndex]=新名称;
lastnames[listBox1.SelectedIndex]=newlastname;
地址[listBox1.SelectedIndex]=新地址;
电子邮件[列表框1.SelectedIndex]=新电子邮件;
删除(“clients.txt”);
使用(FileStream fs=newfilestream(“clients.txt”,FileMode.Append))
{
使用(StreamWriter sw=新StreamWriter(fs))
{
for(int i=0;i
我遇到的问题如下:
我不会直接修复你的代码,但我可以给你一些建议,让你更容易做你需要的事情 持有信息 目前,您将所有客户信息保存在单独的列表中,这样做是可行的,但随着您添加更多细节,处理起来会变得烦人,并使保存稍微困难一些。为什么不创建一个为您保存所有客户端信息的类,例如:
private class CustomerInformation
{
public string Name { get; set; }
public string LastName { get; set; }
public string Address { get; set; }
public string Email { get; set; }
}
使用这个简单的类,您将所有客户信息放在一个地方,然后从列表中获取该类,而不是从单个列表中获取每个字符串list customer=new list()代码>
保存
因此,您需要手动创建一个文本文件,并逐步执行该文件,然后提取保存的行并将其拆分以重建数据。您可以使用with将内存中的数据保存到磁盘,而无需编程。这使得它不太容易出错,因为它将适应您提供给它的数据,而不做任何更改,并使您的程序更易于维护,因为您的需求发生了变化。非常简单,在MSDN站点上有很多很好的例子
有了这个新的CustomerInformation
类,您可以简单地请求它为您加载数据,然后在需要加载数据时指向seri