Click here to Skip to main content
15,915,093 members
Home / Discussions / C#
   

C#

 
AnswerRe: Embedding executable in my application Pin
Judah Gabriel Himango16-Apr-06 6:01
sponsorJudah Gabriel Himango16-Apr-06 6:01 
GeneralRe: Embedding executable in my application Pin
zorro.tmh16-Apr-06 23:36
zorro.tmh16-Apr-06 23:36 
QuestionHow can I use the objects which I made by 3DMax into Open GL ??!! Pin
suroor45316-Apr-06 2:27
suroor45316-Apr-06 2:27 
AnswerRe: How can I use the objects which I made by 3DMax into Open GL ??!! Pin
Judah Gabriel Himango16-Apr-06 6:09
sponsorJudah Gabriel Himango16-Apr-06 6:09 
GeneralRe: How can I use the objects which I made by 3DMax into Open GL ??!! Pin
suroor45316-Apr-06 7:26
suroor45316-Apr-06 7:26 
GeneralRe: How can I use the objects which I made by 3DMax into Open GL ??!! Pin
Judah Gabriel Himango16-Apr-06 15:41
sponsorJudah Gabriel Himango16-Apr-06 15:41 
AnswerRe: How can I use the objects which I made by 3DMax into Open GL ??!! Pin
Judah Gabriel Himango16-Apr-06 6:25
sponsorJudah Gabriel Himango16-Apr-06 6:25 
QuestionList with ListChanged and ListChanging events - asking for code review and suggestions (long source code included) Pin
Patrick Klug16-Apr-06 2:20
Patrick Klug16-Apr-06 2:20 
Hi!

This is my first question in this board. So please be gentle with your comments

I just wanted a List<t> class that provides events when the list is about to change or has already changed.

I briefly looked in the framework documentation and searched the internet but I haven't found anything useful.

I would greatly appreciate any comments or information on best practise solutions

for this requirement.

Thanks in advance!


My implementation:

<SPAN style="COLOR: blue">public</SPAN> <SPAN style="COLOR: blue">class</SPAN> ObservedList<T> : List<T>
    {
        <SPAN style="COLOR: blue">public</SPAN> ObservedList()
        {

        }
        <SPAN style="COLOR: blue">public</SPAN> ObservedList(IEnumerable<T> collection)
            : <SPAN style="COLOR: blue">base</SPAN>(collection)
        {
        }

        <SPAN style="COLOR: blue">public</SPAN> ObservedList(<SPAN style="COLOR: blue">int</SPAN> capacity)
            : <SPAN style="COLOR: blue">base</SPAN>(capacity)
        {
        }

        <SPAN style="COLOR: blue">#region</SPAN> EventArgs
        <SPAN style="COLOR: blue">public</SPAN> <SPAN style="COLOR: blue">enum</SPAN> ChangeAction
        {
            Add,
            Remove,
            Clear,
            Sort
        }

        <SPAN style="COLOR: blue">public</SPAN> <SPAN style="COLOR: blue">class</SPAN> ObservedListChangingEventArgs : ObservedListChangedEventArgs
        {
            <SPAN style="COLOR: blue">public</SPAN> ObservedListChangingEventArgs(ChangeAction action, List<T> item)
                : <SPAN style="COLOR: blue">base</SPAN>(action, item)
            {
            }
            <SPAN style="COLOR: blue">private</SPAN> <SPAN style="COLOR: blue">bool</SPAN> _cancel;

            <SPAN style="COLOR: blue">public</SPAN> <SPAN style="COLOR: blue">bool</SPAN> Cancel
            {
                <SPAN style="COLOR: blue">get</SPAN> { <SPAN style="COLOR: blue">return</SPAN> _cancel; }
                <SPAN style="COLOR: blue">set</SPAN> { _cancel = value; }
            }

            <SPAN style="COLOR: blue">private</SPAN> <SPAN style="COLOR: blue">bool</SPAN> _handled;

            <SPAN style="COLOR: blue">public</SPAN> <SPAN style="COLOR: blue">bool</SPAN> Handled
            {
                <SPAN style="COLOR: blue">get</SPAN> { <SPAN style="COLOR: blue">return</SPAN> _handled; }
                <SPAN style="COLOR: blue">set</SPAN> { _handled = value; }
            }
        }

        <SPAN style="COLOR: blue">public</SPAN> <SPAN style="COLOR: blue">class</SPAN> ObservedListChangedEventArgs : System.EventArgs
        {
            <SPAN style="COLOR: blue">public</SPAN> ObservedListChangedEventArgs(ChangeAction action, List<T> list)
            { 
                <SPAN style="COLOR: blue">this</SPAN>._items = list;
                <SPAN style="COLOR: blue">this</SPAN>._action = action;
            }

            <SPAN style="COLOR: blue">private</SPAN> List<T> _items;

            <SPAN style="COLOR: blue">public</SPAN> List<T> Items
            {
                <SPAN style="COLOR: blue">get</SPAN> { <SPAN style="COLOR: blue">return</SPAN> _items; }
                <SPAN style="COLOR: blue">set</SPAN> { _items = value; }
            }

            <SPAN style="COLOR: blue">private</SPAN> ChangeAction _action;

            <SPAN style="COLOR: blue">public</SPAN> ChangeAction Action
            {
                <SPAN style="COLOR: blue">get</SPAN> { <SPAN style="COLOR: blue">return</SPAN> _action; }
                <SPAN style="COLOR: blue">set</SPAN> { _action = value; }
            }
        }

        <SPAN style="COLOR: blue">#endregion</SPAN>

        <SPAN style="COLOR: blue">#region</SPAN> events

        <SPAN style="COLOR: blue">public</SPAN> <SPAN style="COLOR: blue">event</SPAN> EventHandler<ObservedListChangingEventArgs> OnListChanging;
        <SPAN style="COLOR: blue">public</SPAN> <SPAN style="COLOR: blue">event</SPAN> EventHandler<ObservedListChangedEventArgs> OnListChanged;

        <SPAN style="COLOR: blue">private</SPAN> <SPAN style="COLOR: blue">void</SPAN> FireOnListChangedEvent(ChangeAction action)
        {
            <SPAN style="COLOR: blue">if</SPAN> (OnListChanged != <SPAN style="COLOR: blue">null</SPAN>)
            {
                <SPAN style="COLOR: blue">try</SPAN>
                {
                    <SPAN style="COLOR: blue">this</SPAN>.OnListChanged(<SPAN style="COLOR: blue">this</SPAN>, <SPAN style="COLOR: blue">new</SPAN> ObservedListChangedEventArgs(action, <SPAN style="COLOR: blue">this</SPAN>));
                }
                <SPAN style="COLOR: blue">catch</SPAN> (System.Exception) { }
            }
        }

        <SPAN style="COLOR: gray">/// <summary></SPAN>
        <SPAN style="COLOR: gray">/// returns true if the action should be performed</SPAN>
        <SPAN style="COLOR: gray">/// </summary></SPAN>
        <SPAN style="COLOR: blue">private</SPAN> <SPAN style="COLOR: blue">bool</SPAN> HandleOnListChangingEvents(ChangeAction action, T item)
        {
            List<T> items = <SPAN style="COLOR: blue">new</SPAN> List<T>();
            items.Add(item);

            <SPAN style="COLOR: blue">return</SPAN> HandleOnListChangingEvents(action, items);
        }

        <SPAN style="COLOR: gray">/// <summary></SPAN>
        <SPAN style="COLOR: gray">/// returns true if the action should be performed</SPAN>
        <SPAN style="COLOR: gray">/// </summary></SPAN>
        <SPAN style="COLOR: blue">private</SPAN> <SPAN style="COLOR: blue">bool</SPAN> HandleOnListChangingEvents(ChangeAction action, List<T> items)
        {
            <SPAN style="COLOR: blue">if</SPAN> (OnListChanging != <SPAN style="COLOR: blue">null</SPAN>)
            {
                ObservedListChangingEventArgs e = <SPAN style="COLOR: blue">new</SPAN> ObservedListChangingEventArgs(action,
                    items);

                <SPAN style="COLOR: blue">foreach</SPAN> (Delegate subscriber <SPAN style="COLOR: blue">in</SPAN> OnListChanging.GetInvocationList())
                {
                    EventHandler<ObservedListChangingEventArgs> target = (EventHandler<ObservedListChangingEventArgs>)subscriber;
                    <SPAN style="COLOR: blue">try</SPAN>
                    {
                        target(<SPAN style="COLOR: blue">this</SPAN>, e);
                    }
                    <SPAN style="COLOR: blue">catch</SPAN> (System.Exception)
                    {<SPAN style="COLOR: green">/*catch all exceptions... otherwise an occuring exception would break the whole execution*/</SPAN> }
                    <SPAN style="COLOR: blue">if</SPAN> (e.Handled)
                    {
                        <SPAN style="COLOR: blue">break</SPAN>;
                    }
                }

                <SPAN style="COLOR: blue">return</SPAN> !e.Cancel;
            }
            <SPAN style="COLOR: blue">return</SPAN> <SPAN style="COLOR: maroon">true</SPAN>;
        }
        <SPAN style="COLOR: blue">#endregion</SPAN>

        <SPAN style="COLOR: blue">#region</SPAN> <SPAN style="COLOR: blue">new</SPAN> implementation of the list methods (those changing the list)


        <SPAN style="COLOR: blue">#region</SPAN> Add / AddRange

        <SPAN style="COLOR: blue">public</SPAN> <SPAN style="COLOR: blue">new</SPAN> <SPAN style="COLOR: blue">void</SPAN> Add(T item)
        {
            <SPAN style="COLOR: blue">if</SPAN> (HandleOnListChangingEvents(ChangeAction.Add, item))
            {
                <SPAN style="COLOR: blue">base</SPAN>.Add(item);
                <SPAN style="COLOR: blue">this</SPAN>.FireOnListChangedEvent(ChangeAction.Add);
            }
        }

        <SPAN style="COLOR: blue">public</SPAN> <SPAN style="COLOR: blue">new</SPAN> <SPAN style="COLOR: blue">void</SPAN> AddRange(IEnumerable<T> collection)
        {
            List<T> items = <SPAN style="COLOR: blue">new</SPAN> List<T>(collection);
            <SPAN style="COLOR: blue">if</SPAN> (HandleOnListChangingEvents(ChangeAction.Add,items ))
            {
                <SPAN style="COLOR: blue">base</SPAN>.AddRange(collection);
                <SPAN style="COLOR: blue">this</SPAN>.FireOnListChangedEvent(ChangeAction.Add);
            }
        }

        <SPAN style="COLOR: blue">#endregion</SPAN>

        <SPAN style="COLOR: blue">#region</SPAN> Remove / Clear
        <SPAN style="COLOR: blue">public</SPAN> <SPAN style="COLOR: blue">new</SPAN> <SPAN style="COLOR: blue">void</SPAN> Clear()
        {
            <SPAN style="COLOR: blue">if</SPAN> (HandleOnListChangingEvents(ChangeAction.Clear, <SPAN style="COLOR: blue">this</SPAN>))
            {
                <SPAN style="COLOR: blue">base</SPAN>.Clear();
                <SPAN style="COLOR: blue">this</SPAN>.FireOnListChangedEvent(ChangeAction.Clear);
            }
        }

        <SPAN style="COLOR: blue">public</SPAN> <SPAN style="COLOR: blue">new</SPAN> <SPAN style="COLOR: blue">bool</SPAN> Remove(T item)
        {
            <SPAN style="COLOR: blue">if</SPAN> (HandleOnListChangingEvents(ChangeAction.Remove, item))
            {
               <SPAN style="COLOR: blue">return</SPAN> <SPAN style="COLOR: blue">base</SPAN>.Remove(item);
               <SPAN style="COLOR: blue">this</SPAN>.FireOnListChangedEvent(ChangeAction.Remove);
            }
            <SPAN style="COLOR: blue">return</SPAN> <SPAN style="COLOR: maroon">false</SPAN>;
        }

        <SPAN style="COLOR: blue">public</SPAN> <SPAN style="COLOR: blue">new</SPAN> <SPAN style="COLOR: blue">void</SPAN> RemoveAt(<SPAN style="COLOR: blue">int</SPAN> index)
        {
            <SPAN style="COLOR: blue">if</SPAN> (<SPAN style="COLOR: blue">base</SPAN>.Count > index)
            {
                T item = <SPAN style="COLOR: blue">base</SPAN>[index];
                <SPAN style="COLOR: blue">if</SPAN> (HandleOnListChangingEvents(ChangeAction.Remove, item))
                {
                    <SPAN style="COLOR: blue">base</SPAN>.RemoveAt(index);
                    <SPAN style="COLOR: blue">this</SPAN>.FireOnListChangedEvent(ChangeAction.Remove);

                }
            }
        }

        <SPAN style="COLOR: blue">public</SPAN> <SPAN style="COLOR: blue">new</SPAN> <SPAN style="COLOR: blue">int</SPAN> RemoveAll(Predicate<T> predicate)
        {
            <SPAN style="COLOR: blue">if</SPAN> (HandleOnListChangingEvents(ChangeAction.Remove, <SPAN style="COLOR: blue">this</SPAN>.Find(predicate)))
            {
                <SPAN style="COLOR: blue">int</SPAN> removedItems = <SPAN style="COLOR: blue">base</SPAN>.RemoveAll(predicate);
                <SPAN style="COLOR: blue">this</SPAN>.FireOnListChangedEvent(ChangeAction.Remove);
                <SPAN style="COLOR: blue">return</SPAN> removedItems;
            }
            <SPAN style="COLOR: blue">return</SPAN> <SPAN style="COLOR: maroon">0</SPAN>;
        }

        <SPAN style="COLOR: blue">public</SPAN> <SPAN style="COLOR: blue">new</SPAN> <SPAN style="COLOR: blue">void</SPAN> RemoveRange(<SPAN style="COLOR: blue">int</SPAN> index, <SPAN style="COLOR: blue">int</SPAN> count)
        {
            <SPAN style="COLOR: blue">if</SPAN> (index+count<<SPAN style="COLOR: blue">this</SPAN>.Count)
            {
                <SPAN style="COLOR: blue">if</SPAN> (HandleOnListChangingEvents(ChangeAction.Remove,<SPAN style="COLOR: blue">this</SPAN>.GetRange(index,count)))
                {
                    <SPAN style="COLOR: blue">base</SPAN>.RemoveRange(index,count);
                    <SPAN style="COLOR: blue">this</SPAN>.FireOnListChangedEvent(ChangeAction.Remove);
                }
            }

            
        }

        <SPAN style="COLOR: blue">#endregion</SPAN>

        <SPAN style="COLOR: blue">#region</SPAN> Reverse / Sort

        <SPAN style="COLOR: blue">public</SPAN> <SPAN style="COLOR: blue">new</SPAN> <SPAN style="COLOR: blue">void</SPAN> Reverse()
        {
            <SPAN style="COLOR: blue">if</SPAN> (HandleOnListChangingEvents(ChangeAction.Sort, <SPAN style="COLOR: blue">this</SPAN>))
            {
                <SPAN style="COLOR: blue">base</SPAN>.Reverse();
                <SPAN style="COLOR: blue">this</SPAN>.FireOnListChangedEvent(ChangeAction.Sort);
            }
        }

        <SPAN style="COLOR: blue">public</SPAN> <SPAN style="COLOR: blue">new</SPAN> <SPAN style="COLOR: blue">void</SPAN> Reverse(<SPAN style="COLOR: blue">int</SPAN> index, <SPAN style="COLOR: blue">int</SPAN> count)
        {
            <SPAN style="COLOR: blue">if</SPAN> (index+count<<SPAN style="COLOR: blue">this</SPAN>.Count)
            {
                <SPAN style="COLOR: blue">if</SPAN> (HandleOnListChangingEvents(ChangeAction.Sort,<SPAN style="COLOR: blue">this</SPAN>.GetRange(index,count)))
                {
                    <SPAN style="COLOR: blue">base</SPAN>.Reverse();
                    <SPAN style="COLOR: blue">this</SPAN>.FireOnListChangedEvent(ChangeAction.Sort);
                }
            }
        }

        <SPAN style="COLOR: blue">public</SPAN> <SPAN style="COLOR: blue">new</SPAN> <SPAN style="COLOR: blue">void</SPAN> Sort()
        {
            <SPAN style="COLOR: blue">if</SPAN> (HandleOnListChangingEvents(ChangeAction.Sort, <SPAN style="COLOR: blue">this</SPAN>))
            {
                <SPAN style="COLOR: blue">base</SPAN>.Sort();
                <SPAN style="COLOR: blue">this</SPAN>.FireOnListChangedEvent(ChangeAction.Sort);
            }
        }

        <SPAN style="COLOR: blue">public</SPAN> <SPAN style="COLOR: blue">new</SPAN> <SPAN style="COLOR: blue">void</SPAN> Sort(Comparison<T> comparison)
        {
            <SPAN style="COLOR: blue">if</SPAN> (HandleOnListChangingEvents(ChangeAction.Sort, <SPAN style="COLOR: blue">this</SPAN>))
            {
                <SPAN style="COLOR: blue">base</SPAN>.Sort(comparison);
                <SPAN style="COLOR: blue">this</SPAN>.FireOnListChangedEvent(ChangeAction.Sort);
            }
        }

        <SPAN style="COLOR: blue">public</SPAN> <SPAN style="COLOR: blue">new</SPAN> <SPAN style="COLOR: blue">void</SPAN> Sort(IComparer<T> comparer)
        {
            <SPAN style="COLOR: blue">if</SPAN> (HandleOnListChangingEvents(ChangeAction.Sort, <SPAN style="COLOR: blue">this</SPAN>))
            {
                <SPAN style="COLOR: blue">base</SPAN>.Sort(comparer);
            }
        }

        <SPAN style="COLOR: blue">public</SPAN> <SPAN style="COLOR: blue">new</SPAN> <SPAN style="COLOR: blue">void</SPAN> Sort(<SPAN style="COLOR: blue">int</SPAN> index,<SPAN style="COLOR: blue">int</SPAN> count,IComparer<T> comparer)
        {
            <SPAN style="COLOR: blue">if</SPAN> (index + count < <SPAN style="COLOR: blue">this</SPAN>.Count)
            {
                <SPAN style="COLOR: blue">if</SPAN> (HandleOnListChangingEvents(ChangeAction.Sort, <SPAN style="COLOR: blue">this</SPAN>.GetRange(index,count)))
                {
                    <SPAN style="COLOR: blue">base</SPAN>.Sort(index,count,comparer);
                    <SPAN style="COLOR: blue">this</SPAN>.FireOnListChangedEvent(ChangeAction.Sort);
                }
            }
        }

        <SPAN style="COLOR: blue">#endregion</SPAN>


        <SPAN style="COLOR: blue">#endregion</SPAN>
    }


-- modified at 8:21 Sunday 16th April, 2006
AnswerRe: List with ListChanged and ListChanging events - asking for code review and suggestions (long source code included) Pin
Guffa16-Apr-06 3:45
Guffa16-Apr-06 3:45 
GeneralRe: List with ListChanged and ListChanging events - asking for code review and suggestions (long source code included) Pin
Patrick Klug16-Apr-06 6:10
Patrick Klug16-Apr-06 6:10 
QuestionTreeView Pin
babamara16-Apr-06 2:05
babamara16-Apr-06 2:05 
AnswerRe: TreeView Pin
Judah Gabriel Himango16-Apr-06 6:15
sponsorJudah Gabriel Himango16-Apr-06 6:15 
Questionhow to set Password on Access database ? Pin
hdv21216-Apr-06 1:55
hdv21216-Apr-06 1:55 
GeneralRe: how to set Password on Access database ? Pin
Guffa16-Apr-06 2:16
Guffa16-Apr-06 2:16 
GeneralRe: how to set Password on Access database ? Pin
hdv21216-Apr-06 3:42
hdv21216-Apr-06 3:42 
QuestionHow to receive result of sending the letter in MS Outlook? Pin
Vasya - dragon16-Apr-06 0:47
Vasya - dragon16-Apr-06 0:47 
Questionreading from zi[ file Pin
CoolASL15-Apr-06 23:56
CoolASL15-Apr-06 23:56 
AnswerRe: reading from zi[ file Pin
Vasya - dragon16-Apr-06 0:53
Vasya - dragon16-Apr-06 0:53 
QuestionXmlDocument.SelectSingleNode Pin
Ali Beirami15-Apr-06 23:41
Ali Beirami15-Apr-06 23:41 
AnswerRe: XmlDocument.SelectSingleNode Pin
Judah Gabriel Himango16-Apr-06 6:18
sponsorJudah Gabriel Himango16-Apr-06 6:18 
Questionhow to set Password on Access database ? Pin
hdv21215-Apr-06 22:58
hdv21215-Apr-06 22:58 
AnswerRe: how to set Password on Access database ? Pin
Guffa15-Apr-06 23:16
Guffa15-Apr-06 23:16 
GeneralRe: how to set Password on Access database ? Pin
hdv21215-Apr-06 23:27
hdv21215-Apr-06 23:27 
AnswerRe: how to set Password on Access database ? Pin
S Douglas15-Apr-06 23:59
professionalS Douglas15-Apr-06 23:59 
QuestionDeployment Pin
pankajgarg1215-Apr-06 22:28
pankajgarg1215-Apr-06 22:28 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.