Main Site Documentation

TextBox value control led.SetRedIntensity


#1

is this possible I have a value entered in to a TextBox that I want to set the the value for led.SetRedIntensity(some value)

this is what I have
Thanks


using System;
using System.Text;
using Microsoft.SPOT;
using Microsoft.SPOT.Presentation;
using Microsoft.SPOT.Presentation.Controls;
using Microsoft.SPOT.Presentation.Media;

using GT = Gadgeteer;
using GTM = Gadgeteer.Modules;
using Gadgeteer.Modules.GHIElectronics;
using GHIElectronics.NETMF.Glide;
using GHIElectronics.NETMF.Glide.Display;
using GHIElectronics.NETMF.Glide.UI;

namespace RGBadjustTextBox

{  
    public partial class Program
    {        
        static GHIElectronics.NETMF.Glide.Display.Window window;
        void ProgramStarted()
        {
            // Do one-time tasks here
            Debug.Print("Program Started");
            GlideTouch.Initialize();
            // Load the Window XML string.
            window = GlideLoader.LoadWindow(Resources.GetString(Resources.StringResources.Window));
            // Resize any loaded Window to the LCD's size.
            Glide.FitToScreen = true;
            // Assign the Window to MainWindow; rendering it to the LCD.
            Glide.MainWindow = window;
            // Get the Buttons
            GHIElectronics.NETMF.Glide.UI.Button LoadColor = (GHIElectronics.NETMF.Glide.UI.Button)window.GetChildByName("LoadColor");
            // Get the TextBox
            GHIElectronics.NETMF.Glide.UI.TextBox redNum = (GHIElectronics.NETMF.Glide.UI.TextBox)window.GetChildByName("redNum");
            GHIElectronics.NETMF.Glide.UI.TextBox greenNum = (GHIElectronics.NETMF.Glide.UI.TextBox)window.GetChildByName("greenNum");
            GHIElectronics.NETMF.Glide.UI.TextBox blueNum = (GHIElectronics.NETMF.Glide.UI.TextBox)window.GetChildByName("blueNum");
            
            // Add a tap event handler to open the keyboard.            
            redNum.TapEvent += new OnTap(Glide.OpenKeyboard);
            greenNum.TapEvent += new OnTap(Glide.OpenKeyboard);
            blueNum.TapEvent += new OnTap(Glide.OpenKeyboard);
          
            // Add a value changed handler.
            redNum.ValueChangedEvent += new OnValueChanged(redNum_ValueChangedEvent);
            greenNum.ValueChangedEvent += new OnValueChanged(greenNum_ValueChangedEvent);
            blueNum.ValueChangedEvent += new OnValueChanged(blueNum_ValueChangedEvent);

            // Set up event handlers
            LoadColor.TapEvent += new OnTap(LoadColor_TapEvent);          
        }

        void blueNum_ValueChangedEvent(object sender)
        {
            TextBox blueNum = (TextBox)window.GetChildByName("blueNum");
            Debug.Print(blueNum.Text);          
        }

        void greenNum_ValueChangedEvent(object sender)
        {
            TextBox greenNum = (TextBox)window.GetChildByName("greenNum");
            Debug.Print(greenNum.Text);
        }

        void redNum_ValueChangedEvent(object sender)
        {
            TextBox redNum = (TextBox)window.GetChildByName("redNum");
            Debug.Print(redNum.Text);          
        }

        void LoadColor_TapEvent(object sender)
        {          
            //string strB = ("blueNum");
            byte b = Convert.ToByte("blueNum");
            //string strG = ("greenNum");
            byte g = Convert.ToByte("greenNum");
           // string strR = ("redNum");
            byte r = Convert.ToByte("redNum");
            led.SetRedIntensity(r);
            led.SetBlueIntensity(g);
            led.SetBlueIntensity(b);
        }
    }
}


#2

So, what happens when you run the code? Are you getting an error message? If so, can you post the text of the error?


#3

Everything loads I can enter values in to my Text Boxes but when I press the button I get this on my PC screen

An unhandled exception of type ‘System.Exception’ occurred in mscorlib.dll


#4

You just need to change the convert code as below

byte b = Convert.ToByte(_blueNum.Text);
byte g = Convert.ToByte(_greenNum.Text);
byte r = Convert.ToByte(_redNum.Text);

where _blueNum is the control reference you got via _window.GetChildByName()


using System;
using System.Text;
using Microsoft.SPOT;
using Microsoft.SPOT.Presentation;
using Microsoft.SPOT.Presentation.Controls;
using Microsoft.SPOT.Presentation.Media;
 
using GT = Gadgeteer;
using GTM = Gadgeteer.Modules;
using Gadgeteer.Modules.GHIElectronics;
using GHIElectronics.NETMF.Glide;
using GHIElectronics.NETMF.Glide.Display;
using GHIElectronics.NETMF.Glide.UI;
 
namespace RGBadjustTextBox
{
    public partial class Program
    {
        static GHIElectronics.NETMF.Glide.Display.Window _window;
 
        private GHIElectronics.NETMF.Glide.UI.TextBox _redNum;
        private GHIElectronics.NETMF.Glide.UI.TextBox _greenNum;
        private GHIElectronics.NETMF.Glide.UI.TextBox _blueNum;
        
        void ProgramStarted()
        {
            // Do one-time tasks here
            Debug.Print("Program Started");
            GlideTouch.Initialize();
            // Load the Window XML string.
            _window = GlideLoader.LoadWindow(Resources.GetString(Resources.StringResources.Window));
            // Resize any loaded Window to the LCD's size.
            Glide.FitToScreen = true;
            // Assign the Window to MainWindow; rendering it to the LCD.
            Glide.MainWindow = _window;
            // Get the Buttons
            var loadColor = (GHIElectronics.NETMF.Glide.UI.Button)_window.GetChildByName("LoadColor");
            // Get the TextBox
            _redNum = (GHIElectronics.NETMF.Glide.UI.TextBox)_window.GetChildByName("redNum");
            _greenNum = (GHIElectronics.NETMF.Glide.UI.TextBox)_window.GetChildByName("greenNum");
            _blueNum = (GHIElectronics.NETMF.Glide.UI.TextBox)_window.GetChildByName("blueNum");
 
            // Add a tap event handler to open the keyboard.            
            _redNum.TapEvent += new OnTap(Glide.OpenKeyboard);
            _greenNum.TapEvent += new OnTap(Glide.OpenKeyboard);
            _blueNum.TapEvent += new OnTap(Glide.OpenKeyboard);
 
            // Add a value changed handler.
            _redNum.ValueChangedEvent += new OnValueChanged(RedNumValueChangedEvent);
            _greenNum.ValueChangedEvent += new OnValueChanged(GreenNumValueChangedEvent);
            _blueNum.ValueChangedEvent += new OnValueChanged(BlueNumValueChangedEvent);
 
            // Set up event handlers
            loadColor.TapEvent += new OnTap(LoadColorTapEvent);
 
            led.SwapGreenBlueValues();
        }
 
        void BlueNumValueChangedEvent(object sender)
        {
            Debug.Print(_blueNum.Text);
        }
 
        void GreenNumValueChangedEvent(object sender)
        {
            Debug.Print(_greenNum.Text);
        }
 
        void RedNumValueChangedEvent(object sender)
        {
            Debug.Print(_redNum.Text);
        }
 
        void LoadColorTapEvent(object sender)
        {
            byte b = Convert.ToByte(_blueNum.Text);
            byte g = Convert.ToByte(_greenNum.Text);
            byte r = Convert.ToByte(_redNum.Text);
 
            led.SetRedIntensity(r);
            led.SetGreenIntensity(g);
            led.SetBlueIntensity(b);
        }
    }
}


#5

Cool Thanks I got it to work and I learned a lot I will post a video in a bit


#6

What I have so far I started out trying to control the RGB vales with a slider but with values of 1-255 I thought direct value input might be better. I would like to on the Text Box touch event have the default keyboard open with then number pad but I am still working that out.

should I put this in the Code section how do you credit some one if I do that?

thanks to Perfectphase for showing me the way :slight_smile:


#7

Looks good!

FYI - for the video tags, you only put “ecxVY_eV-8o” between the tags to embed the video.