Skip to main content

Microsoft Silverlight

Answered Question KeyDown event throws an exception when MessageBox.Show(e.Key.ToString()) is calledRSS Feed

(0)

Grey Matter
Grey Matter

Member

Member

39 points

108 Posts

KeyDown event throws an exception when MessageBox.Show(e.Key.ToString()) is called

The TextBox's event:

private void TextBox_KeyDown(object sender, KeyEventArgs e)
{
MessageBox.Show(e.Key.ToString());
}
throws an exception on any key pressed just except the first one. 
It doesn't happen at all on the KeyUp event with the same code within.
What does it mean? 

varshavmane
varshavmane

Contributor

Contributor

6723 points

1,580 Posts

Re: KeyDown event throws an exception when MessageBox.Show(e.Key.ToString()) is called

Check this link:

http://blogs.vertigo.com/personal/keithc/Blog/archive/2008/12/02/keydown-and-messagebox-dont-mix-well-in-silverlight.aspx

Its will answer your post. Hope it helps you Smile

Please "Mark as Answer" if this post answered your question. :)
Visit my Blog: http://varshavmane.blogspot.com/

Grey Matter
Grey Matter

Member

Member

39 points

108 Posts

Re: KeyDown event throws an exception when MessageBox.Show(e.Key.ToString()) is called

 Thank for your explanation, but I still have some doubts:

If any other thread (event) tries to display MessageBox at the same time why can't I catch it at my breakpoint?

It's caught only once and sometimes drops on it. By the way, my Firefox drops too.

Then I added a TextBlock to check a chance of multiple KeyDown events and was putting every got key to the TextBlock's content.

But no multiple events occurred.

Jonathan Shen – MSFT
Jonathan...

All-Star

All-Star

24939 points

2,425 Posts

Microsoft
Answered Question

Re: KeyDown event throws an exception when MessageBox.Show(e.Key.ToString()) is called

Hi Grey Matter,

This is a known issue and has been fixed in Silverlight 3.   Currently, the workaround is to do it like this.

        private void myTextBox_KeyDown(object sender, KeyEventArgs e)
        {
            Dispatcher.BeginInvoke(()=>{
                MessageBox.Show(e.Key.ToString());
            });
        }

Best regards,

Jonathan 

 

Jonathan Shen
Microsoft Online Community Support
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities