How to modify WebView2 content in all tabs in a Winforms TabControl?

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.

New contributor

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(``);");
    }
}

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật