Gilad Naaman
Gilad Naaman

Reputation: 6550

Preventing a dialog from showing multiple times

I have the following piece of code:

        else if (state.IsKeyDown(Keys.H))
        {
            Help help = new Help();
            help.ShowDialog();
        }

For some reason, if I hold the H key, the dialog opens multiple times:alt text

Upvotes: 1

Views: 450

Answers (3)

Neil Knight
Neil Knight

Reputation: 48537

If you are using XNA for your input, then save the previous KeyState and then do a check to see if the previous KeyState is released and the current KeyState is pressed.

This link will help you correctly resolve the problem.

Upvotes: 4

r2d2rigo
r2d2rigo

Reputation: 813

Create the Help dialog as a member of your class. Initialize it once, and change your code for this:

    else if (state.IsKeyDown(Keys.H))
    {
        if (!help.Visible)
             help.ShowDialog();
    }

Upvotes: 2

LaGrandMere
LaGrandMere

Reputation: 10359

Maybe you use the KeyPress Event, and you could use the KeyUp Event (or the KeyDown).

Another workaround would be to use a singleton pattern on your Help Popup.

Upvotes: 0

Related Questions