2015-07-19 65 views
1

第一次发布,希望我没有复制已经问过的问题,我做了几次搜索,并多次重写我的代码无济于事。我的问题来自多个角度。关于我的程序的一些背景信息。多个控件与多个列表C#

我基本上是创建一个客户数据库,它存储Customer列表中Customer类型的对象和列表ReportList中类型InspectionReport类型的对象。然后我使用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]的Item,它给我说ReportList [4],这是所选客户的第一份报告。

在此先感谢。

+0

,当你调用这个函数populateViewReportTabOutputControls你传递两个参数,并声明函数有一个参数,这意味着你传递错误的索引 –

+0

呀,selIndex在方法调用不应该在那里,这是不重要的。它是从试图做别的事情。但我面临的问题是,使用selIndex2作为我的索引值,因为它与组合框中项目的索引相关,而不是项目在列表中的索引。我想要的可能是将组合框中的项目的索引与ReportList中它们各自的索引绑定的方式。 –

+0

也许你应该通过跨越每一行并检查它的作用(特别是它处理的值)来调试你的代码。通过这种方式,你会发现你的指数不匹配的原因。 –

回答

0

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

第一I增加了一个新成员变量来检查报告类,如下所示:

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