CSS Styling Example for WinForms

Nevron NOV RichTextEditor for WinForms ships with hundreds of examples with full source code, as part of the Nevron Open Vision Suite. All WinForms RichTextEditor examples are also available as a stand-alone Visual Studio solution in C# and VB.NET, which you can download from the links below.

CSS Styling Example for WinForms Description

The example demonstrates how to apply css like styles to the text document.


Css Styling Example

C# CSS Styling Example Code for Nevron RichTextEditor for WinForms
Language: C#

using Nevron.Nov;
using Nevron.Nov.Dom;
using Nevron.Nov.Graphics;
using Nevron.Nov.Layout;
using Nevron.Nov.Text;
using Nevron.Nov.UI;
using Nevron.Nov.Windows.Forms;
using System.Windows.Forms;

namespace NovWinFormsProject
{
	public partial class Form1 : Form
	{
		#region Constructors

		public Form1()
		{
			InitializeComponent();

			// Set form title
			Text = "NOV WinForms - NCssStylingExample";

			// Create the NOV example and its controls
			NWidget exampleContent = CreateExampleContent();
			NWidget exampleControls = CreateExampleControls();
			if (exampleControls != null)
			{
				exampleControls.PreferredWidth = 300;
			}

			// Place the example and its controls in a pair box
			NPairBox pairBox = new NPairBox(exampleContent, exampleControls);
			pairBox.FillMode = ENStackFillMode.First;
			pairBox.FitMode = ENStackFitMode.First;
			pairBox.Margins = new NMargins(NDesign.HorizontalSpacing, NDesign.VerticalSpacing);

			// Host the example in the form
			NNovWidgetHost host = new NNovWidgetHost(pairBox);
			host.Dock = DockStyle.Fill;
			Controls.Add(host);
		}

		#endregion

		#region Example

		public NWidget CreateExampleContent()
		{
			// Create the rich text
			m_RichTextWithRibbon = new NRichTextViewWithRibbon();
			m_RichTextWithRibbon.View.Content.Sections.Clear();

			// set the content to web and allow only text copy paste
			m_RichTextWithRibbon.View.Content.Layout = ENTextLayout.Web;

			// add some content
			NSection section = new NSection();

			for (int i = 0; i < 2; i++)
			{
				string text = "Paragraph block contained in a section block. The paragraph will appear with a red background.";
				section.Blocks.Add(new NParagraph(text));
			}

			NGroupBlock groupBlock = new NGroupBlock();
			section.Blocks.Add(groupBlock);

			for (int i = 0; i < 2; i++)
			{
				string text = "Paragraph block contained in a group block. This paragraph will appear with a green background, italic font style, and altered font size and indentation.";
				groupBlock.Blocks.Add(new NParagraph(text));
			}

			NTable table = new NTable(2, 2);
			table.Border = NBorder.CreateFilledBorder(NColor.Black);
			table.BorderThickness = new NMargins(2, 2, 2, 2);
			table.AllowSpacingBetweenCells = false;
			section.Blocks.Add(table);

			for (int rowIndex = 0; rowIndex < table.Rows.Count; rowIndex++)
			{ 
				for (int colIndex = 0; colIndex < table.Columns.Count; colIndex++)
				{
					string text = "Paragraph block contained in a table cell. This paragraph with appear in blue.";
					table.Rows[rowIndex].Cells[colIndex].Blocks.Add(new NParagraph(text));
				}
            }

			m_RichTextWithRibbon.View.Content.Sections.Add(section);

			// create a CSS style sheet that applies different background and font depending on the parent of the paragraph
			NStyleSheet sheet = new NStyleSheet();
			m_RichTextWithRibbon.View.Document.StyleSheets.Add(sheet);

			// for all paragraphs that are directly contained in a section
			{
				NRule rule1 = new NRule();
				rule1.Declarations.Add(new NValueDeclaration("BackgroundFill", new NColorFill(NColor.LightCoral)));
				sheet.Add(rule1);

				NSelector selector1 = new NSelector();
				rule1.Selectors.Add(selector1);

				// select all paragraph
				selector1.Conditions.Add(new NTypeCondition(NParagraph.NParagraphSchema));

				// that are children of range collection
				NChildCombinator childOfBlockCollection = new NChildCombinator();
				selector1.Combinators.Add(childOfBlockCollection);

				// that are direct descendants of section
				NChildCombinator childOfSection = new NChildCombinator();
				childOfSection.Conditions.Add(new NTypeCondition(NSection.NSectionSchema));
				selector1.Combinators.Add(childOfSection);
			}

			// then add a rule for blocks contained inside a group block
			{
				{
					NRule rule2 = new NRule();
					rule2.Declarations.Add(new NValueDeclaration("BackgroundFill", new NColorFill(NColor.LightGreen)));
					rule2.Declarations.Add(new NValueDeclaration("FontSize", 12.0d));
					rule2.Declarations.Add(new NValueDeclaration("FirstLineIndent", 10.0d));
					rule2.Declarations.Add(new NValueDeclaration("FontStyleItalic", true));
					sheet.Add(rule2);

					NSelectorBuilder builder = new NSelectorBuilder(rule2);

					builder.Start();

					builder.Type(NParagraph.NParagraphSchema);
					builder.ChildOf();
					builder.ChildOf();
					builder.Type(NGroupBlock.NGroupBlockSchema);
					builder.ChildOf();
					builder.ChildOf();
					builder.Type(NSection.NSectionSchema);

					builder.End();
				}


				{
					NRule rule3 = new NRule();
					rule3.Declarations.Add(new NValueDeclaration("BackgroundFill", new NColorFill(NColor.LightBlue)));
					sheet.Add(rule3);


					NSelectorBuilder builder = new NSelectorBuilder(rule3);

					builder.Start();

					builder.Type(NParagraph.NParagraphSchema);
					builder.ChildOf();
					builder.ChildOf();
					builder.Type(NGroupBlock.NGroupBlockSchema);
					builder.ChildOf();
					builder.ChildOf();
					builder.Type(NTableRow.NTableRowSchema);

					builder.End();
				}
			}


			return m_RichTextWithRibbon;
		}
		public NWidget CreateExampleControls()
		{
			NStackPanel stack = new NStackPanel();


			return stack;
		}
		public string GetExampleDescription()
		{
			return "

The example demonstrates how to apply css like styles to the text document.

"; } /// /// Gets the relevant documentation links for the example /// /// public (string Name, string Url)[] GetDocumentationUrls() { return new[] { ("Styling and Inheritance", "https://www.nevron.com/docs/openvision/dom-styling-and-inheritance.html"), ("Rich Text View Overview", "https://www.nevron.com/docs/openvision/rich-text-styles-overview.html"), }; } #endregion #region Implementation #endregion #region Event Handlers #endregion #region Fields private NRichTextViewWithRibbon m_RichTextWithRibbon; #endregion #region Static Methods #endregion #region Constants #endregion #region Nested Types #endregion } }

Download a fully functional C# (.cs) CSS Styling Example for WinForms Solution

VB.NET CSS Styling Example Code for Nevron RichTextEditor for WinForms
Language: VB.NET

Imports Nevron.Nov
Imports Nevron.Nov.Dom
Imports Nevron.Nov.Graphics
Imports Nevron.Nov.Layout
Imports Nevron.Nov.Text
Imports Nevron.Nov.UI
Imports Nevron.Nov.Windows.Forms

Public Class Form1

#Region "Constructors"

    Public Sub New()
        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.

        ' Set form title
        Text = "NOV WinForms - NCssStylingExample"

        ' Create the NOV example and its controls
        Dim exampleContent As NWidget = CreateExampleContent()
        Dim exampleControls As NWidget = CreateExampleControls()
        If Not exampleControls Is Nothing Then
            exampleControls.PreferredWidth = 300
        End If

        ' Place the example and its controls in a pair box
        Dim pairBox As NPairBox = New NPairBox(exampleContent, exampleControls)
        pairBox.FillMode = ENStackFillMode.First
        pairBox.FitMode = ENStackFitMode.First
        pairBox.Margins = New NMargins(NDesign.HorizontalSpacing, NDesign.VerticalSpacing)

        ' Host the example in the form
        Dim host As NNovWidgetHost(Of NWidget) = New NNovWidgetHost(Of NWidget)(pairBox)
        host.Dock = DockStyle.Fill
        Controls.Add(host)
    End Sub


#End Region

#Region "Example"

        Public Function CreateExampleContent() As NWidget
            ' Create the rich text
            m_RichTextWithRibbon = New NRichTextViewWithRibbon()
            m_RichTextWithRibbon.View.Content.Sections.Clear()

            ' set the content to web and allow only text copy paste
            m_RichTextWithRibbon.View.Content.Layout = ENTextLayout.Web

            ' add some content
            Dim section As NSection = New NSection()

            For i = 0 To 1
                Dim text = "Paragraph block contained in a section block. The paragraph will appear with a red background."
                section.Blocks.Add(New NParagraph(text))
            Next

            Dim groupBlock As NGroupBlock = New NGroupBlock()
            section.Blocks.Add(groupBlock)

            For i = 0 To 1
                Dim text = "Paragraph block contained in a group block. This paragraph will appear with a green background, italic font style, and altered font size and indentation."
                groupBlock.Blocks.Add(New NParagraph(text))
            Next

            Dim table As NTable = New NTable(2, 2)
            table.Border = NBorder.CreateFilledBorder(NColor.Black)
            table.BorderThickness = New NMargins(2, 2, 2, 2)
            table.AllowSpacingBetweenCells = False
            section.Blocks.Add(table)

            For rowIndex = 0 To table.Rows.Count - 1
                For colIndex = 0 To table.Columns.Count - 1
                    Dim text = "Paragraph block contained in a table cell. This paragraph with appear in blue."
                    table.Rows(rowIndex).Cells(colIndex).Blocks.Add(New NParagraph(text))
                Next
            Next

            m_RichTextWithRibbon.View.Content.Sections.Add(section)

            ' create a CSS style sheet that applies different background and font depending on the parent of the paragraph
            Dim sheet As NStyleSheet = New NStyleSheet()
            m_RichTextWithRibbon.View.Document.StyleSheets.Add(sheet)

            ' for all paragraphs that are directly contained in a section
            If True Then
                Dim rule1 As NRule = New NRule()
                rule1.Declarations.Add(New NValueDeclaration(Of NFill)("BackgroundFill", New NColorFill(NColor.LightCoral)))
                sheet.Add(rule1)

                Dim selector1 As NSelector = New NSelector()
                rule1.Selectors.Add(selector1)

                ' select all paragraph
                selector1.Conditions.Add(New NTypeCondition(NParagraph.NParagraphSchema))

                ' that are children of range collection
                Dim childOfBlockCollection As NChildCombinator = New NChildCombinator()
                selector1.Combinators.Add(childOfBlockCollection)

                ' that are direct descendants of section
                Dim childOfSection As NChildCombinator = New NChildCombinator()
                childOfSection.Conditions.Add(New NTypeCondition(NSection.NSectionSchema))
                selector1.Combinators.Add(childOfSection)
            End If

            ' then add a rule for blocks contained inside a group block
            If True Then
                If True Then
                    Dim rule2 As NRule = New NRule()
                    rule2.Declarations.Add(New NValueDeclaration(Of NFill)("BackgroundFill", New NColorFill(NColor.LightGreen)))
                    rule2.Declarations.Add(New NValueDeclaration(Of Double)("FontSize", 12.0R))
                    rule2.Declarations.Add(New NValueDeclaration(Of Double)("FirstLineIndent", 10.0R))
                    rule2.Declarations.Add(New NValueDeclaration(Of Boolean)("FontStyleItalic", True))
                    sheet.Add(rule2)

                    Dim builder As NSelectorBuilder = New NSelectorBuilder(rule2)

                    builder.Start()

                    builder.Type(NParagraph.NParagraphSchema)
                    builder.ChildOf()
                    builder.ChildOf()
                    builder.Type(NGroupBlock.NGroupBlockSchema)
                    builder.ChildOf()
                    builder.ChildOf()
                    builder.Type(NSection.NSectionSchema)

                    builder.End()
                End If


                If True Then
                    Dim rule3 As NRule = New NRule()
                    rule3.Declarations.Add(New NValueDeclaration(Of NFill)("BackgroundFill", New NColorFill(NColor.LightBlue)))
                    sheet.Add(rule3)


                    Dim builder As NSelectorBuilder = New NSelectorBuilder(rule3)

                    builder.Start()

                    builder.Type(NParagraph.NParagraphSchema)
                    builder.ChildOf()
                    builder.ChildOf()
                    builder.Type(NGroupBlock.NGroupBlockSchema)
                    builder.ChildOf()
                    builder.ChildOf()
                    builder.Type(NTableRow.NTableRowSchema)

                    builder.End()
                End If
            End If


            Return m_RichTextWithRibbon
        End Function
        Public Function CreateExampleControls() As NWidget
            Dim stack As NStackPanel = New NStackPanel()


            Return stack
        End Function
        Public Function GetExampleDescription() As String
            Return "

The example demonstrates how to apply css like styles to the text document.

" End Function ''' ''' Gets the relevant documentation links for the example ''' ''' Public Function GetDocumentationUrls() As (String, String)() Return {("Styling and Inheritance", "https://www.nevron.com/docs/openvision/dom-styling-and-inheritance.html"), ("Rich Text View Overview", "https://www.nevron.com/docs/openvision/rich-text-styles-overview.html")} End Function #End Region #Region "Implementation" #End Region #Region "Event Handlers" #End Region #Region "Fields" Private m_RichTextWithRibbon As NRichTextViewWithRibbon #End Region #Region "Static Methods" #End Region #Region "Constants" #End Region #Region "Nested Types" #End Region End Class

Download a fully functional VB.NET (.vb) CSS Styling Example for WinForms Solution