0

I am programmatically creating menu items and adding event handlers to them, and for some reason I can't seem to get each menu item's event to register its own unique information. For example:

            for (int i = 0; i < this.Slideshow.Speeds.GetLength(0); i++) 
            {

                MenuItem m = new MenuItem() { Header = this.Slideshow.Speeds[i, 0] };

                //Set initial checked MenuItem
                if (i == 3)
                {
                    m.IsChecked = true;
                }


                m.Click += (s, e) => {

                    foreach (MenuItem m_ in this.slideshowSpeedMenuItem.Items)
                    {
                        m_.IsChecked = false;
                    }

                    m.IsChecked = true;

                    this.Slideshow.CurrentSpeed = (int)this.Slideshow.Speeds[i, 1];

                    //i is always the same. i SHOULD be getting a different entry in the array
                    MessageBox.Show(i.ToString());

                };

                this.slideshowSpeedMenuItem.Items.Add(m);

            }

Why is i always the same for each menu item's click event? The idea is to have each menu item do some stuff with a different given speed, as indicated by i.

0 Answers0