This project is read-only.

Creating Dropdown Menus

Jul 5, 2012 at 2:19 AM

I've been trying to make a dropdown menu using the GuiManager, I did this with a button and a listbox. However for some reason while trying to set the listbox to be removed an error triggers on base.Update(gameTime) saying "Obtaining absolute bounds requires the control to be part of a screen". Does anyone know why this is happening or a better way to do this?

 

            ButtonControl file = new ButtonControl();
            file.Name = "File";
            file.Text = "File";
            file.Bounds = new UniRectangle(0f, 0f, 60, 20);
            
            ListControl dropdown = new ListControl();
            dropdown.SelectionMode = ListSelectionMode.Single;
            dropdown.Name = "dropdown";
            dropdown.Slider.Bounds = new UniRectangle(-9000f, -9000f, 0f, 0f); //position the scrollbar somewhere off screen

            file.Pressed += delegate(object sender, EventArgs arguments)
            {
                if (screen.Desktop.Children.Contains(dropdown))
                {
                    screen.Desktop.Children.Remove(dropdown);
                    dropdown.Items.Clear();
                }
                else
                {
                    dropdown.Items.Add("New");
                    dropdown.Items.Add("Open");
                    dropdown.Items.Add("Exit");
                    dropdown.Bounds = new UniRectangle(file.Bounds.Left, file.Bounds.Bottom, 100, dropdown.Items.Count * 21);
                    screen.Desktop.Children.Add(dropdown);
                }
            };
            screen.Desktop.Children.Add(file);

            dropdown.SelectionChanged += delegate
            {
                if (dropdown.SelectedItems.Count > 0)
                {
                    int index = dropdown.SelectedItems[0];
                    switch (dropdown.Items[index])
                    {
                        case "Exit":
                            Exit();
                            break;
                        default:
                            break;
                    }
                }
            };

            //Removes dropdown if loses focus
            screen.FocusChanged += delegate
            {
                if (screen.FocusedControl == null || screen.FocusedControl.Name == null)
                {
                    if (screen.Desktop.Children.Contains(dropdown))
                    {
                        screen.Desktop.Children.Remove(dropdown);
                        dropdown.Items.Clear();
                    }
                }
                else if (screen.Desktop.Children.Contains(dropdown) && !screen.FocusedControl.Name.Contains(dropdown.Name))
                {
                    screen.Desktop.Children.Remove(dropdown);
                    dropdown.Items.Clear();
                }
            };

            IMouse mouse = input.GetMouse();
            mouse.MouseButtonPressed += delegate
            {
                if (screen.FocusedControl == null || screen.FocusedControl.Name == null)
                {
                    if (screen.Desktop.Children.Contains(dropdown))
                    {
                        screen.Desktop.Children.Remove(dropdown);
                        dropdown.Items.Clear();
                    }
                }
                else if (screen.Desktop.Children.Contains(dropdown) && !screen.FocusedControl.Name.Contains(dropdown.Name))
                {
                    screen.Desktop.Children.Remove(dropdown);
                    dropdown.Items.Clear();
                }
            };