【问题标题】:Multiple Controls with Multiple Lists C#具有多个列表的多个控件 C#
【发布时间】:2015-07-19 06:31:24
【问题描述】:

第一次发帖,希望我没有重复已经提出的问题,我已经进行了几次搜索,并且多次重写了我的代码,但无济于事。我的问题来自多个角度。关于我的程序的一些背景信息。

我实际上是在创建一个客户数据库,该数据库将 Customer 类型的对象存储在列表 CustomerEntry 中,并将 InspectionReport 类型的对象存储在列表 ReportList 中。然后,我使用 ListView 显示 CustomerList Items 的详细视图以供选择。从此用户可以添加更多客户、查看选定客户、删除客户、添加报告和查看报告(一旦添加)。

我的问题是,一旦在 ComboBox 中选择了一份报告并选择查看它,它就会在列表中查看错误的项目。

我的代码的问题区域如下:

    private void ViewReportButton_Click(object sender, EventArgs e)
    {
        int selIndex = CustomerListView.SelectedIndices[0];
        int selIndex2 = AvailableReportsDropDownBox.SelectedIndex;
        tabControl.SelectedTab = tabInspReport;
        populateViewReportTabOutputControls(selIndex, selIndex2);
        activateViewReportTabOutputControls();
    }

    public void populateViewReportTabOutputControls(int selIndex2)
    {
        ViewCustNameLabel.Text = ReportList[selIndex2].getCustomerName();
        ViewDateLabel.Text = Convert.ToString(ReportList[selIndex2].getInspectionDate());
        ViewAddressLabel.Text = ReportList[selIndex2].getCustomerAddress();
        AeratorStatusBox.Text = ReportList[selIndex2].getAeratorStatus();
        FilterStatusBox.Text = ReportList[selIndex2].getFilterStatus();
        HLAFloatStatusBox.Text = ReportList[selIndex2].getHLAFloatStatus();
        OnOffFloatStatusBox.Text = ReportList[selIndex2].getOnOffFloatStatus();
        SprayHeadStatusBox.Text = ReportList[selIndex2].getSprayHeadStatus();
        SludgeLevelOutputBox.Text = Convert.ToString(ReportList[selIndex2].getSludgeLevel());
        InspectorsNameOutputBox.Text = ReportList[selIndex2].getInpsectorName();
    }

我想知道的是;有没有更好的方法来显示 ComboBox 中的选定项目,该项目将链接到特定的非数据绑定列表项。

例如,我选择了客户[1],报告列表中有 10 个报告,其中 4 个属于所述客户,但是当我选择组合框中的第一个报告时,而不是给我ReportList[0] 中的项目,它告诉我 ReportList[4],这是所选客户的第一份报告。

提前致谢。

【问题讨论】:

  • 当你调用这个函数 populateViewReportTabOutputControls 你传递了两个参数并且声明函数有一个参数这意味着你传递了错误的索引
  • 是的,方法调用上的 selIndex 不应该存在,它不重要。这是因为尝试做其他事情。但我面临的问题是,使用 selIndex2 作为我的索引值,因为它与组合框中项目的索引有关,而不是它在列表中的项目的索引。我想要的可能是将组合框中项目的索引与它们在 ReportList 中的相应索引联系起来的方法。
  • 也许你应该只调试你的代码,遍历每一行并检查它的作用(尤其是它处理的值)。通过这种方式,您将找出索引不匹配的原因。
  • 欣赏小费,这始终是我做的第一件事。而且我知道它做错了什么以及为什么。它正在寻找一个合适的、精简编码的工作,就像我在下面发布的那样,这就是问题所在。

标签: c# list class listview combobox


【解决方案1】:

所以我做了这个临时修复,目前看来可行,但我仍然愿意接受更好的建议。

首先我在检查报告类中增加了一个新的成员变量,如下:

    public class InspectionReport
    {
        //there are other unrelated members as well
        private string complexID;
        //constructor would be here
        //getters and setters
        public void setComplexID(string inputCustomerName, DateTime inputInspectionDate)
        {
            complexID = Convert.ToString(inputCustomerName + inputInspectionDate);
        }
        public string getComplexID()
        {
            return complexID;
        }
    }

然后我做了它,以便将“complexID”用作组合框中的显示文本。

    public void fillAvalableReportsDropDownBox()
    {
        if (CustomerListView.SelectedItems.Count > 0) 
        {
            clearAvailableReportsDropDownBox();
            if (CustomerListView.SelectedItems.Count == 0)
            {

            }
            else if (CustomerListView.SelectedItems.Count > 0)
            {
                int selIndex = CustomerListView.SelectedIndices[0];
                if (selIndex >= 0 && selIndex < ReportList.Count)
                {
                    int COUNT = ReportList.Count;
                    int x = 0;
                    while (x < COUNT)
                    {
                        if (ReportList[x].getCustomerName().Contains(CustomerList[selIndex].getFirstName() + " " + CustomerList[selIndex].getLastName()))
                        {
                            AvailableReportsDropDownBox.Items.Add(Convert.ToString(ReportList[x].getComplexID()));
                        }
                        x++;
                    }
                }
            }
        }
    }

然后我将填充方法的参数更改为使用“complexID”值。我还做了一个反手方法,基本上循环浏览我的“ReportList”,直到找到具有匹配“complexID”的报告并将我的输出框设置为该报告的值。

    public void populateViewReportTabOutputControls(string selIndex2)
    {
        int x = 0;
        while (x < ReportList.Count)
        {
            if (ReportList[x].getComplexID() == selIndex2)
            {
                ViewCustNameLabel.Text = ReportList[x].getCustomerName();
                ViewDateLabel.Text = Convert.ToString(ReportList[x].getInspectionDate());
                ViewAddressLabel.Text = ReportList[x].getCustomerAddress();
                AeratorStatusBox.Text = ReportList[x].getAeratorStatus();
                FilterStatusBox.Text = ReportList[x].getFilterStatus();
                HLAFloatStatusBox.Text = ReportList[x].getHLAFloatStatus();
                OnOffFloatStatusBox.Text = ReportList[x].getOnOffFloatStatus();
                SprayHeadStatusBox.Text = ReportList[x].getSprayHeadStatus();
                SludgeLevelOutputBox.Text = Convert.ToString(ReportList[x].getSludgeLevel());
                InspectorsNameOutputBox.Text = ReportList[x].getInpsectorName();
            }
            x++;
        }
    }

在这里,你可以看到我现在将selIndex设置为选中的文本,与“complexID”的值相同。

    private void ViewReportButton_Click(object sender, EventArgs e)
    {
        string selIndex2 = AvailableReportsDropDownBox.SelectedItem.ToString();
        tabControl.SelectedTab = tabInspReport;
        populateViewReportTabOutputControls(selIndex2);
        activateViewReportTabOutputControls();
    }

【讨论】:

    猜你喜欢
    • 2016-05-14
    • 2012-05-02
    • 2023-03-20
    • 1970-01-01
    • 2020-01-20
    • 2016-01-18
    • 1970-01-01
    • 2022-01-07
    • 2020-04-11
    相关资源
    最近更新 更多