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. Event handler Maddness

Event handler Maddness

Scheduled Pinned Locked Moved C#
csharpalgorithmsdata-structurestestingbeta-testing
7 Posts 4 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.
  • S Offline
    S Offline
    Squeaker
    wrote on last edited by
    #1

    I am trying to understand how to hook up some event handlers. Here is an analogy to my situation: I have an object tree that represents all passenger trains in a train station. It looks like this: Station .....Trains .........Train_0 ..............Cars ................Car_0 ...................Passengers ..........................Passenger_0 ..........................Passenger_1 .........Train_1 ..............etc... Each of these objects fire events (like when passengers board a car). I have a Form where I want to display status of Trains, Cars, Passengers etc. I need to attach the handlers in the Form, but must be done dynamically, and I have no clue. For testing I tried this, but I get a runtime error if the collections are empty: Station.Trains[1].Cars[5].CarsChanged += new CarsChangedEventHandler(Cars_Changed); I have spent a good bit of time searching for info. Any suggestions would most helpful. I'm guessing that C# has an elegant solution (in lieu of my duck tape). Thanks in advance-

    M B A 3 Replies Last reply
    0
    • S Squeaker

      I am trying to understand how to hook up some event handlers. Here is an analogy to my situation: I have an object tree that represents all passenger trains in a train station. It looks like this: Station .....Trains .........Train_0 ..............Cars ................Car_0 ...................Passengers ..........................Passenger_0 ..........................Passenger_1 .........Train_1 ..............etc... Each of these objects fire events (like when passengers board a car). I have a Form where I want to display status of Trains, Cars, Passengers etc. I need to attach the handlers in the Form, but must be done dynamically, and I have no clue. For testing I tried this, but I get a runtime error if the collections are empty: Station.Trains[1].Cars[5].CarsChanged += new CarsChangedEventHandler(Cars_Changed); I have spent a good bit of time searching for info. Any suggestions would most helpful. I'm guessing that C# has an elegant solution (in lieu of my duck tape). Thanks in advance-

      M Offline
      M Offline
      mav northwind
      wrote on last edited by
      #2

      Of course you get an error when the collection is empty, but that has nothing to do with event handlers. You're trying to access Car number 5 when there is no Car number 5 in the collection if it's empty. An event is usually bound to an instance of an object (there are static events, too, but they're far less common) and unless you have the instance you can't assign event handlers to it.

      Regards, mav -- Black holes are the places where god divided by 0...

      1 Reply Last reply
      0
      • S Squeaker

        I am trying to understand how to hook up some event handlers. Here is an analogy to my situation: I have an object tree that represents all passenger trains in a train station. It looks like this: Station .....Trains .........Train_0 ..............Cars ................Car_0 ...................Passengers ..........................Passenger_0 ..........................Passenger_1 .........Train_1 ..............etc... Each of these objects fire events (like when passengers board a car). I have a Form where I want to display status of Trains, Cars, Passengers etc. I need to attach the handlers in the Form, but must be done dynamically, and I have no clue. For testing I tried this, but I get a runtime error if the collections are empty: Station.Trains[1].Cars[5].CarsChanged += new CarsChangedEventHandler(Cars_Changed); I have spent a good bit of time searching for info. Any suggestions would most helpful. I'm guessing that C# has an elegant solution (in lieu of my duck tape). Thanks in advance-

        B Offline
        B Offline
        beatles1692
        wrote on last edited by
        #3

        Hi Well if you have a tree then you can enumarate it and hook up the event handlers: Something like this: foreach(Train train in this.trains) foreach(Car car in train.Cars) car.CarsChanged+=new CarsChangedEventHandler(Cars_Changed); if you are building the tree dynamically you can add the event handler on the fly for example Car car=new Car(); car.CarsChanged+=new CarsChangedEventHandler(Cars_Changed); this.SelectedTrain.AddCar(car); or //Somewhere in your domain public Car AddNewCarToTrain(Train train) { Car car=new Car(); //other initilizaing and validating code train.Cars.Add(car); return car; } //Then you can add your handler in your UI AddNewCarToTrain(this.selectedTrain).CarsChanged+=new CarsChangedEventHandler(Cars_Changed); Regards

        S 1 Reply Last reply
        0
        • S Squeaker

          I am trying to understand how to hook up some event handlers. Here is an analogy to my situation: I have an object tree that represents all passenger trains in a train station. It looks like this: Station .....Trains .........Train_0 ..............Cars ................Car_0 ...................Passengers ..........................Passenger_0 ..........................Passenger_1 .........Train_1 ..............etc... Each of these objects fire events (like when passengers board a car). I have a Form where I want to display status of Trains, Cars, Passengers etc. I need to attach the handlers in the Form, but must be done dynamically, and I have no clue. For testing I tried this, but I get a runtime error if the collections are empty: Station.Trains[1].Cars[5].CarsChanged += new CarsChangedEventHandler(Cars_Changed); I have spent a good bit of time searching for info. Any suggestions would most helpful. I'm guessing that C# has an elegant solution (in lieu of my duck tape). Thanks in advance-

          A Offline
          A Offline
          Andrei Ungureanu
          wrote on last edited by
          #4

          Hi, Try using a index property in your objects (like Tag property for labels). And then set the same event handler for all you Cars ojects (for example) and depending on the index you can do your work. In your event handler you have a sender object. You can use something like this: private void Cars_Changed(object sender,EventArgs e) { int index = ((Cars)object).index; } Hope it helps

          Do your best to be the best

          S 1 Reply Last reply
          0
          • A Andrei Ungureanu

            Hi, Try using a index property in your objects (like Tag property for labels). And then set the same event handler for all you Cars ojects (for example) and depending on the index you can do your work. In your event handler you have a sender object. You can use something like this: private void Cars_Changed(object sender,EventArgs e) { int index = ((Cars)object).index; } Hope it helps

            Do your best to be the best

            S Offline
            S Offline
            Squeaker
            wrote on last edited by
            #5

            This is what I wanted to do, but I need a little clarification. It looks like you are saying that all Car objects can fire the same event, and the eventArgs will include an index so I will know which car raised the event. If that is correct, then how do I declare the same event in each Car?? Thanks Squeaker

            A 1 Reply Last reply
            0
            • B beatles1692

              Hi Well if you have a tree then you can enumarate it and hook up the event handlers: Something like this: foreach(Train train in this.trains) foreach(Car car in train.Cars) car.CarsChanged+=new CarsChangedEventHandler(Cars_Changed); if you are building the tree dynamically you can add the event handler on the fly for example Car car=new Car(); car.CarsChanged+=new CarsChangedEventHandler(Cars_Changed); this.SelectedTrain.AddCar(car); or //Somewhere in your domain public Car AddNewCarToTrain(Train train) { Car car=new Car(); //other initilizaing and validating code train.Cars.Add(car); return car; } //Then you can add your handler in your UI AddNewCarToTrain(this.selectedTrain).CarsChanged+=new CarsChangedEventHandler(Cars_Changed); Regards

              S Offline
              S Offline
              Squeaker
              wrote on last edited by
              #6

              Thanks. I am building the tree dynamically. I knew there was a way to do this, but you made it very clear. I think I can make that work. Squeaker

              1 Reply Last reply
              0
              • S Squeaker

                This is what I wanted to do, but I need a little clarification. It looks like you are saying that all Car objects can fire the same event, and the eventArgs will include an index so I will know which car raised the event. If that is correct, then how do I declare the same event in each Car?? Thanks Squeaker

                A Offline
                A Offline
                Andrei Ungureanu
                wrote on last edited by
                #7

                Hi, Try using this code: private void Start() { Cars []myCars = new Cars[10]; for (int i = 0 ; i < myCars.Length ; i++) { //some other code myCars[i].Index = i; myCars[i].Click += new EventHandler(Cars_Click); } } private void Cars_Click(object sender,EventArgs e) { int index = ((Cars)sender).Index; } Hope this answers to your question

                Do your best to be the best

                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