I am stuck on getting my program working, basically i am building a elevator simulation with multiple elevators which move simultaneously so i decided to use threads to do so. Thats when the problem manifested.

Basically i have a set of buttons indicating which floor the person would like to vist then the elevator travels there. However in the code below i have a problem i am trying to use a switch statement to move the elevator depending on the button press. I cannot however find away of detecting the button press to pass into my switch statement which will then execute the correct code.

public Form1()
        {
            InitializeComponent();
            button12.Click += new System.EventHandler(clickedButton);
            button11.Click += new System.EventHandler(clickedButton);
            button10.Click += new System.EventHandler(clickedButton);
            button9.Click += new System.EventHandler(clickedButton);
            button8.Click += new System.EventHandler(clickedButton);
            button7.Click += new System.EventHandler(clickedButton);
        }

        Thread e1;


public void thread()
        {
            int loc = elevator1.Location.Y;

            switch (sender)
            {
                case (button12_Click):

                    if (loc < 288)
                    {
                        while (loc != 308)
                        {
                            elevator1.Location = new Point(3, loc);
                            loc++;
                            Thread.Sleep(10);
                        }
                    }
                    else
                    {
                        if (loc > 389)
                        {
                            while (loc != 308)
                            {
                                elevator1.Location = new Point(3, loc);
                                loc--;
                                Thread.Sleep(10);
                            }
                        }
                    }
                    break;

                case (button11_Click):
                    if (loc < 246)
                    {
                        while (loc != 246)
                        {
                            elevator1.Location = new Point(3, loc);
                            loc++;
                            Thread.Sleep(10);
                        }
                    }
                    else
                    {
                        if (loc > 246)
                        {
                            while (loc != 246)
                            {
                                elevator1.Location = new Point(3, loc);
                                loc--;
                                Thread.Sleep(10);
                            }
                        }
                    }
                    break;

                case (button10_Click):

                    if (loc < 185)
                    {
                        while (loc != 185)
                        {
                            elevator1.Location = new Point(3, loc);
                            loc++;
                            Thread.Sleep(10);
                        }
                    }
                    else
                    {
                        if (loc > 185)
                        {
                            while (loc != 185)
                            {
                                elevator1.Location = new Point(3, loc);
                                loc--;
                                Thread.Sleep(10);
                            }
                        }
                    }

                    break;

                case (button9_Click):

                    if (loc < 125)
                    {
                        while (loc != 125)
                        {
                            elevator1.Location = new Point(3, loc);
                            loc++;
                            Thread.Sleep(10);
                        }
                    }
                    else
                    {
                        if (loc > 125)
                        {
                            while (loc != 125)
                            {
                                elevator1.Location = new Point(3, loc);
                                loc--;
                                Thread.Sleep(10);
                            }
                        }
                    }
                    break;

                case (button8_Click):
                    if (loc < 63)
                    {
                        while (loc != 63)
                        {
                            elevator1.Location = new Point(3, loc);
                            loc++;
                            Thread.Sleep(10);
                        }
                    }
                    else
                    {
                        if (loc > 63)
                        {
                            while (loc != 63)
                            {
                                elevator1.Location = new Point(3, loc);
                                loc--;
                                Thread.Sleep(10);
                            }
                        }
                    }
                    break;

                case (button7_Click):

                    if (loc < 2)
                    {
                        while (loc != 2)
                        {
                            elevator1.Location = new Point(3, loc);
                            loc++;
                            Thread.Sleep(10);
                        }
                    }
                    else
                    {
                        if (loc > 2)
                        {
                            while (loc != 2)
                            {
                                elevator1.Location = new Point(3, loc);
                                loc--;
                                Thread.Sleep(10);
                            }
                        }
                    }
                    break;
            }
        }

        public void clickedButton(object sender, EventArgs e)
        {
            e1 = new Thread(thread);
            e1.Start();
        }

The code worked before without using a switch statement but when i try to us it it fails.

How would i solve the the problem of passing which button was pressed into the switch statement?.

Hi, welcome here.
Could you please be more specific in what exactly goes wrong?
Looking at line 19 I see that the variable sender will be not known.
And the switch part must have an integral or string type expression and sender is an object.

I dont know what to pass into the switch statement which will allow the statement to choose which bit of code to execute, which is dependant on the button presse don the GUI.

Use meaningfull names for your buttons.
Use the Tag property to store a floor number.
See here:

public Form1()
        {
            InitializeComponent();
            // init tags
            btnFloor1.Tag = 1;
            //etc.
        }

        private void button_Click(object sender, EventArgs e)
        {
            Button BT = sender as Button;
            int floor = (int)BT.Tag;
            switch (floor)
            {
                case 1:
                //etc.
                default: break;
            }
        }

Leave the thread option.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.