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. WCF and WF
  4. Event !

Event !

Scheduled Pinned Locked Moved WCF and WF
databasetutorial
2 Posts 2 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.
  • R Offline
    R Offline
    RongNK
    wrote on last edited by
    #1

    I have a list: List ImgRow1 = new List(); and i set: foreach (Image item in ImgRow1) { item.MouseDown += new MouseButtonEventHandler(item_MouseDown); } in private void item_MouseDown(object sender, MouseButtonEventArgs e) { } and i want to get index of sender (for example: ImgRow1[2] => index=2 ). Sorry for my english. Thank !

    N 1 Reply Last reply
    0
    • R RongNK

      I have a list: List ImgRow1 = new List(); and i set: foreach (Image item in ImgRow1) { item.MouseDown += new MouseButtonEventHandler(item_MouseDown); } in private void item_MouseDown(object sender, MouseButtonEventArgs e) { } and i want to get index of sender (for example: ImgRow1[2] => index=2 ). Sorry for my english. Thank !

      N Offline
      N Offline
      Nigel Ferrissey
      wrote on last edited by
      #2

      If ImgRow1 is a property like:

      public List<Image> ImgRow1 { get; set; }

      and is in scope in the event handler, then you can modify the event handler like this:

      private void item_MouseDown(object sender, MouseButtonEventArgs e)
      {
      Image myImage = (Image)sender;
      int index = ImgRow1.IndexOf(myImage);

              MessageBox.Show(index.ToString());
          }
      

      Or, You could create your own "IndexedImage" class with a ListIndex property, inheriting from Image and use that.

      public class IndexedImage : Image
      {
      public int ListIndex { get; set; }

          public IndexedImage()
          {
          }
      }
      

      When you add the event handler, add the list index as well:

      foreach (IndexedImage item in ImgRow1)
      {
      item.MouseDown +=new MouseButtonEventHandler(item_MouseDown);
      item.ListIndex = ImgRow1.IndexOf(item);
      }

      Then in the event handler you can get the ListIndex property:

      private void item_MouseDown(object sender, MouseButtonEventArgs e)
      {
      IndexedImage myImage = (IndexedImage)sender;
      MessageBox.Show(myImage.ListIndex.ToString());
      }

      but that has other issues, in that the index may change later on, so the class would need to be more complicated in reality.

      modified on Sunday, October 11, 2009 4:51 PM

      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