---
title: "Check if a target was not hit in WPF"  
description: "Check if a target was not hit in WPF"  
author: "Anonymous User"  
published: 2014-11-14  
updated: 2014-11-15  
canonical: https://www.mindstick.com/forum/12586/check-if-a-target-was-not-hit-in-wpf  
category: "wpf"  
tags: ["c#"]  
reading_time: 2 minutes  

---

# Check if a target was not hit in WPF

I'm using the [code](https://yourviews.mindstick.com/view/85458/alan-turing-the-mastermind-behind-cracking-the-enigma-code-during-world-war-ii) below to see when the mouse [right](https://www.mindstick.com/articles/12766/check-whether-you-are-doing-digital-transformation-right-or-not) [button](https://www.mindstick.com/articles/63/how-to-add-button-in-datagridview-in-csharp-dot-net) is clicked, if it hits a [target](https://yourviews.mindstick.com/view/81207/small-industries-should-be-the-target-of-economic-relief-package) (Drawing) or not.

[Now](https://yourviews.mindstick.com/view/81402/it-s-liberals-vs-progressives-in-us-politics-now) [If the mouse](https://answers.mindstick.com/qa/108159/what-to-do-if-the-mouse-is-not-working) hits the target a [message](https://www.mindstick.com/forum/12802/show-confirmation-message-yes-or-no-in-asp-dot-net) will be shown stating that we hit the target.

But where can I show a message that the target was **NOT** hit? VisualTreeHelper.HitTest() doesn't seem to return a [value](https://www.mindstick.com/articles/23219/an-optimized-description-adds-value-to-experience-and-in-turn-effectively-guest-posting-packages) indicating that the target was hit or not.

```
private void OnMouseRightButtonUp(object sender, MouseButtonEventArgs e){    var x = MousePos.RightDown.X;    var y = MousePos.RightDown.Y;    var hitRect = new Rect(x - 2, y - 2, 4, 4);    var geom = new RectangleGeometry(hitRect);    VisualTreeHelper.HitTest(Drawing,                         
null,                             
MyCallback,                             
new GeometryHitTestParameters(geom));}private HitTestResultBehavior MyCallback(HitTestResult result){    MessageBox.Show("You hit the target");    return HitTestResultBehavior.Stop;}
```

## Replies

### Reply by Anonymous User

Have some class level flag to indicate whether hit is successful or not. Set the flag to true from MyCallback and show message based on that flag.

```
bool isTargetHit;    private void OnMouseRightButtonUp(object sender, MouseButtonEventArgs e){    isTargetHit = false;    .......    VisualTreeHelper.HitTest(Drawing,                              null,                              MyCallback,                              new GeometryHitTestParameters(geom));     if(isTargetHit)    {        MessageBox.Show("You hit the target");    }    else    {        MessageBox.Show("You did not hit the target");    } }private HitTestResultBehavior MyCallback(HitTestResult result){    isTargetHit = true;    return HitTestResultBehavior.Stop;}
```


---

Original Source: https://www.mindstick.com/forum/12586/check-if-a-target-was-not-hit-in-wpf

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
