Cursor position while changing font...

fourinjuly4ij

New member
Joined
Mar 3, 2005
Messages
3
Programming Experience
3-5
Hi,

So I'm writting a small little word processor program just to get back into programming again. Right now, I have a toolbar with buttons, a RichTextBox for writing in, and two combobox's filled with the installed fonts and font sizes.

My actual question is, at this point when I change the font with the combobox it checks to see if any text in the RichTextBox is highlighted. If it is highlighted it changes the highlighted text, which is fine. My problem is when nothing is highlighted, it changes everything in the textbox to that font. I know why it does this, but I don't know how to just set the font past where the cursor is in the RichTextBox.

VB.NET:
Private Sub cboFonts_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboFonts.SelectedIndexChanged
 	   Dim ComboFonts As Drawing.Text.InstalledFontCollection
 	   ComboFonts = New Drawing.text.InstalledFontCollection

 	   If txtContents.SelectionLength > 0 Then
		 txtContents.SelectionFont = New System.Drawing.Font(ComboFonts.Families(cboFonts.SelectedIndex), CInt(cboFontSize.Text), Font.Style)
 		   txtContents.Focus()
 	   Else
		 txtContents.Font = New System.Drawing.Font(ComboFonts.Families(cboFonts.SelectedIndex), CInt(cboFontSize.Text), Font.Style)
 	   End If


    End Sub

I know the problem lies after the Else statement. I set the entire Font property of the RichTextBox, but how do I set it for just after whee the cursor is located?

Thanks!

P.S.
txtContents = RichTextBox
cboFonts = Font combobox
cboFontSize = Font size combobox
 
more than really late repling to this but i noticed this when looking for another thread and here's how to solve your problem

change this:
VB.NET:
		If txtContents.SelectionLength > 0 Then
		 txtContents.SelectionFont = New System.Drawing.Font(ComboFonts.Families(cboFonts.S electedIndex), CInt(cboFontSize.Text), Font.Style)
			txtContents.Focus()
		Else
		 txtContents.Font = New System.Drawing.Font(ComboFonts.Families(cboFonts.S electedIndex), CInt(cboFontSize.Text), Font.Style)
		End If

to:
VB.NET:
 		If txtContents.SelectionLength > 0 Then
		 txtContents.SelectionFont = New System.Drawing.Font(ComboFonts.Families(cboFonts.S electedIndex), CInt(cboFontSize.Text), Font.Style)
 			txtContents.Focus()
 		Else
		 txtContents.SelectionFont = New System.Drawing.Font(ComboFonts.Families(cboFonts.S electedIndex), CInt(cboFontSize.Text), Font.Style)
 		End If

by changing the font it refreshes all the text to that new font, whereas if you change just the selectionfont (which is nothing) it changes the font for just that spot (and anything typed afterwards too)
 
Back
Top