Код, очищающий html-текст webview2:
Код: Выделить всё
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}");
}
}
Это код, который я использую для создания вкладок:
Код: Выделить всё
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;
}
}
Код: Выделить всё
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);
}
}
Вот как это выглядит до и после срабатывания button6_Click_1:
- до: текстовое содержимое webview2 html перед удалением
- после: после первого нажатия кнопки «sil» страница
Страница «Код 1» не создается динамически. Я создал его с помощью редактора winforms.
Если бы этот код работал так, как я хотел: он удалил бы все текстовое содержимое webview2 html моих вкладок, а не только текстовое содержимое html webview2 первой вкладки. .
Подробнее здесь: https://stackoverflow.com/questions/792 ... tabcontrol