C#如何在多个列表框中显示项目?

C#如何在多个列表框中显示项目?,c#,asp.net,C#,Asp.net,我们正在做一个学校作业,我们应该做的页面必须能够显示所有客户的完整列表,也能够显示所有来自日德兰的客户名单,所有来自富南的客户名单和所有来自新西兰的客户名单 问题: 系统不会显示特定于某个地区(如日德兰)的客户,而是将以前的客户和新客户(来自不同地区)添加到代表不同地区的同一字段中。因此,系统无法区分客户的各个区域 在上面的屏幕截图中,列表框中只有Stanislav这个名字。但是,塞巴斯蒂安属于另一个地区,因此也添加了另一个列表框。我们相信这可能与foreach循环有关,尽管我们不能完全确定

我们正在做一个学校作业,我们应该做的页面必须能够显示所有客户的完整列表,也能够显示所有来自日德兰的客户名单,所有来自富南的客户名单和所有来自新西兰的客户名单

问题:

系统不会显示特定于某个地区(如日德兰)的客户,而是将以前的客户和新客户(来自不同地区)添加到代表不同地区的同一字段中。因此,系统无法区分客户的各个区域

在上面的屏幕截图中,
列表框中只有Stanislav这个名字。但是,塞巴斯蒂安属于另一个地区,因此也添加了另一个列表框。我们相信这可能与
foreach
循环有关,尽管我们不能完全确定

任何帮助都将不胜感激

namespace _2ndHandinCsharp
{
    public partial class register : System.Web.UI.Page
    {
        private CustomerTank thetank;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Application["SharedCustomerTank"]== null)
            {
                Application["SharedCustomerTank"] = new CustomerTank();
            }

            thetank = (CustomerTank)Application["SharedCustomerTank"];
        }

        protected void ButtonUpdate_Click(object sender, EventArgs e)
        {
            thetank = (CustomerTank)Application["SharedCustomerTank"];
            UpdateCustomerListView();
        }
        private void UpdateCustomerListView()
        {
            ListBox1.Items.Clear();
            List<Customer> customerInTheTank = thetank.TheCustomers();
            foreach(Customer c in customerInTheTank)
            {
                ListBox1.Items.Add(c.ToString());

            }
        }
        private void UpdateCustomerListViewJutland()
        {

            List<Customer> customerInTheTank = thetank.TheCustomers();

                for (int i = 0; i < customerInTheTank.Count; i++)
                {
                        if (DropDownListRegion.SelectedValue == "Jutland")
                        {
                            ListBoxJutland.Items.Add(customerInTheTank[i].Name);
                        }
                }
        }
        private void UpdateCustomerListViewFunen()
        {

            List<Customer> customerInTheTank = thetank.TheCustomers();


                for (int i=0; i<customerInTheTank.Count;i++)
                {
                        if (DropDownListRegion.SelectedValue == "Funen")
                        {
                    ListBoxFunen.Items.Add(customerInTheTank[i].Name);
                        }
                }

        }

        protected void ButtonAddCustomer_Click(object sender, EventArgs e)
        {
            Customer c = new Customer(
                TextBoxName.Text,
                TextBoxPassword.Text,
                int.Parse(TextBoxAge.Text),
                TextBoxZip.Text,
                DropDownListRegion.Text);

            Application.Lock();
            thetank = (CustomerTank)Application["SharedCustomerTank"];
            thetank.AddCustomer(c);
            Application["SharedCustomerTank"] = thetank; 
            Application.UnLock();
            UpdateCustomerListView();
            UpdateCustomerListViewJutland();
            UpdateCustomerListViewFunen();




        }




}
namespace\u 2ndHandinCsharp
{
公共部分类注册:System.Web.UI.Page
{
私人客户银行;
受保护的无效页面加载(对象发送方、事件参数e)
{
if(应用程序[“SharedCustomerTank”]==null)
{
应用程序[“SharedCustomerTank”]=新客户银行();
}
银行=(客户银行)应用程序[“SharedCustomerTank”];
}
受保护的无效按钮更新\单击(对象发送者,事件参数e)
{
银行=(客户银行)应用程序[“SharedCustomerTank”];
UpdateCustomerListView();
}
私有void UpdateCustomerListView()
{
ListBox1.Items.Clear();
列出客户银行=银行。客户();
foreach(客户银行中的客户c)
{
ListBox1.Items.Add(c.ToString());
}
}
私有void UpdateCustomerListViewJutland()
{
列出客户银行=银行。客户();
对于(int i=0;i对于(int i=0;i,看起来您使用了错误的条件:

而不是这个,

for (int i = 0; i < customerInTheTank.Count; i++)
{
    if (DropDownListRegion.SelectedValue == "Jutland")
    {
        ListBoxJutland.Items.Add(customerInTheTank[i].Name);
    }
}
for(int i=0;i
您应该查看客户的实际位置,而不是区域框中的当前选择:

ListBoxJutland.Items.Clear();
for (int i = 0; i < customerInTheTank.Count; i++)
{
    if (customerInTheTank[i].Region == "Jutland")
    {
        ListBoxJutland.Items.Add(customerInTheTank[i].Name);
    }
}
ListBoxJutland.Items.Clear();
对于(int i=0;i
customer类上region属性的名称不包含在代码中,我假设
region


注意,我添加了一个
Clear()
循环前的语句,如果重复调用该函数,将防止客户名称被一次又一次添加。

你说得对。问题在于for each循环。当你检查区域是否为富南或日德兰时,你忘记了检查你添加的客户是否也是来自富南或日德兰这个区域。你只需要一个额外的IF语句条件。类似这样的条件应该可以

            for (int i=0; i<customerInTheTank.Count;i++)
            {
                    if (DropDownListRegion.SelectedValue == "Funen" && customerInTheTank[i].Region == "Funen")
                    {
                        ListBoxFunen.Items.Add(customerInTheTank[i].Name);
                    }
            }

用于(int i=0;如果从操作说明和屏幕截图来看,我认为
dropdownstregion
用于指定新客户的区域,并且不应该影响客户在区域列表中的分布。是的,我认为你是对的。在这种情况下,你的答案更正确,他们只需要检查客户的区域,而不是DropDownlowstregionThank的价值非常感谢这对我们很有帮助,但当我们试图增加更多“客户”时,我们仍然面临这个问题添加新客户时,它会重复以前的客户。就像这里类似于每次单击按钮和客户时清除ListBox1中的项目一样,请确保同时清除ListBoxJutland和ListBoxFunen。