代码之家  ›  专栏  ›  技术社区  ›  enflky

如何修改Winforms TabControl中所有选项卡中的WebView2内容?

  •  0
  • enflky  · 技术社区  · 6 月前

    你好,伙计们,我试图解决在另一个选项卡中无法删除webview2 html文本的问题,但我可以在第一个选项卡中删除,为什么?

    清除webview2 html文本代码:

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

    我使用该代码删除webview2-html文本,但不知何故,我只能删除第一个选项卡webview2-htmltext,但我还想选择另一个选项卡webview2-html文本更改为空白文本。

    我还使用该代码创建选项卡

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

    以及生成第一个选项卡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);
                }
            }
    

    我的代码中没有错误,我使用的是.net 4.8 winforms和visualstudiotabcontrol.dll

    何时我将在第一个选项卡webview2-html文本上使用清除按钮

    之前: not deleted first tab webview2 html text before

    之后: after presing "sil" button on first page

    但另一个页面上的“sil”按钮并不能清除webview2-html中的所有文本

    “Kod 1”页面在应用程序打开后不会打开,它已经在winforms编辑器上了

    如果该代码正常工作,我还可以删除所有选项卡webview2-html文本,而不是第一个选项卡webview2html文本。

    1 回复  |  直到 6 月前
        1
  •  0
  •   Sal    6 月前

    你的问题来自这条线 button6_Click_1 :

    TabPage selectedTab = tabkont.SelectedTab;
    

    您只需清空“选定”选项卡中的文本。您应该遍历所有选项卡,并在所有选项卡上执行明文代码:

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