Forum |  HardWare.fr | News | Articles | PC | S'identifier | S'inscrire | Shop Recherche
2450 connectés 

  FORUM HardWare.fr
  Programmation
  C#/.NET managed

  Passer un argument à travers un Event handler

 


 Mot :   Pseudo :  
 
Bas de page
Auteur Sujet :

Passer un argument à travers un Event handler

n°2000951
ledim97
Posté le 11-06-2010 à 16:23:10  profilanswer
 

Bonjour ,
mon probleme est énnoncé dans le titre ...  
je suis completement perdu ...  
 
je vous met le code :  
 

Code :
  1. private void menuItem1_Click(object sender, System.EventArgs e)
  2.             {
  3.                 Process.Start(new ProcessStartInfo(/*Path XML à ouvrir*/));
  4.             }
  5. //La méthode en haut est celle que j'appelerais a chaque radmenuitem item que j'aurais crée avec un parametre différent selon le XML  
  6.             private void mAJRecentFile()
  7.             {
  8.                 radMenuItem6.Items.Clear();
  9.                 FileInfo f;
  10.                 foreach (string path in listeRecent)
  11.                 {
  12.                     f = new FileInfo(path);
  13.                     RadMenuItem n = new RadMenuItem(f.Name);
  14.                     n.Click += new EventHandler(menuItem1_Click);
  15.                     radMenuItem6.Items.AddRange(n);
  16.                 }
  17.             }


 
Tout fonctionne il ne me manque que l'argument a faire passer dans méthode menuItem1_Click ...
 
Please , Help...

mood
Publicité
Posté le 11-06-2010 à 16:23:10  profilanswer
 

n°2000958
Harkonnen
Modérateur
Un modo pour les bannir tous
Posté le 11-06-2010 à 16:39:51  profilanswer
 

La classe EventArgs que tu transmets au handler est faite pour ça... A toi de l'utiliser, ou bien de créer ta propre classe qui en héritera.


---------------
J'ai un string dans l'array (Paris Hilton)
n°2000971
TotalRecal​l
Posté le 11-06-2010 à 17:16:41  profilanswer
 

Si j'ai bien compris la question,
- d'une part dans l'event handler tu dois accéder au paramètre sender, qui est le radmenuitem sur lequel l'utilisateur a cliqué (à caster en conséquence)
- de cette façon, tu peux accéder dessus aux propriétés qui le caractérise : son nom si ça te suffit, ou bien un truc du genre DataItem que tu auras pris soin d'affecter lors de l'instantiation du contrôle avec le "f" correspondant dans ta methode MAJtruc.


Message édité par TotalRecall le 11-06-2010 à 17:17:13

---------------
Réalisation amplis classe D / T      Topic .Net - C# @ Prog
n°2000975
TotalRecal​l
Posté le 11-06-2010 à 17:21:25  profilanswer
 

Allé en express :  
 

Code :
  1. private void menuItem1_Click(object sender, System.EventArgs e)
  2.             {
  3.                     RadMenuItem item = sender as RadMenuItem;
  4.                     if(item != null)
  5.                     {
  6.                        FileInfo file = item.DataItem as File;
  7.                        if(file != null)
  8.                        {
  9.                            Process.Start(new ProcessStartInfo(file.FullName);
  10.                         }
  11.                     }
  12.             }
  13. //La méthode en haut est celle que j'appelerais a chaque radmenuitem item que j'aurais crée avec un parametre différent selon le XML  
  14.             private void mAJRecentFile()
  15.             {
  16.                 radMenuItem6.Items.Clear();
  17.                 FileInfo f;
  18.                 foreach (string path in listeRecent)
  19.                 {
  20.                     f = new FileInfo(path);
  21.                     RadMenuItem n = new RadMenuItem(f.Name);
  22.                     n.DataItem = f;
  23.                     n.Click += new EventHandler(menuItem1_Click);
  24.                     radMenuItem6.Items.AddRange(n);
  25.                 }
  26.             }


Ou un truc du genre.


---------------
Réalisation amplis classe D / T      Topic .Net - C# @ Prog
n°2001693
ledim97
Posté le 15-06-2010 à 10:32:54  profilanswer
 

Rebonjour a tous , merci pour vos réponse ,
 
Je n'ai toujours pas reussi a faire passer mon argument :( la méthode TotalRecall ne fonctionne pas ==>  
 
 
Erreur 2 'Telerik.WinControls.UI.RadMenuItem' does not contain a definition for 'DataItem' and no extension method 'DataItem' accepting a first argument of type 'Telerik.WinControls.UI.RadMenuItem' could be found (are you missing a using directive or an assembly reference?)  
 
 
Et je n'arrive toujours pas avec les Event handler a faire passer un argument :(  

n°2001696
TotalRecal​l
Posté le 15-06-2010 à 10:38:08  profilanswer
 

Marrant. http://www.telerik.com/help/aspnet/menu/radmenu-
telerik.webcontrols.radmenuitem-dataitem.html
Je ne connais pas ce contrôle Telerik, j'ai juste regardé la doc avec le nom que tu donnais...


---------------
Réalisation amplis classe D / T      Topic .Net - C# @ Prog
n°2001698
Harkonnen
Modérateur
Un modo pour les bannir tous
Posté le 15-06-2010 à 10:41:34  profilanswer
 

ledim97 a écrit :


Et je n'arrive toujours pas avec les Event handler a faire passer un argument :(  


c'est pourtant pas dur, voici un exemple tiré de cette page : http://msdn.microsoft.com/en-us/li [...] S.71).aspx
 

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


---------------
J'ai un string dans l'array (Paris Hilton)

Aller à :
Ajouter une réponse
  FORUM HardWare.fr
  Programmation
  C#/.NET managed

  Passer un argument à travers un Event handler

 

Sujets relatifs
Passer d'une simulation JAVA sous ExcelPassage d'un tableau de pointeurs de fonctions en argument
Creer des listes d'ojets et se les passer entres fonctionsMacro Excel: Pivottables: Argument ou Appel de procédure incorrect
Passer d'une séquences à l'autre [flash]hover css sans passer par le background
Got Error 12 from table handlerVBS -> bat avec argument
Passer d'un outil développer en vba excel à une interface web[Python3] tkinter: Scale Comment faire passer un argument
Plus de sujets relatifs à : Passer un argument à travers un Event handler


Copyright © 1997-2022 Hardware.fr SARL (Signaler un contenu illicite / Données personnelles) / Groupe LDLC / Shop HFR