question

MarkMather-7608 avatar image
0 Votes"
MarkMather-7608 asked ·

How do I code Enter Key down?

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) ;
                     {
                   
             }
    
    
    
    
         }


windows-uwp
· 1
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I tried to figure out what is your problem... but from your description I can't. If I create a TextBox with an KeyDown event handler like the one you posted, using the debugger I can see that if I press enter while the focus is in TextBox line 4 is hit. Can you describe better what happens to you?

0 Votes 0 ·
danielescipioni avatar image
0 Votes"
danielescipioni answered ·

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 "docs.microsoft.com".

·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

MarkMather-7608 avatar image
0 Votes"
MarkMather-7608 answered ·

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();
    
             }
         }
    
         }
     }
· 1 ·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

This should be a reply to my comment, not an answer. Anyway, check my answer below.

0 Votes 0 ·
MarkMather-7608 avatar image
0 Votes"
MarkMather-7608 answered ·

One more question?

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



tab.png (10.2 KiB)
·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.