Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Creating a new user control using decorator pattern

Creating a new user control using decorator pattern

Scheduled Pinned Locked Moved C#
winformscsharpsysadmintoolsregex
12 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    Berlus
    wrote on last edited by
    #1

    Hello all, To demonstrate the decorator pattern, many articles gives as an example the ability to extend user controls (e.g. a window) with another ability (e.g. scrollable bar). I'm trying to use it in winforms and extend the ProgressBar. I'm building a utility function that copy some files from one network drive to another network drive, and I would like to supply also a progress bar that will show (as accurate as possible) the progress of the copy. In order to that I want to extend the progress bar with a full amount property, and a sniffing method (supplied by the user, e.g. querying the network adapter), and thus the progress bar would display the size so far divided by the known full amount. Decorator pattern seems the perfect choice but i can't seems to implement in winforms. Thanks, Berlus.

    E T 2 Replies Last reply
    0
    • B Berlus

      Hello all, To demonstrate the decorator pattern, many articles gives as an example the ability to extend user controls (e.g. a window) with another ability (e.g. scrollable bar). I'm trying to use it in winforms and extend the ProgressBar. I'm building a utility function that copy some files from one network drive to another network drive, and I would like to supply also a progress bar that will show (as accurate as possible) the progress of the copy. In order to that I want to extend the progress bar with a full amount property, and a sniffing method (supplied by the user, e.g. querying the network adapter), and thus the progress bar would display the size so far divided by the known full amount. Decorator pattern seems the perfect choice but i can't seems to implement in winforms. Thanks, Berlus.

      E Offline
      E Offline
      Ennis Ray Lynch Jr
      wrote on last edited by
      #2

      While you would normally use the File.Copy method to copy files in Windows using the File.OpenRead method will allow you to manually copy using streams and have the file size and stream location immediately available and thus negate the need for a custom progress bar that uses a sniffer. Just remember, never load the full stream in memory.

      Need custom software developed? I do custom programming based primarily on MS tools with an emphasis on C# development and consulting. A man said to the universe: "Sir I exist!" "However," replied the universe, "The fact has not created in me A sense of obligation." --Stephen Crane

      B 1 Reply Last reply
      0
      • E Ennis Ray Lynch Jr

        While you would normally use the File.Copy method to copy files in Windows using the File.OpenRead method will allow you to manually copy using streams and have the file size and stream location immediately available and thus negate the need for a custom progress bar that uses a sniffer. Just remember, never load the full stream in memory.

        Need custom software developed? I do custom programming based primarily on MS tools with an emphasis on C# development and consulting. A man said to the universe: "Sir I exist!" "However," replied the universe, "The fact has not created in me A sense of obligation." --Stephen Crane

        B Offline
        B Offline
        Berlus
        wrote on last edited by
        #3

        Thanks for your answer. The problem is that the file copying is in 3rd party Dll, and I'm aware only to the side effects of the copying process. And, I would like to use this opportunity to exercise the decorator pattern. thanks anyway, Berlus

        E 1 Reply Last reply
        0
        • B Berlus

          Hello all, To demonstrate the decorator pattern, many articles gives as an example the ability to extend user controls (e.g. a window) with another ability (e.g. scrollable bar). I'm trying to use it in winforms and extend the ProgressBar. I'm building a utility function that copy some files from one network drive to another network drive, and I would like to supply also a progress bar that will show (as accurate as possible) the progress of the copy. In order to that I want to extend the progress bar with a full amount property, and a sniffing method (supplied by the user, e.g. querying the network adapter), and thus the progress bar would display the size so far divided by the known full amount. Decorator pattern seems the perfect choice but i can't seems to implement in winforms. Thanks, Berlus.

          T Offline
          T Offline
          T M Gray
          wrote on last edited by
          #4

          Where are you getting your information about patterns? Decorator involves changing the behaviour of a class on the fly at runtime. The stuff you want to do can and probably should be done at compile time. AJAX extenders are good examples of decorator. They add functionality. But what you are wanting to do is more of a change in how the progress bar works than an addition of a feature. You want to change how it calculates progress. That sounds more like a subclass to me.

          B 1 Reply Last reply
          0
          • B Berlus

            Thanks for your answer. The problem is that the file copying is in 3rd party Dll, and I'm aware only to the side effects of the copying process. And, I would like to use this opportunity to exercise the decorator pattern. thanks anyway, Berlus

            E Offline
            E Offline
            Ennis Ray Lynch Jr
            wrote on last edited by
            #5

            Patterns are suggestions when problems arise that they are suited for. Using a pattern for a given problem because you want to use the pattern is not a recipe for success. Sometimes the problem is not with the answer but with the desired approach. Since a progress bar is inheritable this is the simplest method. Note, BeginCopyNotifyChange will call your third party tool and poll. However, keep in mind, that you cannot technically know when the transfer is finished unless you poll the locks placed on the file for copying nor will you know the size, assuming the file copy tool is a third party tool.

            public class FileCopyProgressBar : System.Windows.Forms.ProgressBar {
            public void BeginCopyNotifyChange(string source, string destination);
            }

            Need custom software developed? I do custom programming based primarily on MS tools with an emphasis on C# development and consulting. A man said to the universe: "Sir I exist!" "However," replied the universe, "The fact has not created in me A sense of obligation." --Stephen Crane

            1 Reply Last reply
            0
            • T T M Gray

              Where are you getting your information about patterns? Decorator involves changing the behaviour of a class on the fly at runtime. The stuff you want to do can and probably should be done at compile time. AJAX extenders are good examples of decorator. They add functionality. But what you are wanting to do is more of a change in how the progress bar works than an addition of a feature. You want to change how it calculates progress. That sounds more like a subclass to me.

              B Offline
              B Offline
              Berlus
              wrote on last edited by
              #6

              Thanks for your answer, it is execatly what I was aiming for, beacause I need some help understanding the pattern. I've looked up decorator pattern in wikipedia, and i quote: http://en.wikipedia.org/wiki/Decorator\_pattern "In object-oriented programming, the decorator pattern is a design pattern that allows new/additional behaviour to be added to an existing object dynamically." dynamically, as you said. but in the motivation part: "As an example, consider a window in a windowing system. To allow scrolling of the window's contents, we may wish to add horizontal or vertical scrollbars to it, as appropriate. Assume windows are represented by instances of the Window class, and assume this class has no functionality for adding scrollbars. We could create a subclass ScrollingWindow that provides them, or we could create a ScrollingWindowDecorator that adds this functionality to existing Window objects. At this point, either solution would be fine." which seemed like what i'm trying to do. Thanks, Berlus

              B T 2 Replies Last reply
              0
              • B Berlus

                Thanks for your answer, it is execatly what I was aiming for, beacause I need some help understanding the pattern. I've looked up decorator pattern in wikipedia, and i quote: http://en.wikipedia.org/wiki/Decorator\_pattern "In object-oriented programming, the decorator pattern is a design pattern that allows new/additional behaviour to be added to an existing object dynamically." dynamically, as you said. but in the motivation part: "As an example, consider a window in a windowing system. To allow scrolling of the window's contents, we may wish to add horizontal or vertical scrollbars to it, as appropriate. Assume windows are represented by instances of the Window class, and assume this class has no functionality for adding scrollbars. We could create a subclass ScrollingWindow that provides them, or we could create a ScrollingWindowDecorator that adds this functionality to existing Window objects. At this point, either solution would be fine." which seemed like what i'm trying to do. Thanks, Berlus

                B Offline
                B Offline
                Berlus
                wrote on last edited by
                #7

                After rereading the article i'm attaching a sample class:

                public abstract class ProgressBarDecorator : ProgressBar
                {
                protected ProgressBar m_decoratedProgressBar; // the Window being decorated

                    public ProgressBarDecorator()
                    {
                        
                    }
                
                    public ProgressBarDecorator(ProgressBar p\_decoratedProgressBar)
                    {
                        this.m\_decoratedProgressBar = p\_decoratedProgressBar;
                }
                
                }
                
                // the first concrete decorator which adds functionality
                public class NotifiableProgressBarDecorator : ProgressBarDecorator
                {
                
                    public NotifiableProgressBarDecorator()
                    {
                        
                    }
                    public NotifiableProgressBarDecorator(ProgressBar p\_decoratedProgressBar)
                        : base(p\_decoratedProgressBar)
                    {
                    }
                
                    protected override void OnCreateControl()
                    {
                        base.OnCreateControl();
                        this.BackColor = Color.Green;
                        this.Value = 50;
                    }
                }
                

                }

                It is the base design fot my implementation, if it will be any good, i might consider posting as an article. Your thoughts ans suggestions are most welcome. Thanks, Belrus

                modified on Friday, June 18, 2010 5:17 PM

                D 1 Reply Last reply
                0
                • B Berlus

                  After rereading the article i'm attaching a sample class:

                  public abstract class ProgressBarDecorator : ProgressBar
                  {
                  protected ProgressBar m_decoratedProgressBar; // the Window being decorated

                      public ProgressBarDecorator()
                      {
                          
                      }
                  
                      public ProgressBarDecorator(ProgressBar p\_decoratedProgressBar)
                      {
                          this.m\_decoratedProgressBar = p\_decoratedProgressBar;
                  }
                  
                  }
                  
                  // the first concrete decorator which adds functionality
                  public class NotifiableProgressBarDecorator : ProgressBarDecorator
                  {
                  
                      public NotifiableProgressBarDecorator()
                      {
                          
                      }
                      public NotifiableProgressBarDecorator(ProgressBar p\_decoratedProgressBar)
                          : base(p\_decoratedProgressBar)
                      {
                      }
                  
                      protected override void OnCreateControl()
                      {
                          base.OnCreateControl();
                          this.BackColor = Color.Green;
                          this.Value = 50;
                      }
                  }
                  

                  }

                  It is the base design fot my implementation, if it will be any good, i might consider posting as an article. Your thoughts ans suggestions are most welcome. Thanks, Belrus

                  modified on Friday, June 18, 2010 5:17 PM

                  D Offline
                  D Offline
                  DaveyM69
                  wrote on last edited by
                  #8

                  First thought is to use the 'code block' widget above the edit box when posting so your code is surrounded by <pre></pre> tags so the code is legible.

                  Dave

                  If this helped, please vote & accept answer!

                  Binging is like googling, it just feels dirtier. (Pete O'Hanlon)
                  BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

                  B L 2 Replies Last reply
                  0
                  • D DaveyM69

                    First thought is to use the 'code block' widget above the edit box when posting so your code is surrounded by <pre></pre> tags so the code is legible.

                    Dave

                    If this helped, please vote & accept answer!

                    Binging is like googling, it just feels dirtier. (Pete O'Hanlon)
                    BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

                    B Offline
                    B Offline
                    Berlus
                    wrote on last edited by
                    #9

                    Thanks

                    1 Reply Last reply
                    0
                    • B Berlus

                      Thanks for your answer, it is execatly what I was aiming for, beacause I need some help understanding the pattern. I've looked up decorator pattern in wikipedia, and i quote: http://en.wikipedia.org/wiki/Decorator\_pattern "In object-oriented programming, the decorator pattern is a design pattern that allows new/additional behaviour to be added to an existing object dynamically." dynamically, as you said. but in the motivation part: "As an example, consider a window in a windowing system. To allow scrolling of the window's contents, we may wish to add horizontal or vertical scrollbars to it, as appropriate. Assume windows are represented by instances of the Window class, and assume this class has no functionality for adding scrollbars. We could create a subclass ScrollingWindow that provides them, or we could create a ScrollingWindowDecorator that adds this functionality to existing Window objects. At this point, either solution would be fine." which seemed like what i'm trying to do. Thanks, Berlus

                      T Offline
                      T Offline
                      T M Gray
                      wrote on last edited by
                      #10

                      That isn't what you are trying to do. Adding a scrollbar to a window doesn't change anything about the underlying functionality of the window. How the window is closed, moved, z-ordered etc. all remain intact. Changing the methodology a progress bar uses to determine the amount of progress is a fundamental change to the existing underlying functionality, not an addition. Adding a user preference for the color of the progress bar would be more of a decorator. I see from one of your other responses that it is a 3rd party control you are dealing with. Decorator pattern seems to be what you want only because you can't do it with subclassing because you don't have the source to do it. You are shoehorning your design requirement into a pattern based on other external factors. Why are you using a control that doesn't do what you want it to?

                      B 1 Reply Last reply
                      0
                      • D DaveyM69

                        First thought is to use the 'code block' widget above the edit box when posting so your code is surrounded by <pre></pre> tags so the code is legible.

                        Dave

                        If this helped, please vote & accept answer!

                        Binging is like googling, it just feels dirtier. (Pete O'Hanlon)
                        BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

                        L Offline
                        L Offline
                        Luc Pattyn
                        wrote on last edited by
                        #11

                        Right. :thumbsup:

                        Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

                        Please use < PRE > tags for code snippets, it preserves indentation, and improves readability.

                        1 Reply Last reply
                        0
                        • T T M Gray

                          That isn't what you are trying to do. Adding a scrollbar to a window doesn't change anything about the underlying functionality of the window. How the window is closed, moved, z-ordered etc. all remain intact. Changing the methodology a progress bar uses to determine the amount of progress is a fundamental change to the existing underlying functionality, not an addition. Adding a user preference for the color of the progress bar would be more of a decorator. I see from one of your other responses that it is a 3rd party control you are dealing with. Decorator pattern seems to be what you want only because you can't do it with subclassing because you don't have the source to do it. You are shoehorning your design requirement into a pattern based on other external factors. Why are you using a control that doesn't do what you want it to?

                          B Offline
                          B Offline
                          Berlus
                          wrote on last edited by
                          #12

                          T M Gray wrote:

                          Why are you using a control that doesn't do what you want it to?

                          Form where do i get such control ? anyway, here is the first version of the class, I hope i'm not abusing this forum ...

                          using System;
                          using System.Drawing;
                          using System.Net.NetworkInformation;
                          using System.Windows.Forms;

                          namespace TestProgressBar
                          {
                          public abstract class ProgressBarDecorator : ProgressBar
                          {
                          protected ProgressBar m_decoratedProgressBar; // the Window being decorated

                              public ProgressBarDecorator()
                              {
                          
                              }
                          
                              public ProgressBarDecorator(ProgressBar p\_decoratedProgressBar)
                              {
                                  this.m\_decoratedProgressBar = p\_decoratedProgressBar;
                              }
                          
                          }
                          
                          // the first concrete decorator which adds vertical scrollbar functionality
                          public class NotifiableProgressBarDecorator : ProgressBarDecorator
                          {
                              public long FullAmount;
                          
                              public void InformOnNewData(long p\_newDelta)
                              {
                                  float normalizedSize = Maximum - Minimum;
                          
                                  if (FullAmount != 0)
                                  {
                                      int newValue = Value + (int) (p\_newDelta/(float) FullAmount\*normalizedSize);
                          
                                      if ((Value + newValue) <= Maximum)
                                      {
                                          Value += newValue;
                                      }
                                      else
                                      {
                                          Value = Maximum;
                                      }
                                  }
                              }
                          
                              public void Clear()
                              {
                                  FullAmount = 0;
                              }
                          
                              public NotifiableProgressBarDecorator()
                              {
                              }
                          
                              public NotifiableProgressBarDecorator(ProgressBar p\_decoratedProgressBar)
                                  : base(p\_decoratedProgressBar)
                              {
                              }
                          }
                          
                          public class NetworkNotifiableProgressBarDecorator : NotifiableProgressBarDecorator
                          {
                              private NetworkInterface adapter;
                              private Timer m\_timer = new Timer();
                              private long m\_lastSentBytes;
                          
                              public NetworkNotifiableProgressBarDecorator()
                              {
                                  adapter = NetworkInterface.GetAllNetworkInterfaces()\[1\];
                          
                                  m\_timer.Interval = 500;
                                  m\_timer.Tick += onTimerTick;
                              }
                          
                              public void Init(int p\_totalNofBytes)
                              {
                                  FullAmount = p\_totalNofBytes;
                                  m\_lastSentBytes = adapter.GetIPv4Statistics().BytesSent;
                                  m\_timer.Start();
                              }
                          
                              private void onTimerTick(object p\_sender
                          
                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups