【问题标题】:c# How do to convert a string into a ListView Controlc#如何将字符串转换为ListView控件
【发布时间】:2015-08-22 05:20:45
【问题描述】:

我有一个数组:

string[] array = new string[] { "listView1", "listView2", "listView3" };

我想清除并将项目添加到那些 ListViews 中

如何将字符串转换为ListView控件?

【问题讨论】:

  • 您是否被困在那里使用字符串数组?在我看来,您最好使用通用列表...
  • listView.ItemsSource = array;这个你试过了吗,应该可以解决这个提议。
  • 你能告诉我怎么做吗
  • 为什么我得到了负面评价???我只是在学习 c# ...抱歉我的问题含糊不清

标签: c# winforms listview


【解决方案1】:

如果你使用的是 windows 窗体,你可以像这样按名称获取控件:

var listView1= (ListView)this.Controls["listView1"];

所以你的数组:

foreach(var item in array)
{
    var listView= (ListView)this.Controls[item];
    // Do your stuff with listView here, for example:
    // listView.Items.Clear();
}

【讨论】:

  • 感谢您的帮助...我修改了它,因为我的控件是嵌套的
【解决方案2】:

感谢您的帮助
我这样做是为了让它工作

string[] array = new string[] { "listView1", "listView2", "listView3" };
foreach (var item in array)
{
    ListView lvw = (ListView)(this.Controls.Find(item, true).First());
    lvw.Items.Clear();
    lvw.Items.Add("Cat");
    lvw.Items.Add("Dog");
    lvw.Items.Add("Mouse");
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-01-19
    • 2010-12-15
    • 1970-01-01
    • 1970-01-01
    • 2021-10-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多