MOdifying RTF in RichTextBox COntrol

G

Guest

Hi!

I am building a text input interface using Windows.Forms. RichTextBox
control. Now, since the units of different parameters are not pixels as far
as my application is concerned, I would like to put additional \userprop tags.

For example, when the character spacing changes, I would like to add the
markup

{\*\userprops {\propname mycharspacing}\proptype5
{\staticval 0.5s}} to the RTF document.

1. In effect, I need to identify the cursor position.
2. Insert additional RTF commands
3. Ensure that newly typed text goes in between the RTF commands.

How do I do this?

Thanks
Venkat
 
G

Guest

I'm not real sure what you're going for and I'm not very familiar with RTF,
but I see that no one has responded to your question, so I thought I'd have a
go at it.

I assumed that what you meant was that if the user is entering text and then
hits a button to set some attribute and then goes back to enter more text,
you want the selection to be the same as before the button was pressed
(inside the inserted formatting).

So what I did was to create a class (a struct probably would have worked) to
hold the start position and length of the selection :


internal sealed class
TextSelection
{
private int start ;
private int length ;

internal
TextSelection
(
int Start
,
int Length
)
{
this.start = Start ;
this.length = Length ;
}

internal int
Start
{
get
{
return ( this.start ) ;
}
}

internal int
Length
{
get
{
return ( this.length ) ;
}
}
}

Then I added an event handler for when the text box gets focus:

private void rtText_Enter(object sender, System.EventArgs e)
{
if ( this.rtText.Tag != null )
{
this.rtText.Select ( ((TextSelection) this.rtText.Tag).Start
, ((TextSelection) this.rtText.Tag).Length ) ;
}
}

And then created the click handler for the formatting button (my button just
inserts the <em></em> HTML tags):

private void bTag_Click(object sender, System.EventArgs e)
{
if ( this.rtText.SelectedText != "" )
{
this.rtText.Tag = new TextSelection (
this.rtText.SelectionStart+4 , this.rtText.SelectionLength ) ;
this.rtText.SelectedText = "<em>" + this.rtText.SelectedText
+ "</em>" ;
}
else
{
this.rtText.Tag = new TextSelection (
this.rtText.SelectionStart+4 , this.rtText.SelectionLength ) ;
this.rtText.SelectedText += "<em></em>" ;
}
}

Hope this helps or gives you inspiration, for me it's killed a half hour or
so.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top