首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何通过删除与所有列表框关联的一项从多个列表框中删除项目?

如何通过删除与所有列表框关联的一项从多个列表框中删除项目?
EN

Stack Overflow用户
提问于 2016-03-11 10:04:39
回答 4查看 62关注 0票数 0

我想从它的列表框中删除Server1,我也希望它删除与它相关的其他列表框中的所有其他项。(“ServerDomain1”和所有"Server1-Domain1-CSR's")。有办法这样做吗?

要“绑定”我刚才使用的这些列表框:

代码语言:javascript
复制
domainListBox.Items.Add((serverListBox.SelectedItem) + "-" + (this.domainTextbox.Text));

代码语言:javascript
复制
        csrListBox.Items.Add((domainListBox.SelectedItem) + ("-CSR-1"));
        csrListBox.Items.Add((domainListBox.SelectedItem) + ("-CSR-2"));
        csrListBox.Items.Add((domainListBox.SelectedItem) + ("-CSR-3"));
EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2016-03-11 10:31:58

如果您从服务器列表框中选择您的服务器,您可以删除类似的相关项(让我们假设有一些删除按钮,从列表框中选择域并单击remove按钮):

代码语言:javascript
复制
private void removeBtn_Click(object sender, EventArgs e)
    {
        List<string> items = csrListBox.Items.Cast<string>().ToList();
        foreach (string item in csrListBox.Items)
        {
            Regex regex = new Regex(@"^" + domainListBox.SelectedItem + @"\w*");
            Match match = regex.Match(item);
            if (match.Success)
            {
                items.Remove(item);
            }
        }

        csrListBox.DataSource = items;
    }

希望能帮上忙。

票数 1
EN

Stack Overflow用户

发布于 2016-03-11 10:30:07

创建一个封装Server及其详细信息(如DomainsCsrs )的类。创建一个Servers列表并将其绑定到第一个列表框。然后将其他两个列表框绑定到第一个列表框的当前选定项。最终结果可能如下所示:

代码语言:javascript
复制
serverListBox.ItemSource = Servers;
domainListBox.ItemSource = (serverListBox.SelectedItem as Server).Domains;
csrListBox.ItemSource = (serverListBox.SelectedItem as Server).Csrs;

这使您能够设置不同的列表框数据,而无需编写大量代码,这些代码可能会使其无法维护。

票数 1
EN

Stack Overflow用户

发布于 2016-03-11 10:24:35

看到您的代码,您只需在删除服务器时执行如下操作:

代码语言:javascript
复制
        string server = serverListBox.SelectedItem as string;
        serverListBox.Remove(server);
        for (int i = domainListBox.Items.Count -1; i >-1; i--)
        {
            if (domainListBox.Items[i].ToString().StartsWith(server))
            {
                string domain = domainListBox.Items[i].ToString();
                domainListBox.Items.RemoveAt(i);
                for (int j = csrListBox.Items.Count-1; j > -1; j--)
                {
                    if (csrListBox.Items[j].ToString().StartsWith(domain))
                    {
                        csrListBox.Items.RemoveAt(j);
                    }
                }
            }
        }

编辑我现在已经测试过了,这应该能用

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35937219

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档