Need some expert advice/help

samsmithnz

Senior Contributor
Joined
Jul 22, 2003
Messages
1,038
Location
Boston
I'm just starting work on a new vb.net Windows forms project, that demands we have a textbox with alot of functionality in it. The main requirements are:

- Bold and italics
- Bullet points and numbered lists
- Indenting

I have a couple of ideas that I know must be possible, but I just can't find what I'm looking for. A couple of possible ideas I had were

1. To somehow use embed a word document into my application. (but I can't find any MS Word components or references?)
2. To somehow embed an HTML page with functionality very simliar to THIS VERY edit window - except that this application has to run locally on the users machines, and they won't have IIS installed...

Either way, we'll have to probably store the documents XML or HTML in a database, but that is not really an issue at the moment.

Does anyone have any pointers or ideas to help me get started? I've tryed looking at the the richtextbox, but its functionality is very limited, and I can't seem to find any 3rd party controls on google...

Your comments are most welcome.
 
samsmithnz said:
I'm just starting work on a new vb.net Windows forms project, that demands we have a textbox with alot of functionality in it. The main requirements are:

- Bold and italics
- Bullet points and numbered lists
- Indenting
...
I've tryed looking at the the richtextbox, but its functionality is very limited, and I can't seem to find any 3rd party controls on google...

Your comments are most welcome.

I'm making lot's of use of the RTB which feature is it lacking?
 
How can I get the user to change text in a textbox to have bullets, or be bold or italic then? Am i missing something?
 
samsmithnz said:
How can I get the user to change text in a textbox to have bullets, or be bold or italic then? Am i missing something?

You have to assign a new Font object to the RTB's font property:
This line of code makes the selected part of the text bold:
Code:
myRichTextBox.SelectionFont = new Font( this.SelectionFont.Name,this.SelectionFont.Size , this.SelectionFont.Style | FontStyle.Bold);
 
I understood that you could only change the selected text, and after I unselected it the font style would revert... can I have multiple selectionfonts in one richtextbox??

Do you know of any good examples...?
 
samsmithnz said:
I understood that you could only change the selected text, and after I unselected it the font style would revert... can I have multiple selectionfonts in one richtextbox??

Do you know of any good examples...?

No it creates RTF-Code. You can print the RTF property (of the richTextBox) to the console or a file and see how the RTF works. You have different tags doing all kinds of formatting and the RTB control hides all this.

The code I posted changes the selected text. But it stays and can be combined with all kinds of styles etc. You can actually add a toolbar to your form and use one RTB as a texteditor.
There's not a single font-object assigned to the RTB but you always have to create a new one when you're changing the current selection. I guess (but I'm not sure) the RTB kind of uses the assigned font-object to copy it's properties.
Note: The current selection doesn't have to be a selected text portion but can be the last blank after your text. The assigned fontstyle etc. will be applied to the following text (once you insert it ;-)).

If you actually implement a simple text editor you'll have to update the toolbar when clicking on the RTB or selecting some text of it.
You'll have to chack all the textproperties (that you're toolbar is handling) of the current selection and set the toolbar's buttons accordingly.

In my current app I use these properties:
Bold;Italic;Underline/AlignLeft;AlignCenter;AlignRight/FontColor/FontFamily/FontSize

So far I didn't need the numberedList and I don't know how to add this feature. I don't even know if it's part of the RTF-specification.
 
Excellent, that is great news. I just saved a word document as a rich text file and it seemed to keep bullets, so that must all be possible.

Do you happen to have a example/ sample with basic functionality already implemented or know where I can download one?
 
samsmithnz said:
Excellent, that is great news. I just saved a word document as a rich text file and it seemed to keep bullets, so that must all be possible.

Do you happen to have a example/ sample with basic functionality already implemented or know where I can download one?

I don't have a seperate editor application. Mine is integrated into a larger application. I didn't find a sample project when I was doing it but you can find enough infos on this board.
To set the font properties I'm using the line posted above. You can replace Bold with Italic and Underline and assign them to the corresponding buttons.

Also take a look at the RichTextBox helpfile:

Code:
richTextBox1.SelectionFont = New Font("Verdana", 12, FontStyle.Bold)
richTextBox1.SelectionColor = Color.Red

richTextBox1.SelectionFont = new Font("Verdana", 12, FontStyle.Bold);
richTextBox1.SelectionColor = Color.Red;

Just remember that FontStyle can have multiple values (e.g. text can be bold and underlined at the same time) so you have to use OR and NAND to set or reset FontStyles to existing selections.

To set Alignement:
Code:
richTextBox1.SelectionAlignment = HorizontalAlignment.Left;
richTextBox1.SelectionAlignment = HorizontalAlignment.Center;
richTextBox1.SelectionAlignment = HorizontalAlignment.Right;

My GUI catches an event sent by the RTB whenever it's clicked or some text selected.
In the event-handler the buttons are set (e.g. If the selected text is bold, the corresponding button is checked):
Code:
buttonBold.Checked = this.richTextBox.SelectionFont.Bold;
buttonItalic.Checked = this.richTextBox.SelectionFont.Italic;
buttonUnderline.Checked = this.richTextBox.SelectionFont.Underline;
buttonAlignL.Checked = ( this.richTextBox.SelectionAlignment == HorizontalAlignment.Left);
buttonAlignC.Checked = ( this.richTextBox.SelectionAlignment == HorizontalAlignment.Center);
buttonAlignR.Checked = ( this.richTextBox.SelectionAlignment == HorizontalAlignment.Right);

You'll probably find better code though ;)
 
Last edited:
Problem with that though, if you select text where bold/italic/underline is switched on in one part and off in another, or the font changes, or pretty much anything changes, richTextBox1.SelectionFont == null and that whole function breaks down.
Code:
if(rtf1.SelectionFont==null){
	string r=rtf1.SelectedRtf;
	toolBold.Pushed=Regex.IsMatch(r, @"(?<=(?:\\\\)*)\\b[\\\s]");
	toolBold.PartialPush=Regex.IsMatch(r, @"(?<=(?:\\\\)*)\\b0[\\\s]");
	toolItalic.Pushed=Regex.IsMatch(r, @"(?<=(?:\\\\)*)\\i[\\\s]");
	toolItalic.PartialPush=Regex.IsMatch(r, @"(?<=(?:\\\\)*)\\i0[\\\s]");
	toolUnderline.Pushed=Regex.IsMatch(r, @"(?<=(?:\\\\)*)\\ul[\\\s]");
	toolUnderline.PartialPush=Regex.IsMatch(r, @"(?<=(?:\\\\)*)\\ulnone[\\\s]");
}else{
	toolBold.Pushed=rtf1.SelectionFont.Bold;
	toolBold.PartialPush=false;
	toolItalic.Pushed=rtf1.SelectionFont.Italic;
	toolItalic.PartialPush=false;
	toolUnderline.Pushed=rtf1.SelectionFont.Underline;
	toolUnderline.PartialPush=false;
}
toolLeft.Pushed=rtf1.SelectionAlignment==HorizontalAlignment.Left;
toolCentre.Pushed=rtf1.SelectionAlignment==HorizontalAlignment.Center;
toolRight.Pushed=rtf1.SelectionAlignment==HorizontalAlignment.Right;
this works, for the most part. In this case, if the first half of the text is bold and the second is plain, the bold button is partially pushed. If the first half is plain and the second is bold, then, well, it just says bold. It will also agree that it's bold if that's the only thing selected, or plain if that is the case. The same applies for italic and underline.
Problem with that is if you want to set the style, you need to locate the proper RTF tags in the selection and add/remove where appropriate. Also, if the selected text contains lines with more than one alignment, it will always return left align. You'd be better off making your own.
 
Last edited:
Back
Top