Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/265.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 对如何迭代此集合感到困惑_C#_Winforms_Combobox - Fatal编程技术网

C# 对如何迭代此集合感到困惑

C# 对如何迭代此集合感到困惑,c#,winforms,combobox,C#,Winforms,Combobox,我要做的是获取所有可用的网络适配器 所以我有一个名为Adapters的类,它由两个变量组成,第一个变量是 注册表项,第二个是 保留适配器名称,如(无线、本地等),这是我的代码 List<Adapters> GetAdapterNames(string regPath) { List<Adapters> list = new List<Adapters>(); RegistryKey key = Roo

我要做的是获取所有可用的网络适配器 所以我有一个名为Adapters的类,它由两个变量组成,第一个变量是 注册表项,第二个是 保留适配器名称,如(无线、本地等),这是我的代码

  List<Adapters> GetAdapterNames(string regPath)
      {
          List<Adapters> list = new List<Adapters>();


          RegistryKey key = RootNode(regPath, false);
          if (key != null)
          {
              string[] par = key.GetSubKeyNames();

              foreach (string node in par)
              {

                  if (node != "Descriptions")
                  {

                      RegistryKey keys = RootNode(regPath+"\\"+node + "\\Connection", false);
                      string name = keys.GetValue("Name").ToString();

                      list.Add(new Adapters(name,node));


                  }

      }
     return list;
问题是我如何将这个列表交给combobax并在其中循环。 像这样的

 private const string ADAPTER_PATH =
            @"SYSTEM\ControlSet001\Control\Network\{4D36E972-E325-11CE-BFC1-08002BE10318}";
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
         List<Adapters>adapters= GetAdapterNames(ADAPTER_PATH);


           combobax.valueMember=//list.name
 combobax.displayMember=list.node;


        }
private const字符串适配器\u路径=
@“SYSTEM\ControlSet001\Control\Network\{4D36E972-E325-11CE-BFC1-08002BE10318}”;
公共表格1()
{
初始化组件();
}
私有void Form1\u加载(对象发送方、事件参数e)
{
Listadapters=GetAdapterNames(适配器路径);
combobax.valueMember=//list.name
combobax.displayMember=list.node;
}

根据您得到的示例(请参阅以获取有关如何在您的问题中提供好的代码示例的建议),以下方法可行:

private void Form1_Load(object sender, EventArgs e)
{
    List<Adapters>adapters= GetAdapterNames(ADAPTER_PATH);

    combobax.DataSource = adapters;
    combobax.ValueMember = "Name";
    combobax.DisplayMember = "Val";
}
private void Form1\u加载(对象发送方,事件参数e)
{
Listadapters=GetAdapterNames(适配器路径);
combobax.DataSource=适配器;
combobax.ValueMember=“Name”;
combobax.DisplayMember=“Val”;
}
您可以将
IList
的任何实现分配给
组合框的
数据源
属性

ValueMember
DisplayMember
属性可分别用于控制从
SelectedValue
属性返回的值以及在
组合框中显示的字符串。它们是
字符串
值,包含用于填充
组合框
的对象类的属性名称

注:

  • 我不知道
    combobax
    是否是表单中包含对
    ComboBox
    对象引用的字段的正确名称。这是你输入的,所以这是我留下的
  • 显示节点注册表项名称并使用“友好”适配器名称作为值成员似乎有点奇怪。但同样,这是您键入的内容,这就是我在示例中显示的内容

哪个组合框?WPF、WinForms、ASP.NET?只是WinForms的朋友们,请仔细看看我们是如何/为什么使用标记的。
private void Form1_Load(object sender, EventArgs e)
{
    List<Adapters>adapters= GetAdapterNames(ADAPTER_PATH);

    combobax.DataSource = adapters;
    combobax.ValueMember = "Name";
    combobax.DisplayMember = "Val";
}