How do I code Enter Key down?

Mark Mather 156 Reputation points
2020-05-02T03:28:55.49+00:00

Im creating a web browser UWP, I have the text box but the key down code I'm using seems not to work. I'm providing the code. enter code here

   private void TextBox_KeyDown(object sender, Windows.UI.Xaml.Input.KeyRoutedEventArgs e)
        {
            if (e.Key == Windows.System.VirtualKey.Enter) ;
                    {

            }




        }
Universal Windows Platform (UWP)
{count} votes

Accepted answer
  1. Daniele 1,996 Reputation points
    2020-05-03T16:53:49.907+00:00

    From my understanding you need something like

       private void TextBox_KeyDown(object sender, KeyRoutedEventArgs e)  
       {  
       	if (e.Key != Windows.System.VirtualKey.Enter) return;  
       	var textBox = (TextBox) sender;  
       	string text = textBox.Text;  
       	if (!text.StartsWith("http://")) text = $"http://{text}";  
       	if (!Uri.TryCreate(text, UriKind.Absolute, out Uri uri)) return;  
       	Neutron.Navigate(uri);  
       	e.Handled = true;  
       }  
    

    Of course you have to check the text that an user can input, I did a just a simple check at line 6. The code above loads correctly if you input for example "learn.microsoft.com".

    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Mark Mather 156 Reputation points
    2020-05-03T16:04:09.45+00:00

    Sorry for the delay, I'm trying to get the information entered the text box to make the webview to navigate to the site, the webview is named Neutron

    using System;
    using Windows.UI.Xaml;
    using Windows.UI.Xaml.Controls;
    
    // The Blank Page item template is documented at https://go.microsoft.com/fwlink/?LinkId=234238
    
    namespace Frost
    {
        /// <summary>
        /// An empty page that can be used on its own or navigated to within a Frame.
        /// </summary>
        public sealed partial class Browser2 : Page
        {
            public Browser2()
            {
                this.InitializeComponent();
                Uri w = new Uri("http://MSN.com");
                Neutron.Navigate(w);
            }
    
    
            private void TextBox_KeyDown(object sender, Windows.UI.Xaml.Input.KeyRoutedEventArgs e)
            {
                if (e.Key == Windows.System.VirtualKey.Enter) ;
                        {
    
                }
    
    
    
    
            }
    
            private void Neutron_ContentLoading(WebView sender, WebViewContentLoadingEventArgs args)
            {
                // Show status.
                if (args.Uri != null)
                {
                    StatusTextBox.Text = "Loading content for " + args.Uri.ToString();
    
                }
            }
    
            private void Neutron_DOMContentLoaded(WebView sender, WebViewDOMContentLoadedEventArgs args)
            {
                // Show status.
                if (args.Uri != null)
                {
                    StatusTextBox.Text = "Content for " + args.Uri.ToString() + " has finished loading";
    
                }
            }
    
            private void Neutron_NavigationCompleted(WebView sender, WebViewNavigationCompletedEventArgs args)
            {
                if (args.IsSuccess == true)
                {
                    StatusTextBox.Text = " completed successfully.";
                }
                else
                {
                    StatusTextBox.Text = "Navigation to: " + args.Uri.ToString() +
                                           " failed with error " + args.WebErrorStatus.ToString();
    
                }
            }
    
            }
        }
    

  2. Mark Mather 156 Reputation points
    2020-05-03T17:19:29.48+00:00

    One more question?

    Does this work for changing the tab title and icon?
    7905-tab.png

    0 comments No comments