Click here to Skip to main content
15,885,782 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I have a Richtextbox placed on a win form in c#. I have an issue with changing the font. here is some background on what is working.

I have added a toolstrip with a bunch of buttons and 2 combo boxes. the first combo box is filled with the name of all installed fonts on the computer. the second combo box is meant for the size of the font. I can add bullets, Set the font style of bold, Italics, underline. I can also set the right, left and centering of text. All of this is working fine.

The issue I am having is in my richtextbox when I try to change the font or the font size. I first select the text that I want to change the font on click the appropriate font in the combo box and the font changes. I then click on the dropdown for the size and that works as expected. Here is my issue. Now that I have changed the font and the font size. I now select a new section of text in the rich text and i click on the drop down of the font and all text now changes to whatever font I just clicked. It completely ignores what is selected. I have 1 line of code on the textchange and selected index changed for both combo boxes.

This should just change the font with the sytle that is already in richtextbox and the size of the other text portion of the combo box

C#
rtfDirections.SelectionFont = new Font(tcboFonts.Text, (float)Convert.ToDecimal(tcboSize.Text), rtfDirections.SelectionFont.Style);


What I have tried:

Not sure what I should try to get this so it works as I expect it.
Posted
Updated 22-Oct-18 13:57pm
Comments
BraveKnightFSJ 16-Oct-18 20:10pm    
Ok.
First thing i have rtfdirections.hideselection = false; this is to keep my selection from disappearing. I have that set in the constructor of the form.

As per Bill's question.
I have 2 combo box controls in a toolstrip. I have a tool strip combo box for the font name and a tool strip combo box for the sizes. I have changed it so the font changes on the SelectedIndex change event. This pulls the current style from the rich text box, and sets the font to the font from the cbofont.text and the size from cboSize.text.

I wasn't tracking any previous selection. When dealing with selected text should you assign it to a variable then make sure the selection stays to that variable.

I also noticed that sometimes when I click any toolstrip button my text selection just disappears which should not be the case. as you could see from the top i have it set not to hide selection.

No I wasn't clearing the selection. I assumed that when you select new text in the Rich text box it would just over write what was previously selected and use the new selection.

I put break points on all sources of code and added watches to the selected text and the selected text never changed unless the selection just went away and there were no breakpoints hit.

We need to know what controls apply the changes in font and size: are they independent ? Is the act of applying the changes to the selection independent of selecting font or size in the comboboxes ?

Are you keeping track of the previous selection ? How ?

When the user is done making changes to the current selection, are you clearing the selection ?

Put breakpoints in your code and examine the selection after each change: what do you see ?
 
Share this answer
 
After a lot of trouble shooting I found I had 2 issues.

First. When binding my data to the datasource I bound it to text field instead of the rtf field. changed this and fixed constantly loosing the rtf coding when control lost control of the mouse. now the rtf stay when clicking off out of the rich text box

Second. When trying to change the font in muliple different locations using 2 sets of combo boxes it was better to just use the built in font dialog box.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900