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