Main Site Documentation

Help with TouchEventHandler


#1

Hi all.
I’m using the gadgeteer’s touch screen to get input from the user.
I created a keyboard on screen of numbers 0-9.
I did it by:

            Text zero = new Text("0");
            Text one = new Text("1");
..
..
Text[] nums = { zero, one,...}

After i Placed the numbers on the canvas i want one evnetHandler for all of them so i did:

 for (int i = 0; i < nums.Length; i++){
  nums[i].TouchDown +=new Microsoft.SPOT.Input.TouchEventHandler(Program_TouchDown);
}

and the touchdown is:

        void Program_TouchDown(object sender, Microsoft.SPOT.Input.TouchEventArgs e)
        {"here i want a switch case on the pressed number"}

My question is: How can i know what was the number that the user pressed? Is it hiding somewhere in the sender?


#2

sender is always the object that send the event. Normally it is the object you have added the event handler to.
So in your case: yes the sender is the Text object and you just need to cast it.
Below you see code how to safely cast and check if it is really the expected type:

void Program_TouchDown(object sender, Microsoft.SPOT.Input.TouchEventArgs e)
{
   var text = sender as Text;
   if (text != null)
   {
      if (text.Content == "1")
      {
         ...
      }
      else if (text.Content == "2")
      {
         ...
      }
      ...
   }
}

p.s. I don’t know how the property is named that holds the text of the Text, Content is just a guess.
p.p.s. Unfortunately you can not use a switch/case statement with strings in C#, that’s why I use if/else


#3

My problem is that the text variable doesn’t contain field “Content”.
What i use



I get: Microsoft.SPOT.Presentation.Controls.Text

so i can't compare it to "1" or so..

#4

The property containing the text is named TextContent.
You should download the Platform SDK Documentation from NETMF.codeplex.com


#5

That was the missing part of the puzzle. Thanks