-1

Using a webbrowser control. I'd like to count the number of characters into webbrowser, just like textchange of Textbox class. I just want to count the number of characters in the text that display WebBrowser no html, no images, etc.Any idea about how simulate the behavior of textbox which trigger when change text displayed? Thanks

I'm developing Winforms in C#. No ASP.NET.

  • 1
    A WebBrowser control displays a lot more than text. For example, it includes images. Do you just want to count the number of characters in the text that it displays? Or do you want to count the number of characters in its source HTML? You also allude to the TextChanged event for a TextBox, which gets triggered when the text displayed in the textbox changes...the equivalent event for a WebBrowser would be something like Navigated. Your question needs a lot more details. – Cody Gray - on strike Dec 02 '16 at 21:52
  • Are you talking about counting the characters of the html document? – Alexander Ryan Baggett Dec 13 '16 at 02:23
  • Please clarify your intent so we can answer your question. – Alexander Ryan Baggett Dec 13 '16 at 02:30
  • Good morning, Yes I only want to count characters of html document. Eg: `

    Hello


    World

    ` `count: 10 characters`
    – Christian Torres M Dec 14 '16 at 13:38

1 Answers1

1

Add the following class:

using System.Text.RegularExpressions;

namespace CK.TicketSystem.Shared { public static class HtmlUtils { public static bool IsHtmlFragment(string value) { return Regex.IsMatch(value, @""); }

    /// <summary>
    /// Remove tags from a html string
    /// </summary>
    /// <param name="value"></param>
    /// <returns></returns>
    public static string RemoveTags(string value)
    {
        if (value != null)
        {
            value = CleanHtmlComments(value);
            value = CleanHtmlBehaviour(value);
            value = Regex.Replace(value, @"</[^>]+?>", " ");
            value = Regex.Replace(value, @"<[^>]+?>", "");
            value = value.Trim();
        }
        return value;
    }

    /// <summary>
    /// Clean script and styles html tags and content
    /// </summary>
    /// <returns></returns>
    public static string CleanHtmlBehaviour(string value)
    {
        value = Regex.Replace(value, "(<style.+?</style>)|(<script.+?</script>)", "", RegexOptions.IgnoreCase | RegexOptions.Singleline);

        return value;
    }

    /// <summary>
    /// Replace the html commens (also html ifs of msword).
    /// </summary>
    public static string CleanHtmlComments(string value)
    {
        //Remove disallowed html tags.
        value = Regex.Replace(value, "<!--.+?-->", "", RegexOptions.IgnoreCase | RegexOptions.Singleline);

        return value;
    }

    /// <summary>
    /// Adds rel=nofollow to html anchors
    /// </summary>
    public static string HtmlLinkAddNoFollow(string value)
    {
        return Regex.Replace(value, "<a[^>]+href=\"?'?(?!#[\\w-]+)([^'\">]+)\"?'?[^>]*>(.*?)</a>", "<a href=\"$1\" rel=\"nofollow\" target=\"_blank\">$2</a>", RegexOptions.IgnoreCase | RegexOptions.Compiled);
    }
}

}

I must say that I found this class in some really good developer's blog, but unfortunately I can not remember where I did find it.

Then you do:

var str = HtmlUtils.RemoveTags(yourHtmlString);
var numberOfCharacters = str.Length;

Hope it helps

taquion
  • 2,667
  • 2
  • 18
  • 29