简体   繁体   中英

Multiple Controls with Multiple Lists C#

First time posting, hope I'm not duplicating a question already asked, I've done several searches and have re-written my code multiple times to no avail. My issue comes from multiple angles. Some background info on my program.

I'm essentially creating a customer database that stores objects of the type Customer in a list CustomerEntry, and objects of the type InspectionReport in a list ReportList. I then use a ListView to display the CustomerList Items in detail view to for selection. From this point the user can add more customers, view selected customers, delete customers, add reports and view reports(once added).

My issue is that once a report has been selected in a ComboBox and is selected to be viewed it's viewing the wrong item in the list.

The trouble area's of my Code are as Follows:

    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();
    }

What I would like to know is; Is there a better way to display a selected item from a ComboBox that would link to a specific non-databound list item.

for example, I have customer[1] selected, there are 10 reports in the report list, 4 of which belong to said customer, but when I select, say, the first report in the comboBox, instead of giving me the Item from ReportList[0], it gives me say ReportList[4], this being the first report for the selected customer.

Thanks in advance.

So I've made this temporary fix, for now which seems to work, but I'm still open to better suggestions.

first I added a new member variable to the inspection report class, as follows:

    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;
        }
    }

I then made it so that the "complexID" would be used as the displayed text in the comboBox.

    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++;
                    }
                }
            }
        }
    }

Then I changed the parameter for my populate method to use the "complexID" value. I also did a backhanded method of basically cycling through my "ReportList" until it finds the report with the matching "complexID" and sets my output boxes to that report's values.

    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++;
        }
    }

Here, you can see that I now set selIndex to the text that is selected, which is the same as the "complexID" value.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM