how to change fontfamily without changing fontsize and fontstyle

toostage

Member
Joined
Apr 26, 2006
Messages
12
Programming Experience
1-3
i have a combobox with the fontfamilies in it. When ya select a piece a text and then select a fontfamily from the combobox it will change de fontfamily. But to change a fontfamily (as far as i know) ya have to call a new font for the selected area, and with that a fontfamily, a fontsize and a fontstyle is required:
VB.NET:
           Input.SelectionFont = New Font( _
               cmbType.Text, _
               currentFont.Size, _
               currentFont.Style)
But now i've got the problem when there are several fontsize's or fontstyles in the selected text. How can i fix this? If there is the option to only change the fontfamily please tell me :) but the i've tried several things to do this but fontfamilies is a read only thingy...
 
SelectionFont property returns Nothing when there are multiple fonts in selection. If this is the case you have to process the characters in selection one-by-one.
 
hmm okay, thought allready about that, but then how can i process the characters one by one, when i use getchar or strip i think the fontsettings are lost?
 
toostage said:
when i use getchar or strip i think the fontsettings are lost?
I don't have a clue what you mean by this.

You get the RTB.SelectionFont property, if it is Nothing you have to get to it. First you get information about your working set, that is the RTB.SelectionStart and RTB.SelectionLength properties. Then you start working it with the RTB.Select method, from RTB.SelectionStart to RTB.SelectionLength you RTB.Select one-by-one character and get RTB.SelectionFont for each one, which you modify and apply again.
 
thnx JohnH this is the solution:

VB.NET:
[SIZE=2][COLOR=#0000ff][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] selectStart = Input.SelectionStart
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] selectLength = Input.SelectionLength
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] counter = 0
[/SIZE][SIZE=2][COLOR=#0000ff]For[/COLOR][/SIZE][SIZE=2] counter = 0 [/SIZE][SIZE=2][COLOR=#0000ff]To[/COLOR][/SIZE][SIZE=2] selectLength - 1
[/SIZE][SIZE=2][COLOR=#0000ff]Try
[/COLOR][/SIZE][SIZE=2]Input.SelectionStart = selectStart + counter
Input.SelectionLength = 1
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] currentFont [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Drawing.Font = Input.SelectionFont
Input.SelectionFont = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] Font( _
cmbType.Text, _
currentFont.Size, _
currentFont.Style)
[/SIZE][SIZE=2][COLOR=#0000ff]Catch[/COLOR][/SIZE][SIZE=2] ex [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Exception
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Try
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Next
[/COLOR][/SIZE][SIZE=2]Input.SelectionStart = selectStart
Input.SelectionLength = selectLength
[/SIZE][/COLOR][/SIZE]

 
Last edited:
Back
Top