locked
KeyDown with enter button error

    Question

  • Hi,

    I have the following code: 

            private async void SavePasswordBtn_Click(object sender, RoutedEventArgs e)
            {
                try
                {
                    await data.InitAsync(PasswordEntry.Text, PasswordEntry.Text, false);
                }
                catch(System.Xml.XmlException x)
                {
                    Debug.WriteLine("Something is wrong" + x);
                }
            }
    
            private void PasswordEntry_KeyDown(object sender, KeyRoutedEventArgs e)
            {
                if (e.Key == Windows.System.VirtualKey.Enter)
                {
                    this.SavePasswordBtn_Click(sender, null);
                }
            }

    Whenever I press enter in the password textbox, I get an error for some reason. The actions are assigned to the correct XAML controls and I'm not sure what the problem is. 

    It looks like the error relates to e and the message is "The object is already initialized!". Any ideas why I'm getting this? 

    Thanks for your help!


    • Edited by JB-CI-IRS Monday, July 21, 2014 3:29 PM
    Monday, July 21, 2014 3:28 PM

Answers

  • I don't think that the exception is coming from this code, but probably from

     await data.InitAsync(PasswordEntry.Text, PasswordEntry.Text, false);

    This code works fine for me:

            private void PasswordEntry_KeyDown(object sender, KeyRoutedEventArgs e)
            {
                if (e.Key == Windows.System.VirtualKey.Enter)
                {
                    Debug.WriteLine("Password enter received, and...");
                    this.SavePasswordBtn_Click(sender, null);
                }
                else
                {
                    Debug.WriteLine("Password character received!");
                }
    
            }
    
            private  void SavePasswordBtn_Click(object sender, RoutedEventArgs e)
            {
                try
                {
                    Debug.WriteLine("Button Clicked!");
    
                }
                catch (System.Xml.XmlException x)
                {
                    Debug.WriteLine("Something is wrong" + x);
                }
            }


    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Tuesday, July 22, 2014 1:22 PM
    Moderator

All replies

  • What happens if you pass in the e parameter from the calling method rather than null?

    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Monday, July 21, 2014 7:28 PM
    Moderator
  • I get another error stating that the object is already initialized. The call stack shows: 

    > App2.exe!App2.App.InitializeComponent.AnonymousMethod__1(object sender, Windows.UI.Xaml.UnhandledExceptionEventArgs e) Line 50 C#

    Any thoughts?

    Tuesday, July 22, 2014 12:13 PM
  • I don't think that the exception is coming from this code, but probably from

     await data.InitAsync(PasswordEntry.Text, PasswordEntry.Text, false);

    This code works fine for me:

            private void PasswordEntry_KeyDown(object sender, KeyRoutedEventArgs e)
            {
                if (e.Key == Windows.System.VirtualKey.Enter)
                {
                    Debug.WriteLine("Password enter received, and...");
                    this.SavePasswordBtn_Click(sender, null);
                }
                else
                {
                    Debug.WriteLine("Password character received!");
                }
    
            }
    
            private  void SavePasswordBtn_Click(object sender, RoutedEventArgs e)
            {
                try
                {
                    Debug.WriteLine("Button Clicked!");
    
                }
                catch (System.Xml.XmlException x)
                {
                    Debug.WriteLine("Something is wrong" + x);
                }
            }


    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Tuesday, July 22, 2014 1:22 PM
    Moderator