.Net Events

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Venkadesan
    New Member
    • Aug 2008
    • 7

    .Net Events

    Hai Good Morning,

    I have a doubt in event is that can we handle the event in another from which is raised from some other form in c#. I need it..

    Thanks.
    Venkat
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    Not really sure what you are needed. Does this help?

    Helpful link

    Comment

    • cloud255
      Recognized Expert Contributor
      • Jun 2008
      • 427

      #3
      Ok, so if i unserstand you right, you have multiple forms and you raise an event on one form and you want another form to implement the event handler for that event?

      Comment

      • Venkadesan
        New Member
        • Aug 2008
        • 7

        #4
        Originally posted by cloud255
        Ok, so if i unserstand you right, you have multiple forms and you raise an event on one form and you want another form to implement the event handler for that event?

        Yes Exactly you understood the problem, I need help,

        Thanks

        Comment

        • r035198x
          MVP
          • Sep 2006
          • 13225

          #5
          Originally posted by Venkadesan
          Yes Exactly you understood the problem, I need help,

          Thanks
          Do you have have a reference of that form in the Form that triggered the event?
          All you need to do then is call a method from that handler class. See the skeleton code below

          Code:
          class A :Form {
          //reference to the handler class. Make sure this is intitialized at some point
          B b;
                private void XXButtonClicked(object sender,EventArgs e)
                 {
                    b.Handler();
                }
          }
          
          class B : Form{
          ...
              public void Handler();
          }

          Comment

          • cloud255
            Recognized Expert Contributor
            • Jun 2008
            • 427

            #6
            if you are using MDI controls, you can also access the second form via the collection of MDIChildren.

            This approach allows you to use methods instead of event handlers, its not a better solution, but it is an easier solution

            Comment

            • Venkadesan
              New Member
              • Aug 2008
              • 7

              #7
              Originally posted by cloud255
              if you are using MDI controls, you can also access the second form via the collection of MDIChildren.

              This approach allows you to use methods instead of event handlers, its not a better solution, but it is an easier solution

              Ok Thank you for your contribution,

              Comment

              • tlhintoq
                Recognized Expert Specialist
                • Mar 2008
                • 3532

                #8
                I'm new at this too, so if I am wrong in my understanding or perspective someone more experienced please correct me.

                But if the goal of C# is to be more 'event driven' than 'method driven' wouldn't it be better to have one form's reaction triggered by an event being raised in the other form, rather than a method calling it? If a method of one directly calls the method of another then the first method will break if the other form does not exist. So the code is less durable and harder to reuse from one project to the next. But by using events you could have 5 different forms or controls react to the raising of one event without any one of the children being aware of the existance of the other.

                Lets say Form1 is the parent of Form2 and Form3. Since it created them, it knows of them, therefore it can assign an event from one to a method in the other. I do this with user controls and don't know of a reason it shouldn't work with forms as they are just higher ranking controls, right?

                Code:
                        private void assignPrivilegesToolStripMenuItem_Click(object sender, EventArgs e)
                        {
                            MainLog("Assign privileges");
                
                            ClearContentPanel();
                            SignIn mySignin = new SignIn();
                            toolStripContainer1.ContentPanel.Controls.Add(mySignin);
                            PrivCtrl myPrivCtrl = new PrivCtrl();
                            myPrivCtrl.Location = new Point(mySignin.Bounds.Left, mySignin.Bounds.Bottom + 10);
                            mySignin.LogInClicked += new SignIn.LogInClickHandler(myPrivCtrl.Signin_LogInClicked);
                            toolStripContainer1.ContentPanel.Controls.Add(myPrivCtrl);
                            ResizeToContentPanel();
                        }
                In this example SignIn is a user control and PrivCtrl is a user control. They could just as easily be other forms. There is no code in SignIn that directly knows about such as thing as a PrivCtrl. And a PrivCtrl has no knowledge of what a SignIn control is. But when Form1 creates them both it can (in line 10) subscribe the PrivCtrl to react to the LogInClicked event of the SignIn control.

                Both controls (or forms in your need) retain their indepedence and anonimity. Both controls can easily be picked up an placed in another project without breaking them.

                Comment

                • cloud255
                  Recognized Expert Contributor
                  • Jun 2008
                  • 427

                  #9
                  Originally posted by tlhintoq
                  But if the goal of C# is to be more 'event driven' than 'method driven' wouldn't it be better to have one form's reaction triggered by an event being raised in the other form, rather than a method calling it? If a method of one directly calls the method of another then the first method will break if the other form does not exist. So the code is less durable and harder to reuse from one project to the next. But by using events you could have 5 different forms or controls react to the raising of one event without any one of the children being aware of the existance of the other.
                  You are rigth it is better to have events as this makes use of loose coupling which has many advantagtes (not gonna go into that now) but event driven programming, especially inderstanding event subscriptions inversion of control, custom events with custom objects as arguments can be alot for a beginner to grasp. As i said earlier using methods is not a better solution, its an easier solution as most people are more comfortable with methods than events.

                  The code anaylysis feature of VS2008 only complains about direct references (methods) to other classes when you have something like 80 method calls to a particular class. Often the decision on how tightly coupled an application is, is up to the developers preferences and skill rather than good analysis.

                  We are kind of stealing this thread, so if yo want to discuss this further (i'm more than happy too) we should start a thread somewhere, maybe in software discussion or the lounge.

                  Comment

                  • tlhintoq
                    Recognized Expert Specialist
                    • Mar 2008
                    • 3532

                    #10
                    Originally posted by cloud255
                    You are rigth it is better to have events as this makes use of loose coupling which has many advantagtes (not gonna go into that now) but event driven programming, especially inderstanding event subscriptions inversion of control, custom events with custom objects as arguments can be alot for a beginner to grasp. As i said earlier using methods is not a better solution, its an easier solution as most people are more comfortable with methods than events.

                    The code anaylysis feature of VS2008 only complains about direct references (methods) to other classes when you have something like 80 method calls to a particular class. Often the decision on how tightly coupled an application is, is up to the developers preferences and skill rather than good analysis.

                    We are kind of stealing this thread, so if yo want to discuss this further (i'm more than happy too) we should start a thread somewhere, maybe in software discussion or the lounge.
                    I'm game, but new enough to all of this to know I don't know much. If you care to set up the discussion or point me in the right direction for the area you had in mind...

                    Comment

                    • alarock
                      New Member
                      • Jan 2008
                      • 14

                      #11
                      did you using MDI.


                      If you are using MDI (Multiple Document Interface)


                      Form1 childForm=(Form 1)this.ActiveMd iChid;
                      childForm.callE vent(sender,e) // callEvent belong to child.



                      If Any clarification needed,feel free to ask me.

                      Comment

                      • PRR
                        Recognized Expert Contributor
                        • Dec 2007
                        • 750

                        #12
                        "I have a doubt in event is that can we handle the event in another from which is raised from some other form in c#. I need it.. "

                        ok.. from what i have understood ... i think you could declare:
                        public static form1 f1;
                        public static form1 f2;

                        in your main form.. then on click event of any form.. u can initialize form object .. and do the necessary work...see if it works this way...

                        Comment

                        Working...