UserControl i kolekcje w designerze

0

Tworzę sobie kontrolkę która jedną z właściwości jest jako kolekcja. Po dodaniu w desingerze itema na mojej kontrolce tworzy się groupbox i wszystko ok. Zrobiłem to poprzez nadpisanie metody listy generycznej (InsertItem) i reagowaniu na zdarzenie AfterItemInserted poniżej kod. Przy debugowaniu kontrolki wszystko ok. Designer też dobrze działa i jak ustawie sobię pułapki wchodzi ładnie. Idąc tą drogą nadpisałem metodę RemoveItem no i chciałem aby po naciśniećiu w desinerze usuwał mi stworzone groupboxy no i tu pojawił się zonk. A mianowicie po kliknięciu usuń w ogóle nie wchodzi w to zdarzenie i ugrzązłem w martwym punkcie nie wiem co dalej wykombinować. Byłbym wdzięczny za pomoc.

protected override void InsertItem(int index, T item)
{
EventHandler<ItemInsertedArgs<T>> beforehandler = BeforeItemInserted;
EventHandler<ItemInsertedArgs<T>> afterhandler = AfterItemInserted;
EventHandler handlerListChanged = ListChanged;
if (beforehandler != null)
{
beforehandler(this, new ItemInsertedArgs<T>(index, item));
}
base.InsertItem(index, item);
if (afterhandler != null)
{
afterhandler(this, new ItemInsertedArgs<T>(index, item));
}
if (handlerListChanged != null)
{
handlerListChanged(this, null);
}
}

    protected override void RemoveItem(int index)
    {
        EventHandler<ItemRemovedArgs<T>> beforehandler = BeforeItemRemoved;
        EventHandler<ItemRemovedArgs<T>> afterhandler = AfterItemRemoved;
        EventHandler handlerListChanged = ListChanged;
        T item = default(T);
        if (index >= 0)
            item = this[index];
        WRListClass<T> old = this;
        if (beforehandler != null)
        {
            beforehandler(this, new ItemRemovedArgs<T>(index, item));
        }
        base.RemoveItem(index);
        if (afterhandler != null)
        {
            afterhandler(this, new ItemRemovedArgs<T>(index, item));
        }
        if (handlerListChanged != null)
        {
            if (old != this)
                handlerListChanged(this, null);
        }
    }
0

domyslam sie ze uzyles System.Collections.ObjectModel.Collection<T>
twoj RemoveItem wyglada ok, raczej albo odpielo cie sie zdarzenie, albo masz zly kod wywolujacy usuwanie w designerze
jakiego edytora uzyles w designerze?

0

Edytor [EditorAttribute(typeof(System.ComponentModel.Design.CollectionEditor), typeof(System.Drawing.Design.UITypeEditor))]

Poniżej kod kolekcji a jeszcze ponizej kod wlasciwosci

public class WRListClass<T> : Collection<T>
{
public event EventHandler<ItemInsertedArgs<T>> BeforeItemInserted;
public event EventHandler<ItemRemovedArgs<T>> BeforeItemRemoved;
public event EventHandler<ItemSetArgs<T>> BeforeItemSet;
public event EventHandler<ItemInsertedArgs<T>> AfterItemInserted;
public event EventHandler<ItemRemovedArgs<T>> AfterItemRemoved;
public event EventHandler<ItemSetArgs<T>> AfterItemSet;
public event EventHandler ListChanged;

    protected override void InsertItem(int index, T item)
    {
        EventHandler<ItemInsertedArgs<T>> beforehandler = BeforeItemInserted;
        EventHandler<ItemInsertedArgs<T>> afterhandler = AfterItemInserted;
        EventHandler handlerListChanged = ListChanged;
        if (beforehandler != null)
        {
            beforehandler(this, new ItemInsertedArgs<T>(index, item));
        }
        base.InsertItem(index, item);           
        if (afterhandler != null)
        {
            afterhandler(this, new ItemInsertedArgs<T>(index, item));
        }
        if (handlerListChanged != null)
        {
            handlerListChanged(this, null);
        }
    }

    public new bool Remove(T item)
    {
        return base.Remove(item);
    }

    public new void RemoveAt(int index)
    {
        base.RemoveAt(index);
    }

    protected override void RemoveItem(int index)
    {
        EventHandler<ItemRemovedArgs<T>> beforehandler = BeforeItemRemoved;
        EventHandler<ItemRemovedArgs<T>> afterhandler = AfterItemRemoved;
        EventHandler handlerListChanged = ListChanged;
        T item = default(T);
        if (index >= 0)
            item = this[index];
        WRListClass<T> old = this;
        if (beforehandler != null)
        {
            beforehandler(this, new ItemRemovedArgs<T>(index, item));
        }
        base.RemoveItem(index);
        if (afterhandler != null)
        {
            afterhandler(this, new ItemRemovedArgs<T>(index, item));
        }
        if (handlerListChanged != null)
        {
            if (old != this)
                handlerListChanged(this, null);
        }
    }


    protected override void SetItem(int index, T item)
    {
        EventHandler<ItemSetArgs<T>> beforehandler = BeforeItemSet;
        EventHandler<ItemSetArgs<T>> afterhandler = AfterItemSet;
        EventHandler handlerListChanged = ListChanged;
        WRListClass<T> old = this;
        if (beforehandler != null)
        {
            beforehandler(this, new ItemSetArgs<T>(index, item));
        }
        base.SetItem(index, item);
        if (afterhandler != null)
        {
            afterhandler(this, new ItemSetArgs<T>(index, item));
        }
        if (handlerListChanged != null)
        {

            if (old != this)
                handlerListChanged(this, null);
        }
    }
}

public class ItemInsertedArgs<T> : EventArgs
{
    public int Index;
    public T Item;

    public ItemInsertedArgs(int index, T item)
    {
        this.Index = index;
        this.Item = item;
    }
}


public class ItemSetArgs<T> : EventArgs
{
    public int Index;
    public T Item;

    public ItemSetArgs(int index, T item)
    {
        this.Index = index;
        this.Item = item;
    }
}

public class ItemRemovedArgs<T> : EventArgs
{
    public int Index;
    public T Item;

    public ItemRemovedArgs(int index, T item)
    {
        this.Index = index;
        this.Item = item;
    }
}

KOD właściwości:

[Category("Grupy właściwości")]
[Description("Definicja grup właściwości")]
[System.ComponentModel.DesignerSerializationVisibility(System.ComponentModel.DesignerSerializationVisibility.Content)]
[EditorAttribute(typeof(System.ComponentModel.Design.CollectionEditor), typeof(System.Drawing.Design.UITypeEditor))]
public WRListClass<GroupClass> Groups
{
get { return pGroups; }
set {
pGroups = value;
}
}

0

a czy wewnetrzna zmienna listy pGroup jest zainicjalizowana?

WRListClass<GroupClass> pGroups = new WRListClass<GroupClass>();

0

chyba jak robię insert to sama się inicjuje.... :-|

0

chyba jednak nie
sprawdze jeszcze to dokladnie, ale wstepny test sklania mnie do stwierdzenia ze sama nie i musisz ja zainicjowac

0

Daj znać jak na coś wpadniesz

0

Nie ma to nic wspólnego z tym ze nie jest zainicjowana. W moim przypadku przechowywany jest item który nie wymaga specjalnego konstruktora więc item sam się tworzy inicjuje z nullami. Co się okazało, dodawanie nowego itema do kolekcji jak i usuwanie itema wiąże się z tym ze wszystko do zera jest clearowane :-) hehehe dobre

0

Nie wiem co rozumiesz przez "wszystko jest clearowane". W Collection<T> elementy są trzymane w obiekcie List<T> - a w nim - tablica. Przy dodaniu elementu kluczową metodą jest Array.Copy.

Może to ci pomoże.

Oto jak wygląda Insert dla tej klasy:

public void Insert(int index, T item)
{
    if (index > this._size)
    {
        ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index, ExceptionResource.ArgumentOutOfRange_ListInsert);
    }
    if (this._size == this._items.Length)
    {
        this.EnsureCapacity(this._size + 1);
    }
    if (index < this._size)
    {
        Array.Copy(this._items, index, this._items, index + 1, this._size - index);
    }
    this._items[index] = item;
    this._size++;
    this._version++;
}

 
0

Chdziło mi oto że. Jak robisz insert item (który nadpisałem) zawsze jest robiony clearlist i nawet jak dodajesz nowy item to po wyczyszczeniu insertitem jest wywoływany tyle razy ile masz już elementów +1 który dodałeś. Zaznaczam że mówimy o usercontrol oraz zachowaniu designer'a

1 użytkowników online, w tym zalogowanych: 0, gości: 1