Example #1
0
        //Every time the selection changes in the CotrolOptions listbox the 3x3
        //array of buttons in ButtonGrid is repolpulated.
        private void SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ButtonGrid.Children.Clear();

            if (ControlOptions.SelectedItem != null)
            {
                ButtonArray = new Button[9];
                TextBox       myTextbox            = null;
                ControlOption myOption             = (ControlOption)ControlOptions.SelectedItem;
                int           visibleButtonCounter = 0; //Used to iterate through label array from ControlOption

                for (int i = 0; i < 9; i++)
                {
                    ButtonArray[i] = new Button();

                    if (myOption.buttonVisible[i])
                    {
                        ButtonArray[i].Style = (Style)Application.Current.Resources["Pusher"];
                        ButtonArray[i].Name  = "Button" + i.ToString();

                        ButtonArray[i].Margin = new Thickness(10, 10, 10, 10);

                        StackPanel ButtonContent = new StackPanel();
                        ButtonContent.HorizontalAlignment = HorizontalAlignment.Center;
                        ButtonContent.Orientation         = Orientation.Vertical;
                        if (myOption.buttonUris[visibleButtonCounter] != null)
                        {
                            Image ContentImage = new Image();
                            ContentImage.Source = new BitmapImage(myOption.buttonUris[visibleButtonCounter]);
                            ContentImage.HorizontalAlignment = HorizontalAlignment.Center;
                            ContentImage.MaxWidth            = 50;
                            ButtonContent.Children.Add(ContentImage);
                        }
                        TextBlock ContentText = new TextBlock();
                        ContentText.Text = myOption.buttonLabels[visibleButtonCounter];
                        ContentText.HorizontalAlignment = HorizontalAlignment.Center;
                        ButtonContent.Children.Add(ContentText);

                        ButtonArray[i].Content = ButtonContent;
                        Grid.SetColumn(ButtonArray[i], i % 3 + 1);
                        Grid.SetRow(ButtonArray[i], i / 3 + 1);
                        ButtonGrid.Children.Add(ButtonArray[i]);



                        ButtonArray[i].PreviewMouseDown += delegate(object a, MouseButtonEventArgs b)
                        {
                            buttonPressed = true;
                            CheckSound();
                            ((UCSettings)DataContext).ButtonKey = ((Button)a).Name;
                        };

                        ButtonArray[i].PreviewMouseUp += delegate(object a, MouseButtonEventArgs b)
                        {
                            buttonPressed = false;
                            ((UCSettings)DataContext).ButtonKey = "ButtonNull";
                        };


                        ButtonArray[i].MouseEnter += delegate(object a, MouseEventArgs b)
                        {
                            if (((UCSettings)DataContext).IsHover)
                            {
                                CheckSound();
                                buttonPressed = true;
                                CheckCenterMouse();
                                ((UCSettings)DataContext).ButtonKey = ((Button)a).Name;
                            }
                        };

                        ButtonArray[i].MouseLeave += delegate(object a, MouseEventArgs b)
                        {
                            if (((UCSettings)DataContext).IsHover)
                            {
                                buttonPressed = false;
                                ((UCSettings)DataContext).ButtonKey = "ButtonNull";
                            }
                        };

                        visibleButtonCounter++;
                    }
                }

                if (myOption.textBoxVisible)
                {
                    myTextbox = new TextBox();
                    myTextbox.TextWrapping = TextWrapping.Wrap;
                    myTextbox.Name         = "TextInput";
                    myTextbox.FontSize     = 36;
                    Grid.SetColumn(myTextbox, 1);
                    Grid.SetRow(myTextbox, 1);
                    Grid.SetColumnSpan(myTextbox, 3);
                    ButtonGrid.Children.Add(myTextbox);
                    foreach (Control curControl in ButtonGrid.Children)
                    {
                        if (curControl.GetType() == HelpButton.GetType()) //Note the bug: If no image added to Button label
                        {
                            if (((TextBlock)((StackPanel)((Button)curControl).Content).Children[1]).Text.Equals("Clear"))
                            {
                                ((Button)curControl).Click += delegate(object a, RoutedEventArgs i)
                                {
                                    myTextbox.Text = null;
                                };
                                ((Button)curControl).MouseEnter += delegate(object a, MouseEventArgs b)
                                {
                                    if (((UCSettings)DataContext).IsHover)
                                    {
                                        CheckSound();
                                        ((Button)a).RaiseEvent(new RoutedEventArgs(ButtonBase.ClickEvent));
                                    }
                                };
                            }
                        }
                    }
                    foreach (Control curControl in ButtonGrid.Children)
                    {
                        if (curControl.GetType() == HelpButton.GetType()) //Note the bug: If no image added to Button label
                        {
                            if (((TextBlock)((StackPanel)((Button)curControl).Content).Children[1]).Text.Equals("Send"))
                            {
                                ((Button)curControl).Click += delegate(object a, RoutedEventArgs i)
                                {
                                    NotifyServer(localIP + "Alexa" + "/" + 4,
                                                 myTextbox.Text,
                                                 "POST",
                                                 "text/plain");
                                };
                                ((Button)curControl).MouseEnter += delegate(object a, MouseEventArgs b)
                                {
                                    if (((UCSettings)DataContext).IsHover)
                                    {
                                        CheckSound();
                                        ((Button)a).RaiseEvent(new RoutedEventArgs(ButtonBase.ClickEvent));
                                    }
                                };
                            }
                        }
                    }
                }
                HeaderPic.Source = new BitmapImage(myOption.actualUri);
                ((UCSettings)DataContext).Message = myOption.name;
            }
            CheckCenterMouse();
        }