Click here to Skip to main content
15,881,687 members
Articles / Desktop Programming / WPF

WPF HTML Supported TextBlock

Rate me:
Please Sign up or sign in to vote.
4.36/5 (9 votes)
7 Feb 2009LGPL32 min read 173.3K   1.1K   35  
This article describes HtmlTextBlock which is a WPF TextBlock that can parse a limited set of HTML tags and display them.
using System;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
using System.Collections.Generic;

namespace QuickZip.MiniHtml2
{

    public class CurrentStateType
    {
        private List<HtmlTag> activeStyle = new List<HtmlTag>();
        private bool bold;
        private bool italic;
        private bool underline;
        private string hyperlink;

        public bool Bold { get { return bold; } }
        public bool Italic { get { return italic; } }
        public bool Underline { get { return underline; } }
        public string HyperLink { get { return hyperlink; } }

        public void UpdateStyle(HtmlTag aTag)
        {
            if (!aTag.IsEndTag)
                activeStyle.Add(aTag);
            else
                for (int i = activeStyle.Count - 1; i >= 0; i--)
                    if ('/' + activeStyle[i].Name == aTag.Name)
                    {
                        activeStyle.RemoveAt(i);
                        break;
                    }
            updateStyle();
        }


        private void updateStyle()
        {
            bold = false;
            italic = false;
            underline = false;
            hyperlink = "";

            foreach (HtmlTag aTag in activeStyle)
                switch (aTag.Name)
                {
                    case "b": bold = true; break;
                    case "i": italic = true; break;
                    case "u": underline = true; break;
                    case "a": if (aTag.Contains("href")) hyperlink = aTag["href"]; break;
                }
        }

        public CurrentStateType()
        {

        }



    }
	
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU Lesser General Public License (LGPLv3)


Written By
Founder
Hong Kong Hong Kong

Comments and Discussions