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. Eventhandler in C# - How to send parameter

Eventhandler in C# - How to send parameter

Scheduled Pinned Locked Moved C#
questiontutorialcsharpdatabasedata-structures
8 Posts 3 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.
  • V Offline
    V Offline
    Vikas K
    wrote on last edited by
    #1

    Hi, My question is "How to send a user defined variable value while calling eventhandler. Pls go thru the below example: 1. I have an array of Pictureboxes arranged on a Panel 2. I have handled the Click event on PictureBoxes as below but dont know: how to pass "string FilePath" as part of "Object sender" /* I have a string value (FilePath) parameter which I want to send to the function ClickHandler which gets invoked whenerever someone clicks on a PictureBox */ string FilePath; //Has file path info picBoxes[index].Click += new System.EventHandler(ClickHandler); ... ... // Signature of ClickHandler method is as follows public void ClickHandler(Object sender, System.EventArgs e) { // I can get the handle to the PictureBox which was clicked by the user // as follows PictureBox pbx = new PictureBox(); pbx = (PictureBox) ((System.Windows.Forms.PictureBox)sender); // How do I access FilePath ? } Thanks

    M 1 Reply Last reply
    0
    • V Vikas K

      Hi, My question is "How to send a user defined variable value while calling eventhandler. Pls go thru the below example: 1. I have an array of Pictureboxes arranged on a Panel 2. I have handled the Click event on PictureBoxes as below but dont know: how to pass "string FilePath" as part of "Object sender" /* I have a string value (FilePath) parameter which I want to send to the function ClickHandler which gets invoked whenerever someone clicks on a PictureBox */ string FilePath; //Has file path info picBoxes[index].Click += new System.EventHandler(ClickHandler); ... ... // Signature of ClickHandler method is as follows public void ClickHandler(Object sender, System.EventArgs e) { // I can get the handle to the PictureBox which was clicked by the user // as follows PictureBox pbx = new PictureBox(); pbx = (PictureBox) ((System.Windows.Forms.PictureBox)sender); // How do I access FilePath ? } Thanks

      M Offline
      M Offline
      Martin 0
      wrote on last edited by
      #2

      Hello, I think what you want to do is only possible with an Inherited class and the usage of Custom Eventargs. 1) Inherit a class from PictureBox

      public class SpecialPictureBox : System.Windows.Forms.PictureBox

      1. Add a string property FilePath

      private string _filepath = "";
      [System.ComponentModel.DefaultValue("")]
      public string FilePath
      {
      get
      {
      return _filepath;
      }
      set
      {
      _filepath = value;
      }
      }

      3)Inherit a class from System.EventArgs with a string member

      public class SpecialEventArgs : System.EventArgs
      {
      public string filepath;

      public SpecialEventArgs(string \_filepath)
      {
      filepath= \_filepath;
      }
      

      }

      4)Create a delegate for your special event in your SpecialPictureBox class, which uses the SpecialEventArgs.

      public delegate void SpecialEvent(object sender, SpecialEventArgs specialEA);

      5)Create an event which uses the delegate

      public event SpecialEvent SpecialChanged;

      6)override the OnClick event of your PictureBox and fire the event, with the Filepath property.

      if(SpecialChanged!=null)
      {
      SpecialChanged(this, new SpecialEventArgs(this.FilePath));
      }

      7)In your Form Handle the SpecialChanged event and set the FilePath property

      yourSpecialPictureBox.FilePath = "yourpath";
      yourSpecialPictureBox.SpecialChanged+= new YourNamespace.SpecialEvent(yourSpecialPictureBox_SpecialChanged);

      private void yourSpecialPictureBox_SpecialChanged(object sender, SpecialEventArgs specialEA)
      {
      //specialEA.filepath
      }

      Hope it helps! -- modified at 5:35 Friday 27th July, 2007

      All the best, Martin

      V M 3 Replies Last reply
      0
      • M Martin 0

        Hello, I think what you want to do is only possible with an Inherited class and the usage of Custom Eventargs. 1) Inherit a class from PictureBox

        public class SpecialPictureBox : System.Windows.Forms.PictureBox

        1. Add a string property FilePath

        private string _filepath = "";
        [System.ComponentModel.DefaultValue("")]
        public string FilePath
        {
        get
        {
        return _filepath;
        }
        set
        {
        _filepath = value;
        }
        }

        3)Inherit a class from System.EventArgs with a string member

        public class SpecialEventArgs : System.EventArgs
        {
        public string filepath;

        public SpecialEventArgs(string \_filepath)
        {
        filepath= \_filepath;
        }
        

        }

        4)Create a delegate for your special event in your SpecialPictureBox class, which uses the SpecialEventArgs.

        public delegate void SpecialEvent(object sender, SpecialEventArgs specialEA);

        5)Create an event which uses the delegate

        public event SpecialEvent SpecialChanged;

        6)override the OnClick event of your PictureBox and fire the event, with the Filepath property.

        if(SpecialChanged!=null)
        {
        SpecialChanged(this, new SpecialEventArgs(this.FilePath));
        }

        7)In your Form Handle the SpecialChanged event and set the FilePath property

        yourSpecialPictureBox.FilePath = "yourpath";
        yourSpecialPictureBox.SpecialChanged+= new YourNamespace.SpecialEvent(yourSpecialPictureBox_SpecialChanged);

        private void yourSpecialPictureBox_SpecialChanged(object sender, SpecialEventArgs specialEA)
        {
        //specialEA.filepath
        }

        Hope it helps! -- modified at 5:35 Friday 27th July, 2007

        All the best, Martin

        V Offline
        V Offline
        Vikas K
        wrote on last edited by
        #3

        Oh wow, didnt think about it! let me try. Thank you Martin. :-D

        M 1 Reply Last reply
        0
        • V Vikas K

          Oh wow, didnt think about it! let me try. Thank you Martin. :-D

          M Offline
          M Offline
          Martin 0
          wrote on last edited by
          #4

          You are wellcome! Hope it works out for you. Please let us know.

          All the best, Martin

          1 Reply Last reply
          0
          • M Martin 0

            Hello, I think what you want to do is only possible with an Inherited class and the usage of Custom Eventargs. 1) Inherit a class from PictureBox

            public class SpecialPictureBox : System.Windows.Forms.PictureBox

            1. Add a string property FilePath

            private string _filepath = "";
            [System.ComponentModel.DefaultValue("")]
            public string FilePath
            {
            get
            {
            return _filepath;
            }
            set
            {
            _filepath = value;
            }
            }

            3)Inherit a class from System.EventArgs with a string member

            public class SpecialEventArgs : System.EventArgs
            {
            public string filepath;

            public SpecialEventArgs(string \_filepath)
            {
            filepath= \_filepath;
            }
            

            }

            4)Create a delegate for your special event in your SpecialPictureBox class, which uses the SpecialEventArgs.

            public delegate void SpecialEvent(object sender, SpecialEventArgs specialEA);

            5)Create an event which uses the delegate

            public event SpecialEvent SpecialChanged;

            6)override the OnClick event of your PictureBox and fire the event, with the Filepath property.

            if(SpecialChanged!=null)
            {
            SpecialChanged(this, new SpecialEventArgs(this.FilePath));
            }

            7)In your Form Handle the SpecialChanged event and set the FilePath property

            yourSpecialPictureBox.FilePath = "yourpath";
            yourSpecialPictureBox.SpecialChanged+= new YourNamespace.SpecialEvent(yourSpecialPictureBox_SpecialChanged);

            private void yourSpecialPictureBox_SpecialChanged(object sender, SpecialEventArgs specialEA)
            {
            //specialEA.filepath
            }

            Hope it helps! -- modified at 5:35 Friday 27th July, 2007

            All the best, Martin

            M Offline
            M Offline
            Mike Dimmick
            wrote on last edited by
            #5

            In .NET Framework 2.0, if you're following the EventArgs pattern, you don't need to declare your own delegate, you can use the generic EventHandler<T> delegate. Saves time and metadata space. So you'd have

            public event EventHandler<SpecialEventArgs> SpecialChanged;

            yourSpecialPictureBox.SpecialChanged +=
            new EventHandler<SpecialEventArgs>(yourSpecialPictureBox_SpecialChanged);

            I'm finding this pretty handy.

            Stability. What an interesting concept. -- Chris Maunder

            M 1 Reply Last reply
            0
            • M Mike Dimmick

              In .NET Framework 2.0, if you're following the EventArgs pattern, you don't need to declare your own delegate, you can use the generic EventHandler<T> delegate. Saves time and metadata space. So you'd have

              public event EventHandler<SpecialEventArgs> SpecialChanged;

              yourSpecialPictureBox.SpecialChanged +=
              new EventHandler<SpecialEventArgs>(yourSpecialPictureBox_SpecialChanged);

              I'm finding this pretty handy.

              Stability. What an interesting concept. -- Chris Maunder

              M Offline
              M Offline
              Martin 0
              wrote on last edited by
              #6

              Hello Mike,

              Mike Dimmick wrote:

              In .NET Framework 2.0,

              I'm one of the poor, who have to work with .Net1.1 only. But thanks for the info!

              All the best, Martin

              1 Reply Last reply
              0
              • M Martin 0

                Hello, I think what you want to do is only possible with an Inherited class and the usage of Custom Eventargs. 1) Inherit a class from PictureBox

                public class SpecialPictureBox : System.Windows.Forms.PictureBox

                1. Add a string property FilePath

                private string _filepath = "";
                [System.ComponentModel.DefaultValue("")]
                public string FilePath
                {
                get
                {
                return _filepath;
                }
                set
                {
                _filepath = value;
                }
                }

                3)Inherit a class from System.EventArgs with a string member

                public class SpecialEventArgs : System.EventArgs
                {
                public string filepath;

                public SpecialEventArgs(string \_filepath)
                {
                filepath= \_filepath;
                }
                

                }

                4)Create a delegate for your special event in your SpecialPictureBox class, which uses the SpecialEventArgs.

                public delegate void SpecialEvent(object sender, SpecialEventArgs specialEA);

                5)Create an event which uses the delegate

                public event SpecialEvent SpecialChanged;

                6)override the OnClick event of your PictureBox and fire the event, with the Filepath property.

                if(SpecialChanged!=null)
                {
                SpecialChanged(this, new SpecialEventArgs(this.FilePath));
                }

                7)In your Form Handle the SpecialChanged event and set the FilePath property

                yourSpecialPictureBox.FilePath = "yourpath";
                yourSpecialPictureBox.SpecialChanged+= new YourNamespace.SpecialEvent(yourSpecialPictureBox_SpecialChanged);

                private void yourSpecialPictureBox_SpecialChanged(object sender, SpecialEventArgs specialEA)
                {
                //specialEA.filepath
                }

                Hope it helps! -- modified at 5:35 Friday 27th July, 2007

                All the best, Martin

                V Offline
                V Offline
                Vikas K
                wrote on last edited by
                #7

                Yes, This solution works properly! :) though a bit advanced for me! thanks

                M 1 Reply Last reply
                0
                • V Vikas K

                  Yes, This solution works properly! :) though a bit advanced for me! thanks

                  M Offline
                  M Offline
                  Martin 0
                  wrote on last edited by
                  #8

                  Glad I could help! Thanks for the feedback! :)

                  All the best, Martin

                  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