bigelowr
bigelowr

Reputation: 431

OpenModalWindow throws error first time the screen calls it

I have been building an IT Ticketing / inventory system, and the program is done, now I'm adding instructions for the end users and the other techs in our school. Since I know nobody will read an instruction manual, I opted to make instructions for each screen, and put them in Modal Windows, activated by pushing a Help button I added to the Screen Commands section.

That works wonderfully, so I decided to capture the KeyDown event, and launch the window if they press F1. This is where things get a little weird. If the HelpWindow for this particular screen has been opened at least once, pressing F1 opens it again with no trouble. If it has never been opened, pressing F1 results in an Error 'Control 'HelpWindow' doesn't contain a modal window. OpenModalWindow/CloseModalWindow cannot be used.' After closing this error message, F1 will launch the HelpWindow exactly as expected. Very bizarre...

Background information: Visual Studio 2012 Lightswitch project in VB (I work in both VB and C#, flipped a coin for this project) The Modal Window is a group on the screen that is not visible, named "HelpWindow"; I use OpenModalWindow("HelpWindow") to open it. The exact same line of code in the HelpButton_Execute code, and the event handler for the KeyDown event. It's the same method I use for every other modal window in the program, for submitting new tickets, adding equipment to the inventory, etc. This problem only happens in the event handler, and only the first time the F1 key is pressed. The behavior is the same on every screen that has a help window.

My attempts to Google the problem were fruitless. Has anybody ever seen this behavior before?

Upvotes: 0

Views: 234

Answers (1)

Yann Duran
Yann Duran

Reputation: 3879

That does sound very strange. I have to admit that I haven't seen anything like this myself with a modal window.

You don't mention where you're trapping the KeyDown key, so it's a bit hard to comment on that.

What I have seen sometimes, especially when doing something a little "different", is the error message not telling you the actual cause of the problem.

I would try wrapping the code with a dispatcher call, to make sure the call is being performed on the correct thread, as well as a try/catch to see if you can find the real cause of the error:

Private Sub YourClickHandler
    Try
        Me.Details.Dispatcher.BeginInvoke(
            Sub()
                OpenModalWindow("HelpWindow")
            End Sub)

    Catch ex As Exception
        Me.ShowMessageBox(ex.Message)
    End Try
End Sub

I hope that helps, or at least points you in the direction of a solution.

Upvotes: 1

Related Questions