HtmlEditor :  Phorum 5 The fastest message board... ever.

This is the discussion forum for the HtmlEditor. See also the HtmlEditor home page, where you can download the control, and the Documentation Wiki, a collaborative project for documenting the control.

Goto Thread: PreviousNext
Goto: Forum ListMessage ListNew TopicSearchLog In
Applying Styles to a Block of Text
Posted by: Shari (---.co.marin.ca.us)
Date: Wednesday, 09-Apr-2003, 23:06:44

Hi.

I'm really liking this Control. I have a question that's probably simple.

Is there a way to apply a specific style to a block of text?

TIA

Re: Applying Styles to a Block of Text
Posted by: Tim Anderson (---.server.ntl.com)
Date: Friday, 11-Apr-2003, 07:09:40

Shari,

You can do this through the DOM:

paraElement.setAttribute("className","mystyle", 0);

where paraElement is an element of type mshtml.IHTMLElement.

The main thing to watch is to apply the the attribute to the correct element. Typically this would be a block element like <p>, <div> or <span>.

Tim

Re: Applying Styles to a Block of Text
Posted by: Shari (---.co.marin.ca.us)
Date: Friday, 11-Apr-2003, 19:17:07

I was envisioning applying the style to a FormatBlock, i.e., a user would select a block of text and apply a style to it. The solution you're suggesting is more toward a single element, correct?

Re: Applying Styles to a Block of Text
Posted by: Tim Anderson (---.netcomuk.co.uk)
Date: Saturday, 12-Apr-2003, 12:05:05

You can do this by surrounding the selected block with a <span> and applying the style to that.

Tim

Re: Applying Styles to a Block of Text
Posted by: Tim Anderson (---.server.ntl.com)
Date: Monday, 14-Apr-2003, 10:36:50

...of course I'm assuming you mean a CSS style.

If you mean something like simple bold, italic etc, you can use execCommand as in the demo app.

Tim

Re: Applying Styles to a Block of Text
Posted by: Shari (---.co.marin.ca.us)
Date: Wednesday, 07-May-2003, 22:53:51

Well, using the span tags with a text range seems to work okay, except for one thing.

When I try to change a style from another one, I can't get the old span tags to go away. When I call rng.pasteHTML, it doesn't replace the entire text contained in rng.htmlText (where rng is a textrange).

Re: Applying Styles to a Block of Text
Posted by: (62.48.170.---)
Date: Friday, 09-May-2003, 16:20:15

I'm having the same problem... Canīt undo the current style.

Re: Applying Styles to a Block of Text
Posted by: Tim Anderson (---.server.ntl.com)
Date: Saturday, 10-May-2003, 06:36:08

I do this by calling setAttribute on the existing SPAN element; or you remove it completely with removeNode.

Tim

Re: Applying Styles to a Block of Text
Posted by: gokul (61.11.45.---)
Date: Tuesday, 05-Aug-2003, 18:08:15

You can try using the removeAttribute("",0) to the span method....so that the span tag would be there but no changes would be visible.....

Re: Applying Styles to a Block of Text
Posted by: John Cousins (---.uk.mediasurface.com)
Date: Tuesday, 30-Aug-2005, 14:34:15

Could you provide a small code sample that illustrates this 'surrounding'? I'm new to all this mshtml stuff smiling smiley

tiaTim wrote:

> You can do this by surrounding the selected block with a <span>
> and applying the style to that.
>
> Tim

Re: Applying Styles to a Block of Text
Posted by: Dominic (---.range81-153.btcentralplus.com)
Date: Friday, 21-Oct-2005, 14:44:27

try using

textrange.execCommand('delete');
textrange.pasteHTML(newcontent);

this should remove all of the span tag and its contents, and then you just paste in the new content you want to go in there.
you might need to save the textrange contents and perform some processing with regular expressions etc if you dont want to lose all the content inside.

Cheers,

Dom

Re: Applying Styles to a Block of Text
Posted by: Alec_M (---.copeohs.com)
Date: Thursday, 10-May-2007, 14:11:16

Hi,
I'm trying to get to grips with the HtmlEdit control for use on an internal business project.

I'd like to define a set stylesheet for the content. I've found the htmleditor.setstylesheet method, but this doesn't seem to inject the required reference to the html? (Perhaps as I'm referencing a url rather than a local file system file?)

I've also tried this:

Dim sSheets As onlyconnect.IHTMLStyleSheetsCollection
sSheets = Me.heEmail.HtmlDocument2.GetStyleSheets()
If sSheets.length = 0 Then
Me.heEmail.HtmlDocument2.CreateStyleSheet(My.Resources.WebPath + My.Resources.HTMLMsgStyleSheet, 0)
End If

Where the webpath and HTMLMsgStyleSheet combine to form a full url (the html being created will eventually be sent as an HTML email to customers, so this path is publicly accessible).

But this techique fails with an "object reference not set" type of error - but I'm not sure why the reference isn't created, as I'm trying the code immediately after having loaded an existing html source string to the editor via LoadDocument.

Sorry if this is a bit long-winded.
Appreciate any help offered!
Al

Re: Applying Styles to a Block of Text
Posted by: Alec_M (---.copeohs.com)
Date: Thursday, 10-May-2007, 18:16:18

Replying to my own post:
I didn't use the available properties in the end, but a simpler approach of string detection and replacement. This suits my purpose as I'm always going to use the same style sheet reference (the user won't be getting an option to choose).
My method:

Dim source As String = Me.heEmail.GetDocumentSource
'Construct string to look for/insert.
Dim sSheet As String = "<link href=""" & My.Resources.WebPath + My.Resources.HTMLMsgStyleSheet & _
""" rel=""stylesheet"" type=""text/css"" media=""screen"" />"
'Check it doesn't already have our stylesheet
If InStr(source, sSheet) = 0 Then
'Match not found, so we add it.
If source.Contains("</head>") Then
source = source.Replace("</HEAD>", Chr(13) + sSheet + "</head>")
End If
End If

If there's a better method, I'd be glad to improve it!



Your Name: 
Your Email: 
Subject: 
Spam prevention:
Please, enter the code that you see below in the input field. This is for blocking bots that try to post this form automatically.
Pcwyj
This is a moderated forum. Your message will remain hidden until it has been approved by a moderator or administrator
This forum powered by Phorum.