【问题标题】:Linking 4 List Boxes链接 4 个列表框
【发布时间】:2012-05-13 22:13:56
【问题描述】:

首先我对 C# 非常陌生,我正在尝试重新创建我在 Java 中创建的应用程序。

我有 4 个列表框。每个框都将保存 xml 文件中的值列表。

<Year> 的 listBox_year。 listBox_make 为<Make><Model> 的 listBox_model。 listBox_subModel 用于<sub-Model>

假设我将所有年份添加到 listBox_year 中,没有重复年份。假设我单击某一年,它将调出该年的所有汽车品牌。然后我点击Make,它会显示那个年份的那个Make的模型等等......

使用 Java,我能够使用 HashMap 来完成这项工作,我可以拥有多个同名键,并且我可以搜索在这种情况下选择年份的任何键获取所有具有该名称的 Makes 或 Values年份作为键。

这里是 XML 格式

<?xml version="1.0" encoding="utf-8" ?>
<vehicles>

  <Manufacturer>
    <Make>Subaru</Make>
    <Year>2010</Year>
    <Model>Impreza</Model>
    <Sub-Model>2.0i</Sub-Model>
    <Highway>36 MPG highway</Highway>
    <City>27 MPG city</City>
    <Price>$17,495</Price>
    <Description>
      Symmetrical All-Wheel Drive. 
      SUBARU BOXER® engine. 
      Seven airbags standard. 
      >Vehicle Dynamics Control (VDC). 
    </Description>
  </Manufacturer>

  <Manufacturer>
    <Make>Toyota</Make>
    <Year>2012</Year>
    <Model>Supra</Model>
    <Sub-Model>TT</Sub-Model>
    <Highway>22 MPG highway</Highway>
    <City>19 MPG city</City>
    <Price>$48,795</Price>
    <Description>
      16-inch aluminum-alloy wheels.
      6-speaker audio system w/iPod® control.
      Bluetooth® hands-free phone and audio.
      Available power moonroof.
    </Description>
  </Manufacturer>

  <Manufacturer>
    <Make>Subaru</Make>
    <Year>2011</Year>
    <Model>Impreza</Model>
    <Sub-Model>2.0i Limited</Sub-Model>
    <Highway>36 MPG highway</Highway>
    <City>27 MPG city</City>
    <Price>$18,795</Price>
    <Description>
      16-inch aluminum-alloy wheels. 
      6-speaker audio system w/iPod® control. 
      Bluetooth® hands-free phone and audio. 
      Available power moonroof.
    </Description>
  </Manufacturer>

  <Manufacturer>
    <Make>Subaru</Make>
    <Year>2011</Year>
    <Model>Impreza</Model>
    <Sub-Model>2.0i Limited</Sub-Model>
    <Highway>36 MPG highway</Highway>
    <City>27 MPG city</City>
    <Price>$18,795</Price>
    <Description>
      16-inch aluminum-alloy wheels.
      6-speaker audio system w/iPod® control.
      Bluetooth® hands-free phone and audio.
      Available power moonroof.
    </Description>
  </Manufacturer>

</vehicles>

【问题讨论】:

  • 您的问题是什么?您在寻找Dictionary 吗?
  • 我不确定集合被称为什么,但似乎@lcfseth 很快回答了这个问题。

标签: c# xml parsing listbox hashmap


【解决方案1】:

最接近 java hashmap 的类型是 Dictionary。由于您需要拥有多个具有相同密钥的项目,因此我将使用Dictionary&lt;int,List&lt;Item&gt;&gt;。 以下是您可能需要的一些基本功能:

void AddItem(int key, Item i, Dictionary<int,List<Item>> dict)
{
   if (!dict.ContainsKey(key))
   {
      dict.Add(i,new List<Item>());
   }
   dict[key].Add(i);
}

List<Item> GetList(int key)
{
   if (dict.ContainsKey(key))
   {
      return dict[key];
   }
   else
   {
      return new List<Item>(); // can also be null
   }
}

【讨论】:

  • 非常感谢,我会试一试。我有一段时间没上这个网站了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多