【问题标题】:How to debug problems in a C# Windows form application?如何调试 C# Windows 窗体应用程序中的问题?
【发布时间】: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();
                    }


                }
            }
        }
    }

我遇到的问题如下:

  1. 我不能只编辑一个文本框。例如,我想编辑客户名称,我记下了新名称,但它删除了其他信息。
  2. 删除 - 我只能删除一次。然后它显示错误。
  3. 获取信息- 我只能获取第一个客户信息。当我选择其他客户端时,它会再次输出第一个客户端信息。

【问题讨论】:

  • @UweKeim:我认为现在不鼓励从标题中删除“标签”,因为它会使热门网络问题侧边栏中的标题变得毫无意义。只需重写标题,以便“自制标签”,例如[Winforms] [C#] 被合并到标题中作为自然英语的问题。这个很好,除了请帮忙乞讨。

标签: c# winforms add edit


【解决方案1】:

我不会直接修复你的代码,但我可以给你一些建议,让你更容易做你需要的事情。

持有资料

目前,您将所有客户信息保存在单独的列表中,它可以工作,但随着您向其中添加更多详细信息并使得保存变得更加困难,处理起来会变得很烦人。为什么不创建一个包含所有客户信息的类,例如:

private class CustomerInformation
{
    public string Name { get; set; }
    public string LastName { get; set; }
    public string Address { get; set; }
    public string Email { get; set; }
}

通过这个简单的类,您可以将所有客户信息集中在一个位置,然后您可以从列表中获取该类,而不是从单个列表中获取每个字符串List&lt;CustomerInformation&gt; customer = new List&lt;CustomerInformation&gt;();

保存

因此 atm 您正在手动创建一个文本文件并逐步浏览它,然后获取您保存的行并将它们分开以重建您的数据。您可以使用serialiser 来获取您在内存中的数据并将其保存到磁盘,而无需您对其工作方式进行编程。这使得它不太容易出错,因为它将适应您提供给它的数据而无需更改,并且随着您的需求变化,您的程序更易于维护。 The Binary Serializer 相当简单,在 MSDN 网站上有很多很好的例子。

有了新的CustomerInformation 类,您现在可以简单地要求它为您提供serialise 它,然后当您需要加载该数据时,您将序列化程序指向该文件并要求它为deserialise 它你把它变回你原来输入的数据!

旁注:您的保存代码可能应该是一个单独的方法,因为它在多个地方使用并且应该易于维护! :)

删除

如果您查看删除代码。当您删除客户信息时会发生什么?它是否仍然显示您刚刚删除的客户的信息?我猜是的!

您需要检查您的listBox1.SelectedIndex 是否超出范围...所以if(listBox1.SelectedIndex &lt; 0 || listBox1.SelectedIndex &gt; customer.length),假设您更改了数据以使用之前的类。如果该 if 语句为真,则您不能删除您没有的 List 条目。

此外,由于该客户数据刚刚被删除,您应该将字段留空或显示其他人的数据,最好是前一个选项。您可能还需要从 ListBox 中删除客户,因为他们不再存在。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-02-07
    • 2011-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-28
    相关资源
    最近更新 更多