Hello,
i have programmed a selection list without Glide. However, I find that it is very cumbersome. Is there a way to improve or reduce the code.
Does anyone have a better idea?
I use the FEZ Spider Mainboard.
Here is the Code:
using System;
using System.Collections;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Presentation;
using Microsoft.SPOT.Presentation.Controls;
using Microsoft.SPOT.Presentation.Media;
using Microsoft.SPOT.Touch;
using Gadgeteer.Networking;
using GT = Gadgeteer;
using GTM = Gadgeteer.Modules;
using Gadgeteer.Modules.GHIElectronics;
using Microsoft.SPOT.Presentation.Shapes;
namespace GadgeteerApp3
{
public partial class Program
{
Window mainWindow;
Canvas layout;
Text chooseButton;
Text chooseOne;
Text chooseTwo;
Text chooseThree;
Text chooseFour;
bool buttonIsVisible = false;
string theChoose;
string[] selection = new string[] { "--","d0","d1","d2" };
int buttonWidth = 35;
int buttonHeight = 30;
// This method is run when the mainboard is powered up or reset.
void ProgramStarted()
{
// Use Debug.Print to show messages in Visual Studio's "Output" window during debugging.
Debug.Print("Program Started");
SetupUI();
}
void SetupUI()
{
display.SimpleGraphics.BackgroundColor = GT.Color.White;
mainWindow = display.WPFWindow;
layout = new Canvas();
chooseOne = new Text(selection[0]);
SetupButton(chooseOne, 0);
chooseOne.TouchUp += new Microsoft.SPOT.Input.TouchEventHandler(chooseOne_TouchUp);
chooseTwo = new Text(selection[1]);
SetupButton(chooseTwo, 1);
chooseTwo.TouchUp += new Microsoft.SPOT.Input.TouchEventHandler(chooseTwo_TouchUp);
chooseThree = new Text(selection[2]);
SetupButton(chooseThree, 2);
chooseThree.TouchUp += new Microsoft.SPOT.Input.TouchEventHandler(chooseThree_TouchUp);
chooseFour = new Text(selection[3]);
SetupButton(chooseFour, 3);
chooseFour.TouchUp += new Microsoft.SPOT.Input.TouchEventHandler(chooseFour_TouchUp);
chooseButton = SetupChooseButton();
mainWindow.Child = layout;
}
private void SetupButton(Text button, int position)
{
button.Height = buttonHeight;
button.Width = buttonWidth;
button.Font = Resources.GetFont(Resources.FontResources.NinaB);
button.TextAlignment = TextAlignment.Center;
button.Visibility = Visibility.Hidden;
layout.Children.Add(button);
Canvas.SetLeft(button, 70);
Canvas.SetTop(button, 50 + position * (buttonHeight + 2));
}
private Text SetupChooseButton()
{
Text choosebutton;
choosebutton = new Text("--");
choosebutton.Height = buttonHeight;
choosebutton.Width = buttonWidth;
choosebutton.ForeColor = Colors.Black;
choosebutton.Font = Resources.GetFont(Resources.FontResources.NinaB);
choosebutton.TextAlignment = TextAlignment.Center;
layout.Children.Add(choosebutton);
Canvas.SetLeft(choosebutton, 50);
Canvas.SetTop(choosebutton, 50);
choosebutton.TouchUp += new Microsoft.SPOT.Input.TouchEventHandler(choosebutton_TouchUp);
return choosebutton;
}
void choosebutton_TouchUp(object sender, Microsoft.SPOT.Input.TouchEventArgs e)
{
Debug.Print("pressed");
if (buttonIsVisible == false)
{
ShowButtons();
buttonIsVisible = true;
Debug.Print("button show");
}
else if (buttonIsVisible == true)
{
HideButtons();
buttonIsVisible = false;
Debug.Print("button hide");
}
mainWindow.Invalidate();
}
void chooseFour_TouchUp(object sender, Microsoft.SPOT.Input.TouchEventArgs e)
{
theChoose = selection[3];
chooseButton.TextContent = theChoose;
HideButtons();
buttonIsVisible = false;
mainWindow.Invalidate();
}
void chooseThree_TouchUp(object sender, Microsoft.SPOT.Input.TouchEventArgs e)
{
theChoose = selection[2];
chooseButton.TextContent = theChoose;
HideButtons();
buttonIsVisible = false;
mainWindow.Invalidate();
}
void chooseTwo_TouchUp(object sender, Microsoft.SPOT.Input.TouchEventArgs e)
{
theChoose = selection[1];
chooseButton.TextContent = theChoose;
HideButtons();
buttonIsVisible = false;
mainWindow.Invalidate();
}
void chooseOne_TouchUp(object sender, Microsoft.SPOT.Input.TouchEventArgs e)
{
theChoose = selection[0];
chooseButton.TextContent = theChoose;
HideButtons();
buttonIsVisible = false;
mainWindow.Invalidate();
}
void ShowButtons()
{
chooseOne.Visibility = Visibility.Visible;
chooseTwo.Visibility = Visibility.Visible;
chooseThree.Visibility = Visibility.Visible;
chooseFour.Visibility = Visibility.Visible;
}
void HideButtons()
{
chooseOne.Visibility = Visibility.Hidden;
chooseTwo.Visibility = Visibility.Hidden;
chooseThree.Visibility = Visibility.Hidden;
chooseFour.Visibility = Visibility.Hidden;
}
}
}