Click here to Skip to main content
11,706,477 members (45,747 online)
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 12: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 146
1 Sergey Alexandrovich Kryukov 140
2 chainerlt 110
3 Mika Wendelius 80
4 ppolymorphe 71
0 OriginalGriff 9,190
1 Sergey Alexandrovich Kryukov 8,407
2 CPallini 5,189
3 Maciej Los 4,756
4 Mika Wendelius 3,686


Advertise | Privacy | Mobile
Web04 | 2.8.150819.1 | Last Updated 19 Sep 2013
Copyright © CodeProject, 1999-2015
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