I’m trying to fix the problem that I cant delete webview2 html texts in any tabs but the first tab. Why?
Code that clears webview2 html text:
private async void button6_Click_1(object sender, EventArgs e)
{
{
try
{
if (tabkont.TabPages.Count > 0)
{
TabPage selectedTab = tabkont.SelectedTab;
Microsoft.Web.WebView2.WinForms.WebView2 webView2 = selectedTab.Controls.Find("webView2", true).FirstOrDefault() as Microsoft.Web.WebView2.WinForms.WebView2;
if (webView2 != null)
{
await webView2.CoreWebView2.ExecuteScriptAsync($"SetText(``);");
}
}
}
catch (Exception ex)
{
MessageBox.Show($"An error occurred: {ex.Message}");
}
}
I’m using that code to delete webview2 html text but for some reason i can only delete the first tab’s webview2 html text content. But i also want other tabs’ webview2 html text contents to be changed to blank text.
This is the code I’m using to create tabs:
private int tabCount = 1;
private async void button1_Click(object sender, EventArgs e)
{
if (tabCount == 7)
{
MessageBox.Show("Tab Limit is 7!!", "Error", MessageBoxButtons.OK);
}
else {
TabPage newTab = new TabPage($"Tab {tabCount + 1}");
var webView2 = new Microsoft.Web.WebView2.WinForms.WebView2
{
Dock = DockStyle.Fill
};
await webView2.EnsureCoreWebView2Async(null);
webView2.Source = new Uri($"file:///{Directory.GetCurrentDirectory()}/Kod/index.html");
newTab.Controls.Add(webView2);
tabkont.TabPages.Add(newTab);
tabCount++;
tabkont.SelectTab(newTab);
tabadd.Left = tabadd.Left + 55;
tabdel.Left = tabdel.Left + 55;
}
}
And this is the code that spawns the first tab webview2 html:
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 Newtonsoft.Json;
using System.IO;
using Microsoft.Web.WebView2.Core;
using Microsoft.Web.WebView2.Wpf;
using Microsoft.Web.WebView2.WinForms;
using VisualStudioTabControl;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
namespace Agazistan_HtmlTextEditor
{
public partial class agazistan_htmltexteditor : Form
{
public agazistan_htmltexteditor()
{
InitializeComponent();
InitializeAsync();
}
private async void InitializeAsync()
{
try
{
await webView2.EnsureCoreWebView2Async(null);
webView2.CoreWebView2.Navigate(new Uri($"file:///{Directory.GetCurrentDirectory()}/Kod/index.html").ToString());
}
catch (Exception ex)
{
MessageBox.Show($"Error initializing WebView2: {ex.Message}", "Initialization Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
There’s no error as a result of my code. I’m using .net 4.8 winforms and visualstudiotabcontrol.dll
Here’s a before and after of how it looks when button6_Click_1
is triggered:
- before:webview2 html text content before being deleted
- after: after presing “sil” button on first page
But that “sil” button doesn’t clear all the text inside of other pages containing webview2 html content.
The “Kod 1” Page is not dynamically created. I created it using winforms editor.
If that code worked as I wanted it to: It would delete all of my tabs’ webview2 html text content, not just the first tab’s webview2 html text content.
enflky is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Your issue is from this line in button6_Click_1
:
TabPage selectedTab = tabkont.SelectedTab;
You are only emptying out the text in the Selected tab. You should loop through all the tabs and execute your clear text code on all of them:
foreach (TabPage tab in tabkont.TabPages)
{
var webView2 = tab.Controls.Find("webView2", true).FirstOrDefault() as Microsoft.Web.WebView2.WinForms.WebView2;
if (webView2 != null)
{
await webView2.CoreWebView2.ExecuteScriptAsync($"SetText(``);");
}
}