Beispiel #1
0
        public void LiftButtonHandler(object sender, EventArgs e)
        {
            // Each button has a tag associated with it. The tag is two digits - the first specifies
            // the lift the button belongs to, the second is the floor specified. e.g. 23 is lift 2, floor 3.
            // This handler will handle all the lift buttons without having to write a Click event handler
            // for each individual button.

            // The sender will be one of the lift buttons, so cast the sender to a Button object.
            Button buttonSender = (Button)sender;

            // Cast the Tag to a string
            string buttonTag = (string)buttonSender.Tag;

            // Get the lift and floor that the sender button is referring to.
            // Strings are arrays of characters, so we can get the individual characters we need
            // FIXME: Convert.ToInt32 can parse characters, but returns the decimal ASCII value of those characters
            // So we need to get the character, convert it to a string, then convert it to an int.
            // Casting to an integer doesn't work either...
            int lift  = Int32.Parse(buttonTag[0].ToString());
            int floor = Int32.Parse(buttonTag[1].ToString());

            bool destOK = false;

            switch (lift)
            {
            case 1:
                if (lift1.GetCurrentFloor != floor && !lift1.IsDest(floor))
                {
                    lift1.AddDest(floor); destOK = true;
                }
                break;

            case 2:
                if (lift2.GetCurrentFloor != floor && !lift2.IsDest(floor))
                {
                    lift2.AddDest(floor); destOK = true;
                }
                break;

            case 3:
                if (lift3.GetCurrentFloor != floor && !lift3.IsDest(floor))
                {
                    lift3.AddDest(floor); destOK = true;
                }
                break;

            default:
                break;
            }

            if (destOK)
            {
                buttonSender.BackColor = Color.DarkRed;
                buttonSender.ForeColor = Color.White;
            }
        }