Results 1 to 5 of 5

Thread: Events Help

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Feb 2002
    Location
    North Carolina
    Posts
    131

    Events Help

    I am learning C# after working with VB6 for about three years and I am having one heck of a time with EVENTS in C#. I know that I have to use DELEGATES but here is the thing. Let’s say I have one cs file Called "processing" and a Class in that file called "processing" and then I also have a form1.cs file with a class called "formclass". Now, let’s say I am processing some database records or some other data and I want to raise an EVENT in the "Processing" class of the "Processing.cs" file to update a progress bar on form1.cs. How can I do this? Every example or tutorial I have seen on EVENTS have the events triggered from the form class. I know this can be done, but the whole DELEGATES and EVENTS thing seems to be really confusing.

  2. #2
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256

    Re: Events Help

    In the form that you want to update the progress bar, just subscribe to the event then each time the event is raised you can update your progress bar.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Feb 2002
    Location
    North Carolina
    Posts
    131

    Re: Events Help

    Thats the problem...every tutorial or sample that I have seen shows the delegate and event declared and handled in a class, but the triggering of the event takes place on the form side. I am not sure how to flip this arround. I have attempted to alter a sample from MS and it errors on every alteration. Change it back to fire the event from the form and no errors occur
    In VB this was soooo simple declare event, raise event, dim with events and the code to do whatever you want. I know it has to be something simple that I am missing, I'm just not sure what it is. Another thing I find confusing is the references to (object sender, EventArgs e) this also seems to cause problems in the sample that I have been working with. I am just lost!

  4. #4
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,177

    Re: Events Help

    Not sure exactly what you're looking for, but here's a very basic example of implementing events and delegates.

    In a class called "ProgressEventArgs":
    Code:
    using System;
    
    namespace EventsExample
    {
       /// <summary>
       /// Basic Progress Event Arguments class
       /// Inherits the "EventArgs" class
       /// </summary>
       public class ProgressEventArgs : EventArgs
       {
          // Class property value variables
          private int m_value = 0;
          private int m_min   = 0;
          private int m_max   = 0;
    
          // Overloaded constructor which accepts the Value, 
          // Min and Max properties as arguments
          public ProgressEventArgs(int value, int min, int max):base()
          {
             m_value = value;
             m_min = min;
             m_max = max;
          }
    
          // Property accessor for getting/setting the Value property
          public int Value
          {
             get{return m_value;}
             set{m_value = value;}
          }
    
          // Property accessor for getting/setting the Min property
          public int Min
          {
             get{return m_min;}
             set{m_min = value;}
          }
    
          // Property accessor for getting/setting the Max property
          public int Max
          {
             get{return m_max;}
             set{m_max = value;}
          }
       }
    }
    In a class called "Progress":
    Code:
    using System;
    
    namespace EventsExample
    {
       /// <summary>
       /// Basic Progress Class
       /// </summary>
    	public class Progress
    	{
          // Event Delegate (Defines what the event will look like)
          // In this case there will be a "sender" argument which will contain
          // an "object" representing the class raising the event and 
          // an "e" argument of "ProgressEventArgs" type which will contain
          // information relevant to why the event was raised.
          public delegate void ProgressUpdatedEventHandler(object sender, ProgressEventArgs e);
    		
          // Event defined using the established delegate, defines what the event will be called
          public event ProgressUpdatedEventHandler ProgressUpdated;
    
          // Variables used to hold the property 
          // values for this class
          private int m_value = 0;
          private int m_min = 0;
          private int m_max = 0;
    
          // Standard Constructor
          public Progress()
          {
          }
    
          // Overloaded Constructor which accepts the Minimum and 
          // Maximum progress values as arguments
          public Progress(int min, int max)
    		{
             m_min = min;
             m_max = max;
    		}
    
          // Property accessor for getting/setting the progress Value 
          public int Value
          {
             get{return m_value;}
             set
             {
                // Make sure the value being assigned doesn't exceed
                // the min/max boundaries
                if( value > m_max ) value = m_max;
                if( value < m_min ) value = m_min;
                m_value = value;
    
                // As the value is being updated, trigger the Update event
                OnUpdateProgress();
             }
          }
    
          // Property accessor for getting/setting the progress Minimum value
          public int Min
          {
             get{return m_min;}
             set{m_min = value;}
          }
    
          // Property accessor for getting/setting the progress Maximum alue 
          public int Max
          {
             get{return m_max;}
             set{m_max = value;}
          }
    
          // This method simply executes the event, passing in the appropriate 
          // event arguments, which are "this" class as the "sender" and 
          // the Value, Min and Max values in the form of a ProgressEventArgs class
          protected void OnUpdateProgress()
          {
             // Only raise the event if someone has subscribed to it
             if( ProgressUpdated != null )
                ProgressUpdated(this, new ProgressEventArgs(m_value, m_min, m_max));
          }
    	}
    }
    Then in a form containing a ProgressBar control and a Button:

    Code:
          // Define a form level variable of type "Progress"
          private Progress m_progress = null;
    ...
    Code:
    		public Form1()
    		{
    			InitializeComponent();
             m_progress = new Progress();
             m_progress.ProgressUpdated +=new Progress.ProgressUpdatedEventHandler(OnProgressUpdated);
    		}
    ...
    Code:
          private void button1_Click(object sender, System.EventArgs e)
          {
             // Some imaginary process that uses the Progress class to track progress:
             m_progress.Min = 0;
             m_progress.Max = 100;
             for(int i=0; i<1000000; i++)
                m_progress.Value = i / 10000;
          }
    
          private void OnProgressUpdated(object sender, ProgressEventArgs e)
          {
             progressBar1.Minimum = e.Min;
             progressBar1.Maximum = e.Max;
             progressBar1.Value = e.Value;
          }

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Feb 2002
    Location
    North Carolina
    Posts
    131

    Re: Events Help

    This is exactly what I was looking for. Now I can step through the code and see exactly what is happening. I just needed it spelled out for me, that all!

    Seriously, being able to see what's going on makes a difference in understanding.

    Thanks!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width