---
title: "Using event in WPF"  
description: "Using event in WPF"  
author: "Anonymous User"  
published: 2013-06-11  
updated: 2013-06-12  
canonical: https://www.mindstick.com/forum/1039/using-event-in-wpf  
category: "vb.net"  
tags: ["vb.net"]  
reading_time: 1 minute  

---

# Using event in WPF

Hi [Expert](https://www.mindstick.com/articles/13120/an-expert-financial-advice-will-improve-your-finances),\
I have created two textboxes in WPF [named](https://answers.mindstick.com/qa/44918/what-is-a-no-day-yet-named-motion) is txt1 and txt2\
I have written following line of code on txt1 **lostFocus** [event](https://www.mindstick.com/articles/167719/marquee-hire-benefits-of-event-lighting-hire-london)\
**If txt1.Text is nothing then** **txt1.Focus****End If**\
on txt2 **lostFocus** event\
**If txt2.Text is nothing then** **txt2.Focus****End If**\
Now, If **txt1** and **txt2** are both empty and user presses TAB key in **txt1** the [problem](https://yourviews.mindstick.com/view/81399/tackling-the-problem-of-unemployment-during-corona-pandemic) occurs. [Program](https://www.mindstick.com/blog/12337/scaling-up-your-mentorship-program) goes in [infinite](https://www.mindstick.com/forum/159435/react-app-crash-infinite-loop-identify-and-break) loop. I mean [cursor](https://www.mindstick.com/articles/13003/create-a-simple-cursor-in-sql-server) comes to **txt1** and goes to **txt2** infinite times.I know This is normal [behavior](https://www.mindstick.com/forum/159354/runtimeexception-and-exception-behavior) according to my code.\
So I want to have validating event to [avoid](https://yourviews.mindstick.com/story/1518/tips-to-avoid-dengue-at-home) the problems like above. But I cannot find one in WPF. So which event should I use?\
Thanks in [advance](https://www.mindstick.com/blog/33258/jee-mains-and-jee-advance-exams) for any recommendations or solutions.

## Replies

### Reply by AVADHESH PATEL

Hi Brad,\
You can try as following\
Add event handler for event **PreviewLostKeyboardFocus**. inside the event handler set **e.Handled** to true if the text is empty. Sample C# code.Below I have writer a generic handler.\
**private void TextBox_PreviewLostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)****{** **if (string.IsNullOrEmpty((sender as TextBox).Text))** **{** **e.Handled = true;** **}****}****\**I hope it helpful for you.


---

Original Source: https://www.mindstick.com/forum/1039/using-event-in-wpf

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
