How to Navigate on goButton_Click in winView2 ?

vmars316 621 Reputation points
2020-10-16T14:35:44.93+00:00

Tia ;
How to Navigate on goButton_Click in winView2 ?
Doesnt Navigate

        private void goButton_Click(object sender, EventArgs e)  
        {  
            infoBox.Text = "goButton_Click  BEFORE if";  
            if (webView != null && webView.CoreWebView2 != null)  
            {  
                webView.CoreWebView2.Navigate(addressBar.Text);  
                infoBox.Text = "goButton_Click  AFTER if";  
            }  
        }  
```Full code    
    
```powershell
using System;  
using System.Collections.Generic;  
using System.ComponentModel;  
using System.Data;  
using System.Drawing;  
using System.Linq;  
using System.Text;  
using System.Threading.Tasks;  
using System.Windows.Forms;  
using Microsoft.Web.WebView2.Core;  

  
namespace WinFormsGettngStarted  
{  
    public partial class Form1 : Form  
    {  
        public Form1()  
        {  
            InitializeComponent();  
            this.Resize += new System.EventHandler(this.Form_Resize);  
            webView = new Microsoft.Web.WebView2.WinForms.WebView2();  

  
        }  
            private void Form_Resize(object sender, EventArgs e)  
        {  
            webView.Size = this.ClientSize - new System.Drawing.Size(webView.Location);  
            webView.Height = this.ClientSize.Height - 70; // - new System.Drawing.Size(webView.Location);  
            webView.Width = this.ClientSize.Width - 20; // - new System.Drawing.Size(webView.Location);  
            sourceButton.Left = this.ClientSize.Width - sourceButton.Width - 20;  
            goButton.Left = this.ClientSize.Width - sourceButton.Width - goButton.Width - 40;  
            addressBar.Width = goButton.Left - addressBar.Left - 20;  
            infoBox.Text = webView.Width.ToString() + "    " + webView.Height.ToString();  
//            infoBox.Text = "Form_Resize";  
        }  
        private void goButton_Click(object sender, EventArgs e)  
        {  
            infoBox.Text = "goButton_Click  BEFORE if";  
            if (webView != null && webView.CoreWebView2 != null)  
            {  
                webView.CoreWebView2.Navigate(addressBar.Text);  
                infoBox.Text = "goButton_Click  AFTER if";  
            }  
        }  

  
        private void addressBar_TextChanged(object sender, EventArgs e)  
        {  

  
            infoBox.Text = "addressBar_TextChanged";  
        }  

  
        private void refreshButton_Click(object sender, EventArgs e)  
        {  

  
            infoBox.Text = "refreshButton_Click";  
        }  

  
        private void homeButton_Click(object sender, EventArgs e)  
        {  

  
            infoBox.Text = "homeButton_Click";  
        }  

  
        private void infoBox_TextChanged(object sender, EventArgs e)  
        {  

  
        }  

  
        private void backButton_Click(object sender, EventArgs e)  
        {  

  
            infoBox.Text = "backButton_Click";  
        }  

  
        private void forwardButton_Click(object sender, EventArgs e)  
        {  

  
            infoBox.Text = "forwardButton_Click";  
        }  

  
        private void sourceButton_Click(object sender, EventArgs e)  
        {  

  
            infoBox.Text = "sourceButton_Click";  
        }  
    } // class Form1  
} // namespace WinFormsGettngStarted  

Ste5 in winforms

If I remove the 'if statement' and run with just

webView.CoreWebView2.Navigate(addressBar.Text); 

I get error; System.NullReferenceException: 'Object reference not set to an instance of an object.'

I don't understand why is 'webView.CoreWebView2' ok within the 'if statement'
but not ok in the 'Navigate statement' ?

When I compile with only this much of the 'if statement'


if (webView.CoreWebView2 != null) 

Then there is no error , But Navigate does not navigate anywhere .

Thanks

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,891 questions
{count} votes

Accepted answer
  1. Daniel Zhang-MSFT 9,626 Reputation points
    2020-10-19T08:49:46.593+00:00

    Hi vmars316,
    In your code, I noticed that you use "webView = new Microsoft.Web.WebView2.WinForms.WebView2()" to create a new webView.
    In the step 3 of this document, you have created a WebView.
    So you don't need to create a new webView.
    And I delete this statement and made a test with your code, it worked fine.
    Best Regards,
    Daniel Zhang


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentationto enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.