0

On my ListBoxItem MouseEnter event I am creating a new window with the following code.

Window w = new Window();
w.Show();

When the mouse leaves the current item I want to close the window.

How do I do that?

Many Thanks.

2 Answers 2

2

Store a reference to the window (w in your case) in a place where both the event handler for MouseEnter and MouseExit can access it and then just do a w.Close().

2
  • Hi, I have been thinking on the same line, but how do I store a reference?
    – Anand Shah
    Apr 11, 2009 at 12:09
  • @Anand: Usually you do this with a field in the class. Apr 11, 2009 at 12:21
0

Instead of storing the w in a local variable, store it in a member variable of the current class. On MouseExit, use the member variable to close the Window.

public partial class ListWindow
{
    Window displayWindow;

    public void OnListBoxItem_MouseEnter()
    {
        displayWindow = new Window();
        displayWindow.Show();
    }

    public void OnListBoxItem_MouseExit()
    {
        displayWindow.Close();
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.