IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

ASP.NET Discussion :

comment créer un nouvel arguement pour un eventHandler


Sujet :

ASP.NET

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre éclairé
    Profil pro
    Inscrit en
    Septembre 2006
    Messages
    279
    Détails du profil
    Informations personnelles :
    Localisation : Maroc

    Informations forums :
    Inscription : Septembre 2006
    Messages : 279
    Par défaut comment créer un nouvel arguement pour un eventHandler
    Bonjour;

    j'ai l'instruction suivante :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
      link.Click += new EventHandler(Link_Click(22) ;
     
            protected void Link_Click( object sender, EventArgs e,int monParam)
            {
     
             //mon code ici
            }
    en fait je veux passer un argument supplémentaire quand je clique sur mon lien , mais j'ai eu une erreur que je peux pas le faire ainsi , en cherchant j'ai trouvé la solution suivante sur un autre forum :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
     
    using System;
     
    // FireEventArgs: a custom event inherited from EventArgs.
     
    public class FireEventArgs: EventArgs {
        public FireEventArgs(string room, int ferocity) {
            this.room = room;
            this.ferocity = ferocity;
        }
     
        // The fire event will have two pieces of information-- 
        // 1) Where the fire is, and 2) how "ferocious" it is.  
     
        public string room;
        public int ferocity;
     
    }    //end of class FireEventArgs
     
     
    // Class with a function that creates the eventargs and initiates the event
    public class FireAlarm {
     
        // Events are handled with delegates, so we must establish a FireEventHandler
        // as a delegate:
     
        public delegate void FireEventHandler(object sender, FireEventArgs fe);
     
        // Now, create a public event "FireEvent" whose type is our FireEventHandler delegate. 
     
        public event FireEventHandler FireEvent;    
     
        // This will be the starting point of our event-- it will create FireEventArgs,
        // and then raise the event, passing FireEventArgs. 
     
        public void ActivateFireAlarm(string room, int ferocity) {
     
            FireEventArgs fireArgs = new FireEventArgs(room, ferocity);
     
            // Now, raise the event by invoking the delegate. Pass in 
            // the object that initated the event (this) as well as FireEventArgs. 
            // The call must match the signature of FireEventHandler.
     
            FireEvent(this, fireArgs); 
        }
    }    // end of class FireAlarm
     
     
    // Class which handles the event
     
    class FireHandlerClass {
     
        // Create a FireAlarm to handle and raise the fire events. 
     
        public FireHandlerClass(FireAlarm fireAlarm)    {
     
            // Add a delegate containing the ExtinguishFire function to the class'
            // event so that when FireAlarm is raised, it will subsequently execute 
            // ExtinguishFire.
     
            fireAlarm.FireEvent += new FireAlarm.FireEventHandler(ExtinguishFire);
        }
     
        // This is the function to be executed when a fire event is raised. 
     
        void ExtinguishFire(object sender, FireEventArgs fe) {
     
            Console.WriteLine("\nThe ExtinguishFire function was called by {0}.", sender.ToString());
     
            // Now, act in response to the event.
     
            if (fe.ferocity < 2)
                Console.WriteLine("This fire in the {0} is no problem.  I'm going to pour some water on it.", fe.room);
            else if (fe.ferocity < 5)
                Console.WriteLine("I'm using FireExtinguisher to put out the fire in the {0}.",  fe.room);
            else 
                Console.WriteLine("The fire in the {0} is out of control.  I'm calling the fire department!", fe.room);
        }
    }    //end of class FireHandlerClass
     
    public class FireEventTest {
        public static void Main ()     {    
     
            // Create an instance of the class that will be firing an event.
     
            FireAlarm myFireAlarm = new FireAlarm();
     
            // Create an instance of the class that will be handling the event. Note that 
            // it receives the class that will fire the event as a parameter. 
     
            FireHandlerClass myFireHandler = new FireHandlerClass(myFireAlarm);
     
            //use our class to raise a few events and watch them get handled
            myFireAlarm.ActivateFireAlarm("Kitchen", 3);
            myFireAlarm.ActivateFireAlarm("Study", 1);
            myFireAlarm.ActivateFireAlarm("Porch", 5);
     
            return;
     
        }    //end of main
     
    }    // end of FireEventTest
    je l'ai implémenté mais malheureusement je ne vois pas comment je pourrai passer la valeur de mon argument supplémentaire, car l'instruction est définie ainsi
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    fireAlarm.FireEvent += new FireAlarm.FireEventHandler(ExtinguishFire);
    avec "ExtinguishFire" le nom de la fonction à exécuter qui est dans mon cas "Link_Click" comment je mettrai "monParam" à 22 ??

    je me perds franchement

    Merci.

  2. #2
    Rédacteur/Modérateur


    Homme Profil pro
    Développeur .NET
    Inscrit en
    Février 2004
    Messages
    19 875
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 43
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Développeur .NET
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Février 2004
    Messages : 19 875
    Par défaut
    Tu peux faire comme ça :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    link.Click += new EventHandler((sender, e) => Link_Click(sender, e, 22)) ;

  3. #3
    Membre éclairé
    Profil pro
    Inscrit en
    Septembre 2006
    Messages
    279
    Détails du profil
    Informations personnelles :
    Localisation : Maroc

    Informations forums :
    Inscription : Septembre 2006
    Messages : 279
    Par défaut
    Bonjour;

    alors si j'ai bien compris j'ai pas besoin de créer toutes ces classes là ?


  4. #4
    Rédacteur/Modérateur


    Homme Profil pro
    Développeur .NET
    Inscrit en
    Février 2004
    Messages
    19 875
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 43
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Développeur .NET
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Février 2004
    Messages : 19 875
    Par défaut
    Citation Envoyé par zanoubya Voir le message
    alors si j'ai bien compris j'ai pas besoin de créer toutes ces classes là ?
    Non, en tous cas pas dans ce cas précis.

Discussions similaires

  1. Comment créer une nouvelle IHM pour la création de classes
    Par walid0577 dans le forum Eclipse Platform
    Réponses: 2
    Dernier message: 22/08/2011, 15h14
  2. Comment créer une nouvelle fenetre pour recherche
    Par RaimS dans le forum Général JavaScript
    Réponses: 5
    Dernier message: 02/06/2006, 16h02
  3. comment créer un add-in pour Word 2000 - XP2003 ???
    Par gide_x dans le forum API, COM et SDKs
    Réponses: 1
    Dernier message: 13/11/2005, 19h22
  4. Réponses: 10
    Dernier message: 02/11/2005, 11h12

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo