Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# Windows Phone
Hi there,
 
I have in my Windows Phone application something like that:
 
string mhm= "Test";
TextBlock K = new TextBlock();
K.Name = "ID";
K.FontSize = 22;
K.Text = mhm;
K.Tap += MyClick;
 
and
 
private void MyClick(object sender, GestureEventArgs e)
{
   // How to get string from element mhm ("Test") from sender?
}
Posted 8-Nov-12 13:07pm
jakusza326
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

You have to cast sender to its real class or one of the interfaces it implements to use methods and properties.
 
Here is one way to do it:
private void MyClick(object sender, GestureEventArgs e)
{
    TextBlock txt = sender as TextBlock;
    if( txt == null )
    {
        throw new ArgumentException("MyClick needs first parameter to be a TextBlock");
    }
 
    string whatEver = txt.Text;
}
  Permalink  
v2
Comments
jakusza at 9-Nov-12 8:57am
   
Great job. But why does it work?
lukeer at 12-Nov-12 4:01am
   
Because sender indeed is a TextBlock. It's passed to the event handler routine as object because of a convention that most of the .NET framework adheres to: "First parameter is object sender, so you'll recognize an event handler on first sight." (kind of)
 
See also: MSDN on the "as" key word[^]
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

You need to cast the sender with your original control
TextBlock txtBlock = sender as TextBlock;
 
Thanks,
Rachit
http://agarwalrachit.blogspot.in/[^]
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

Please try this code.
 
private void MyClick(object sender, GestureEventArgs e)
{
   TextBlock txtBlk = new TextBlock();
   txtBlk (TextBlock )sender;
   String txtBlkText = txtBlk.Text;
   
}
 

Thanks,
Bilaal
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 431
1 Afzaal Ahmad Zeeshan 184
2 /\jmot 176
3 Sergey Alexandrovich Kryukov 175
4 Marcin Kozub 175
0 OriginalGriff 8,344
1 Sergey Alexandrovich Kryukov 7,407
2 DamithSL 5,634
3 Maciej Los 5,024
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web04 | 2.8.1411023.1 | Last Updated 19 Sep 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100