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. How to update progressBar from another class

How to update progressBar from another class

Scheduled Pinned Locked Moved C#
csharpc++databasevisual-studiocom
26 Posts 4 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.
  • M MichCl

    I'm still not sure about the event with parameters!! If I had the delegate above, how would that be used to fire an event and notify the method waiting for the integer value? I started with this, but I'm not sure how to have an event call it. I was looking at this link: http://stackoverflow.com/questions/1488099/call-an-eventhandler-with-arguments[^] private void _updateProgressBar(object sender, EventArgs e, int i) { //call code from here waiting for int i }

    J Offline
    J Offline
    Jibesh
    wrote on last edited by
    #8

    Ok I will try to explain with your class name define a event in your interface iCR.cs

    public delegate void ProgressChangeHandler(int progress);

    interface CRInterface
    {
    event ProgressChangeHandler ProgressChanged;
    int ProcessTWriting(ref byte[] WDat, ref int pb_value)
    }

    Implement this interface in CRD.cs class

    class MyCR: CRInterface
    {
    public event ProgressChangeHandler ProgressChanged;

    public int ProcessTWriting(ref byte[] WDat, ref int pb_value)
    {
    status = AttemptWrites(ref pb_value);
    }

    private int AttemptWrites(ref int pb_value)
    {
    for (int i = 0; i < ((wDat.Length) / 4); i++)
    {
    RaiseProgressChange(i);
    //as it does writes, I want to set pb_value here to % complete
    }
    }

    private void RaiseProgressChange(int progress)
    {
    if(ProgressChanged!= null)
    {
    ProgressChanged(progress);
    }
    }
    }

    Finally Subscribe for event change in your FormClass i.e PC.cs

    // call this line just below where you create the instance of CR5 class
    private FormLoad(...)
    {
    cr.ProgressChanged += new ProgressChangeHandler(CR_ProgressChanged);
    }

    private void CR_ProgressChanged(int progress)
    {
    progressBar.Value = progress;
    }

    Hope this helps.

    Jibesh V P

    M 1 Reply Last reply
    0
    • J Jibesh

      Ok I will try to explain with your class name define a event in your interface iCR.cs

      public delegate void ProgressChangeHandler(int progress);

      interface CRInterface
      {
      event ProgressChangeHandler ProgressChanged;
      int ProcessTWriting(ref byte[] WDat, ref int pb_value)
      }

      Implement this interface in CRD.cs class

      class MyCR: CRInterface
      {
      public event ProgressChangeHandler ProgressChanged;

      public int ProcessTWriting(ref byte[] WDat, ref int pb_value)
      {
      status = AttemptWrites(ref pb_value);
      }

      private int AttemptWrites(ref int pb_value)
      {
      for (int i = 0; i < ((wDat.Length) / 4); i++)
      {
      RaiseProgressChange(i);
      //as it does writes, I want to set pb_value here to % complete
      }
      }

      private void RaiseProgressChange(int progress)
      {
      if(ProgressChanged!= null)
      {
      ProgressChanged(progress);
      }
      }
      }

      Finally Subscribe for event change in your FormClass i.e PC.cs

      // call this line just below where you create the instance of CR5 class
      private FormLoad(...)
      {
      cr.ProgressChanged += new ProgressChangeHandler(CR_ProgressChanged);
      }

      private void CR_ProgressChanged(int progress)
      {
      progressBar.Value = progress;
      }

      Hope this helps.

      Jibesh V P

      M Offline
      M Offline
      MichCl
      wrote on last edited by
      #9

      Thanks for the extra info, Jibesh. It's not often I have to deal with events. When I tried to do the above in my CR5.cs (implements iCR),

      public event ProgressChangeHandler ProgressChanged;

      it says "The type or namespace name 'ProgressChangeHandler' could not be found (are you missing a using directive or an assembly reference?)" and also, iCR

      public delegate void ProgressChangeHandler(int progress);

      I get "'ProgressChangeHandler': interfaces cannot declare types" Maybe it's not going to work with an interface between PC.cs and CR5.cs (implements iCR)?

      J 1 Reply Last reply
      0
      • M MichCl

        Thanks for the extra info, Jibesh. It's not often I have to deal with events. When I tried to do the above in my CR5.cs (implements iCR),

        public event ProgressChangeHandler ProgressChanged;

        it says "The type or namespace name 'ProgressChangeHandler' could not be found (are you missing a using directive or an assembly reference?)" and also, iCR

        public delegate void ProgressChangeHandler(int progress);

        I get "'ProgressChangeHandler': interfaces cannot declare types" Maybe it's not going to work with an interface between PC.cs and CR5.cs (implements iCR)?

        J Offline
        J Offline
        Jibesh
        wrote on last edited by
        #10

        The above code sample is working version if you closely look at the sample you can find that delegate method is defined in the dll where iCR is located and you don't need to use the Public access specifier for Interface methods and data. This will definitely work and I have done same stuff in the past. ProgressChangeHandler must be defined globally so that this delegate is accessible by all members.

        Jibesh V P

        M 1 Reply Last reply
        0
        • J Jibesh

          The above code sample is working version if you closely look at the sample you can find that delegate method is defined in the dll where iCR is located and you don't need to use the Public access specifier for Interface methods and data. This will definitely work and I have done same stuff in the past. ProgressChangeHandler must be defined globally so that this delegate is accessible by all members.

          Jibesh V P

          M Offline
          M Offline
          MichCl
          wrote on last edited by
          #11

          Thank you very much!!!! This is very helpful. I got it building now.

          J 1 Reply Last reply
          0
          • M MichCl

            Thank you very much!!!! This is very helpful. I got it building now.

            J Offline
            J Offline
            Jibesh
            wrote on last edited by
            #12

            Good Happy to hear that.

            Jibesh V P

            M 1 Reply Last reply
            0
            • J Jibesh

              Good Happy to hear that.

              Jibesh V P

              M Offline
              M Offline
              MichCl
              wrote on last edited by
              #13

              If I could add one more little question onto the above, which I got to work. I also have a C_Comm.cs class which is referenced in my CR5.cs, the original PC.cs, and iCR.cs. It turns out that one area I need to update my progress bar (in PC.cs) from is C_Comm, which is also in a different VS project. I'm trying to get it working, and am getting an error: Cannot implicitly convert type 'ProgressChangeHandler' to C_Comm.ProgressChangeHandler' Right now it looks like this: PC.cs (the form code)

              public event ProgressChangeHandler ProgressChanged;

              public void GetPC(...)
              {
              cr.ProgressChanged += new ProgressChangeHandler(updateProgressBar); //cr is instance of CR5 through iCR interface/factory
              cb.ProgressChanged += new ProgressChangeHandler(updateProgressBar); //cb is C_Comm
              }

              public int updateProgressBar(int i)
              {
              pb.Value = i;
              }

              iCR.cs (interface)

              public delegate void ProgressChangeHandler(int progress);
              public interface iCR
              {
              event ProgressChangeHandler ProgressChanged;
              }

              CR5.cs (implements iCR)

              public event ProgressChangeHandler ProgressChanged;
              private int AttemptWrites()
              {
              for(i=0...)
              {
              RaiseProgressChange(i);
              }
              }

              private void RaiseProgressChange(int progress)
              {
              if (ProgressChanged != null)
              {
              ProgressChanged(progress);
              }
              }

              C_Comm.cs

              public delegate void ProgressChangeHandler(int progress);

              public class C_Comm{
              public event ProgressChangeHandler ProgressChanged;

              public int Read(byte\[\] data, int ind)
              {
                  for(int i...)
                  {
                      RaiseProgressChange(i);
                  }
              }
              
              private void RaiseProgressChange(int progress)
              {
                  if (ProgressChanged != null)
                  {
                      ProgressChanged(progress);
                  }
              }
              

              }

              Do you know how I could get the ProgressBar in PC.cs to be updated also from C_Comm.cs?

              J 1 Reply Last reply
              0
              • M MichCl

                If I could add one more little question onto the above, which I got to work. I also have a C_Comm.cs class which is referenced in my CR5.cs, the original PC.cs, and iCR.cs. It turns out that one area I need to update my progress bar (in PC.cs) from is C_Comm, which is also in a different VS project. I'm trying to get it working, and am getting an error: Cannot implicitly convert type 'ProgressChangeHandler' to C_Comm.ProgressChangeHandler' Right now it looks like this: PC.cs (the form code)

                public event ProgressChangeHandler ProgressChanged;

                public void GetPC(...)
                {
                cr.ProgressChanged += new ProgressChangeHandler(updateProgressBar); //cr is instance of CR5 through iCR interface/factory
                cb.ProgressChanged += new ProgressChangeHandler(updateProgressBar); //cb is C_Comm
                }

                public int updateProgressBar(int i)
                {
                pb.Value = i;
                }

                iCR.cs (interface)

                public delegate void ProgressChangeHandler(int progress);
                public interface iCR
                {
                event ProgressChangeHandler ProgressChanged;
                }

                CR5.cs (implements iCR)

                public event ProgressChangeHandler ProgressChanged;
                private int AttemptWrites()
                {
                for(i=0...)
                {
                RaiseProgressChange(i);
                }
                }

                private void RaiseProgressChange(int progress)
                {
                if (ProgressChanged != null)
                {
                ProgressChanged(progress);
                }
                }

                C_Comm.cs

                public delegate void ProgressChangeHandler(int progress);

                public class C_Comm{
                public event ProgressChangeHandler ProgressChanged;

                public int Read(byte\[\] data, int ind)
                {
                    for(int i...)
                    {
                        RaiseProgressChange(i);
                    }
                }
                
                private void RaiseProgressChange(int progress)
                {
                    if (ProgressChanged != null)
                    {
                        ProgressChanged(progress);
                    }
                }
                

                }

                Do you know how I could get the ProgressBar in PC.cs to be updated also from C_Comm.cs?

                J Offline
                J Offline
                Jibesh
                wrote on last edited by
                #14

                if C_Comm.cs file is common to both project you can remove the delegate declaration inside the interface class and use the delegate inside C_Comm.cs in all places that should be fine

                Jibesh V P

                M 1 Reply Last reply
                0
                • J Jibesh

                  if C_Comm.cs file is common to both project you can remove the delegate declaration inside the interface class and use the delegate inside C_Comm.cs in all places that should be fine

                  Jibesh V P

                  M Offline
                  M Offline
                  MichCl
                  wrote on last edited by
                  #15

                  I tried to just have the delegate in my C_Comm.cs (not in iCR), and now in my CR5.cs class, I get the error "The name 'ProgressChanged' does not exist in the current context" in my RaiseProgressChange method. Then if I try to use cb.ProgressChanged != null, I get the error "The event 'C_Comm.ProgressChanged' can only appear on the left hand side of += or -=". I'm not very clear on using events! Do you have any ideas?

                  J 1 Reply Last reply
                  0
                  • M MichCl

                    I tried to just have the delegate in my C_Comm.cs (not in iCR), and now in my CR5.cs class, I get the error "The name 'ProgressChanged' does not exist in the current context" in my RaiseProgressChange method. Then if I try to use cb.ProgressChanged != null, I get the error "The event 'C_Comm.ProgressChanged' can only appear on the left hand side of += or -=". I'm not very clear on using events! Do you have any ideas?

                    J Offline
                    J Offline
                    Jibesh
                    wrote on last edited by
                    #16

                    I dont see any error in your previous code other than two declaration of the delegates ProgressChangeHandler. is C_Comm.cs is defined in a common dll? can you give some more input about the project files where these .cs files are added. If it was me, I keep all the common implementations/interfaces in a shared dll and refer these dll in different projects rather keeping same .cs files each project. what are you trying to do here with this 'cb.ProgressChanged != null' checking for null right??

                    Jibesh V P

                    M 1 Reply Last reply
                    0
                    • J Jibesh

                      I dont see any error in your previous code other than two declaration of the delegates ProgressChangeHandler. is C_Comm.cs is defined in a common dll? can you give some more input about the project files where these .cs files are added. If it was me, I keep all the common implementations/interfaces in a shared dll and refer these dll in different projects rather keeping same .cs files each project. what are you trying to do here with this 'cb.ProgressChanged != null' checking for null right??

                      Jibesh V P

                      M Offline
                      M Offline
                      MichCl
                      wrote on last edited by
                      #17

                      C_Comm.cs is a separate VS project, separate dll, commonly referenced by everything (CR5.cs, PC.cs). Do you think I still need everything except the delegate part in my iCR? The CR5 implements iCR and that's the only way I can see getting the ProgressChanged defined in CR5 from PC.cs, but when I take out the delegate part, I get "The type or namespace name 'ProgressChangeHandler' could not be found (are you missing a using directive or an assembly reference?)". I can't keep my iCR.cs and C_Comm.cs in the same dll because of the whole layout. The VS project layout is: Project 1 has iCR.cs Project 2 has PC.cs and a couple of other things. (update - this has form with progress bar) Project 3 has CR5 (implements iCR) and a couple of other things. Project 4 has CR_Factory which will return a CR5 through the iCR interface to my PC.cs Project 5 has C_Comm.cs. It is referenced in PC.cs, CR5, and iCR. In the future I'll have a CR6 project like the CR5. As far as cb.ProgressChanged != null, I'm just trying to get what you suggested originally to work with my delegate in my C_Comm. Yes, checking for null before I send the update.

                      J 1 Reply Last reply
                      0
                      • M MichCl

                        C_Comm.cs is a separate VS project, separate dll, commonly referenced by everything (CR5.cs, PC.cs). Do you think I still need everything except the delegate part in my iCR? The CR5 implements iCR and that's the only way I can see getting the ProgressChanged defined in CR5 from PC.cs, but when I take out the delegate part, I get "The type or namespace name 'ProgressChangeHandler' could not be found (are you missing a using directive or an assembly reference?)". I can't keep my iCR.cs and C_Comm.cs in the same dll because of the whole layout. The VS project layout is: Project 1 has iCR.cs Project 2 has PC.cs and a couple of other things. (update - this has form with progress bar) Project 3 has CR5 (implements iCR) and a couple of other things. Project 4 has CR_Factory which will return a CR5 through the iCR interface to my PC.cs Project 5 has C_Comm.cs. It is referenced in PC.cs, CR5, and iCR. In the future I'll have a CR6 project like the CR5. As far as cb.ProgressChanged != null, I'm just trying to get what you suggested originally to work with my delegate in my C_Comm. Yes, checking for null before I send the update.

                        J Offline
                        J Offline
                        Jibesh
                        wrote on last edited by
                        #18

                        I dont see any problems here. did you refer Project5 in iCRS

                        Jibesh V P

                        M 1 Reply Last reply
                        0
                        • J Jibesh

                          I dont see any problems here. did you refer Project5 in iCRS

                          Jibesh V P

                          M Offline
                          M Offline
                          MichCl
                          wrote on last edited by
                          #19

                          I can't reference CR5 in iCR because that would be an incorrect implementation for factory design pattern. Plus, CR5 is an iCR and if iCR referenced CR5, it would be circular.

                          M 1 Reply Last reply
                          0
                          • M MichCl

                            I can't reference CR5 in iCR because that would be an incorrect implementation for factory design pattern. Plus, CR5 is an iCR and if iCR referenced CR5, it would be circular.

                            M Offline
                            M Offline
                            MichCl
                            wrote on last edited by
                            #20

                            I wound up leaving the original delegate in my iCR and everything you originally recommended above. Then I created a separate delegate and handler in my C_Comm.cs class. I couldn't see any other way to update my progressBar and also share the same delegate and event. Thanks for the help!

                            J 1 Reply Last reply
                            0
                            • M MichCl

                              I wound up leaving the original delegate in my iCR and everything you originally recommended above. Then I created a separate delegate and handler in my C_Comm.cs class. I couldn't see any other way to update my progressBar and also share the same delegate and event. Thanks for the help!

                              J Offline
                              J Offline
                              Jibesh
                              wrote on last edited by
                              #21

                              I dont see any error in your declaration, i.e the delegate method is declared in Comm.cs file which is referred in all the other 3(or more project) iCR,CR5 and form Class. so the delegate should be available in all other classes and you need to include the proper namespaces in other project files.

                              Jibesh V P

                              M 1 Reply Last reply
                              0
                              • J Jibesh

                                I dont see any error in your declaration, i.e the delegate method is declared in Comm.cs file which is referred in all the other 3(or more project) iCR,CR5 and form Class. so the delegate should be available in all other classes and you need to include the proper namespaces in other project files.

                                Jibesh V P

                                M Offline
                                M Offline
                                MichCl
                                wrote on last edited by
                                #22

                                Thank you so much for the help. We have a very small work group here, and they are not programmers and don't understand this stuff (not to mention me having a tough time with events and this architecture). It turned out I wasn't "using" C_Comm in my interface. Once I did that and fixed a couple of declarations to use the C_Comm version of the Handler, all worked. :)

                                J 1 Reply Last reply
                                0
                                • M MichCl

                                  Thank you so much for the help. We have a very small work group here, and they are not programmers and don't understand this stuff (not to mention me having a tough time with events and this architecture). It turned out I wasn't "using" C_Comm in my interface. Once I did that and fixed a couple of declarations to use the C_Comm version of the Handler, all worked. :)

                                  J Offline
                                  J Offline
                                  Jibesh
                                  wrote on last edited by
                                  #23

                                  Glad to hear this. Good Luck Jibesh

                                  Jibesh V P

                                  M 2 Replies Last reply
                                  0
                                  • J Jibesh

                                    Glad to hear this. Good Luck Jibesh

                                    Jibesh V P

                                    M Offline
                                    M Offline
                                    MichCl
                                    wrote on last edited by
                                    #24

                                    Thank you! And thanks for sticking with this thread.

                                    J 1 Reply Last reply
                                    0
                                    • M MichCl

                                      Thank you! And thanks for sticking with this thread.

                                      J Offline
                                      J Offline
                                      Jibesh
                                      wrote on last edited by
                                      #25

                                      You are welcome!! Cheers!

                                      Jibesh V P

                                      1 Reply Last reply
                                      0
                                      • J Jibesh

                                        Glad to hear this. Good Luck Jibesh

                                        Jibesh V P

                                        M Offline
                                        M Offline
                                        MichCl
                                        wrote on last edited by
                                        #26

                                        @jibesh - I added an interface (see my recent question), and my event is null when I try to update my progress bar. I know you have a good idea about events and interfaces. If you could take a look at my newly posted question(Thursday post maybe), I'd appreciate it. Thanks!

                                        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