Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Use Variable after this

How is it possible to use a var after "this."

i want to set the Background color of a TextBlock to Blue.

 Brush blue = new SolidColorBrush(Colors.LightBlue); //Working Fine


//Click on the TextBlock (Working Fine) 
    private void MouseDown_TextBlock(object sender, MouseButtonEventArgs e)
    {
    
        var stand = ((TextBlock)sender).Name; //Here i am getting the Name of the TextBlock (Working Fine)
        this.stand.Background = blue; //This is not working here
    }

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

If you want to access the control you receive as Sender in an event, just cast it to the appropriate class:

private void textBlock1_MouseDown(object sender, MouseButtonEventArgs e)
{
    // Get sender as TextBlock
    TextBlock standTextBlockFromSender = (TextBlock)sender;
    standTextBlockFromSender.Background = new SolidColorBrush(Color.FromArgb(255, 0, 0, 255));
}

If you want to find the control by name, you can use the FindName method.

private void textBlock1_MouseDown(object sender, MouseButtonEventArgs e)
{
    // Get name of sender 
    string stand = ((TextBlock)sender).Name;

    // Get TextBlock by name
    TextBlock standTextBlockFromName = (TextBlock)this.FindName(stand);

    standTextBlockFromName.Background = new SolidColorBrush(Color.FromArgb(255, 0, 0, 255));
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading