簡體   English   中英

如何通過刪除與所有列表框關聯的一項來從多個列表框中刪除項?

[英]How do you remove items from several list boxes by removing one item associated with them all?

在此處輸入圖片說明

我想從它的列表框中刪除Server1,我希望它也刪除與它關聯的其他列表框中的所有其他項目。 (“ Server-Domain1”和所有“ Server1-Domain1-CSR”)。 有沒有辦法做到這一點?

要“綁定”我剛剛使用的這些列表框:

domainListBox.Items.Add((serverListBox.SelectedItem) + "-" + (this.domainTextbox.Text));

        csrListBox.Items.Add((domainListBox.SelectedItem) + ("-CSR-1"));
        csrListBox.Items.Add((domainListBox.SelectedItem) + ("-CSR-2"));
        csrListBox.Items.Add((domainListBox.SelectedItem) + ("-CSR-3"));

創建一個封裝Server及其詳細信息的類,例如DomainsCsrs 創建Servers列表並將其綁定到第一個列表框。 然后將其他兩個列表框綁定到第一個列表框的當前選定項目。 最終結果可能如下所示:

serverListBox.ItemSource = Servers;
domainListBox.ItemSource = (serverListBox.SelectedItem as Server).Domains;
csrListBox.ItemSource = (serverListBox.SelectedItem as Server).Csrs;

這使您能夠設置不同的“列表框”數據,而無需編寫很多可能使其無法維護的代碼。

如果您從服務器列表框中選擇服務器,則可以刪除這樣的關聯項(假裝有一些刪除按鈕,您可以從列表框中選擇域,然后單擊刪除按鈕):

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

希望能幫助到你。

好吧,看到您的代碼,刪除服務器時只需執行以下操作:

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

編輯我已經測試過了,應該可以

更好的選擇是將可觀察模式用作

 public class ObservableObject<T> : IList, IListSource
{

    protected BindingSource src = null;
    List<ListControl> Subscribers;
    ObservableCollection<T> Data = new ObservableCollection<T>();

    public bool IsReadOnly
    {
        get
        {
            return false;
        }
    }

    public bool IsFixedSize
    {
        get
        {
            return false;
        }
    }

    public int Count
    {
        get
        {
            return Data.Count;
        }
    }

    public object SyncRoot
    {
        get
        {
            throw new NotImplementedException();
        }
    }

    public bool IsSynchronized
    {
        get
        {
            throw new NotImplementedException();
        }
    }

    public bool ContainsListCollection
    {
        get
        {
            return true;
        }
    }

    object IList.this[int index]
    {
        get
        {
            return Data[index];
        }

        set
        {
            Data[index] = (T)value;
        }
    }

    public T this[int index]
    {
        get
        {
            return Data[index];
        }

        set
        {
            Data[index] = value;
        }
    }

    public ObservableObject()
    {
        Data.CollectionChanged += Domains_CollectionChanged;
        Subscribers = new List<ListControl>();
        src = new BindingSource();
        src.DataSource = Data;


    }
    private void Domains_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
    {
         src.ResetBindings(false);
    }
    public virtual void Subscribe(ListBox ctrl)
    {

        this.Subscribers.Add(ctrl);
        //ctrl.DataBindings.Add(new Binding("SelectedValue", src, "Name",
        //                    true, DataSourceUpdateMode.Never));
        ctrl.DataSource = src;
    }

    public int Add(object value)
    {
        Data.Add((T)value);
        return Data.Count - 1;
    }

    public bool Contains(object value)
    {
        return Data.Contains((T)value);
    }

    public void Clear()
    {
        Data.Clear();
    }

    public int IndexOf(object value)
    {
        return Data.IndexOf((T)value);
    }

    public void Insert(int index, object value)
    {
        Data.Insert(index, (T)value);
    }

    public void Remove(object value)
    {
        Data.Remove((T)value);
    }

    public void RemoveAt(int index)
    {
        Data.RemoveAt(index);
    }

    public void CopyTo(Array array, int index)
    {
        throw new NotImplementedException();
    }

    public IEnumerator GetEnumerator()
    {
        return Data.GetEnumerator();
    }

    public IList GetList()
    {
        return Data;
    }
}

public class BaseModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    private string _name;
    public string Name
    {
        get { return _name; }
        set
        {
            this._name = value;

            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs("Name"));
            }
        }
    }
}

public class CSR : BaseModel
{

    public override string ToString()
    {
        return Name;
    }


}
public class Domain : BaseModel
{
    public ObservableObject<CSR> CSRs { get; set; }


    public Domain()
    {
        CSRs = new ObservableObject<CSR>();
    }
    public override string ToString()
    {
        return Name;
    }

}
public class Server : BaseModel
{

    public ObservableObject<Domain> Domains { get; set; }

    public Server()
    {
        Domains = new ObservableObject<Domain>();
    }
    public override string ToString()
    {
        return Name;
    }


}
public class DataModel : BaseModel
{
    public ObservableObject<Server> Servers
    {
        get; set;
    }
    public DataModel()
    {
        Servers = new ObservableObject<Server>();
    }
    public override string ToString()
    {
        return Name;
    }


}

在表格上

     DataModel m = new DataModel();
    public Form1()
    {
       ...
        m.Servers.Subscribe(listBox1);
    }
 private void listBox2_Click(object sender, EventArgs e)
    {
        if (listBox2.SelectedIndex >= 0)
        {
            m.Servers[listBox1.SelectedIndex].Domains[listBox2.SelectedIndex].CSRs.Subscribe(listBox3);

        }
    }

    private void listBox1_Click(object sender, EventArgs e)
    {
        if (listBox1.SelectedIndex >= 0)
        {
            m.Servers[listBox1.SelectedIndex].Domains.Subscribe(listBox2);

        }
    }

您無需編寫代碼即可從此角度從表單中刪除項目。從模型中刪除對象

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM