Windows Develop Bookmark and Share   
 index > Windows Forms General > Changing Index to Font and Size comboboxes, in a RixhTextBox.
 

Changing Index to Font and Size comboboxes, in a RixhTextBox.

Hello, i wonder if i could get some help over here.

I have a richtextbox and i use 2 comboboxes to display the font and the size of the rtb's text.

When there is no selected text the comboboxes display the exact font name and size whenever and wherever i click the mouse inside the written text using the following code.

Code Snippet

Dim SelectionLength As Integer = rtb1.SelectionLength

If SelectionLength = 0

Then

ToolStripComboBox1.Text = rtb1.SelectionFont.Size.ToString()

ToolStripComboBox2.Text = rtb1.SelectionFont.Name.ToString()

End if

When i have selected text though one problem appears.

When the selected text includes only one font or size everything is ok. Comboboxes display the font name and size.

When the selected text includes different fonts i cant find a way to make the combobox remain blank. Same goes for different sizes. (I want the comboboxes to get a "" text when different sizes or fonts exist inside the selected text like it happens with word)

Any help to this?

With regards,

Theodor Panagiotakopoulos.

dreameR-  Thursday, September 20, 2007 10:48 AM

dreameR,

It could be accomplished using EM_GETCHARFORMAT message.It will retrieve selection text height even if different fonts were used.

Code Snippet

'DLL Imports

<StructLayout(LayoutKind.Sequential)> _

Public Structure CHARFORMAT2

Public cbSize As Integer

Public dwMask As UInt32

Public dwEffects As UInt32

Public yHeight As Integer

Public yOffset As Integer

Public crTextColor As Integer

Public bCharSet As Byte

Public bPitchAndFamily As Byte

<MarshalAs(UnmanagedType.ByValArray, SizeConst:=&H20)> _

Public szFaceName As Char()

Public wWeight As Short

Public sSpacing As Short

Public crBackColor As Integer

Public LCID As Integer

Public dwReserved As UInt32

Public sStyle As Short

Public wKerning As Short

Public bUnderlineType As Byte

Public bAnimation As Byte

Public bRevAuthor As Byte

End Structure

<DllImport("user32.dll", CharSet:=CharSet.Auto)> _

Public Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal msg As Integer, ByVal wParam As Integer, ByRef lParam As CHARFORMAT2) As Integer

End Function

' Constants

Private Const EM_GETCHARFORMAT As Integer = &H43A

Private Const SCF_SELECTION As Integer = 1

Private Const CFM_SIZE As Integer = &H80000000

Private Sub rtb1_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rtb1.SelectionChanged

If rtb1.SelectionFont Is Nothing Then

ToolStripComboBox2.Text = ""

Dim nHeight As Integer = GetSelectionHeight()

If nHeight <> 0 Then

ToolStripComboBox1.Text = nHeight.ToString()

Else

ToolStripComboBox1.Text = ""

End If

Else

ToolStripComboBox1.Text = rtb1.SelectionFont.SizeInPoints.ToString()

ToolStripComboBox2.Text = rtb1.SelectionFont.Name.ToString()

End If

End Sub

Private Function GetSelectionHeight() As Integer

Dim chFormat As New CHARFORMAT2

chFormat.cbSize = Marshal.SizeOf(chFormat)

Dim nSelectedTextFormatting As Integer = SendMessage(rtb1.Handle, EM_GETCHARFORMAT, SCF_SELECTION, chFormat)

Dim nFontHeight As Integer = 0

If ((nSelectedTextFormatting And CFM_SIZE) = CFM_SIZE) Then

nFontHeight = (chFormat.yHeight / 20) ' returned height is in twips(1/20 point)

End If

Return nFontHeight

End Function

Regards,
Oleh.

Oleh Svintsitskyy  Thursday, September 20, 2007 4:23 PM

Hi Theodor,

If current text selection has more than one font specified, this property isNothing.

It could be updated to

Code Snippet

If rtb1.SelectionFont Is Nothing Then

ToolStripComboBox1.Text = ""

ToolStripComboBox2.Text = ""

Else

ToolStripComboBox1.Text = rtb1.SelectionFont.Size.ToString()

ToolStripComboBox2.Text = rtb1.SelectionFont.Name.ToString()

End If

Regards,
Oleh.

Oleh Svintsitskyy  Thursday, September 20, 2007 12:19 PM

Hello again and thnx for your response.

The code you provided hassome problems:

In this section:

Code Snippet

If rtb1.SelectionFont Is Nothing Then

ToolStripComboBox1.Text = ""

ToolStripComboBox2.Text = ""

If Size remains stable then why get a "" value to the size combobox?

In this section

Code Snippet

Else

ToolStripComboBox1.Text = rtb1.SelectionFont.Size.ToString()

ToolStripComboBox2.Text = rtb1.SelectionFont.Name.ToString()

if size changes then the combobox should get a "" value.

In other words this code affects only font changes and not size changes. Using your code i came up with something.

Code Snippet

If rtb1.SelectionFont Is Nothing Then

ToolStripComboBox1.Text = rtb1.SelectionFont.Size.ToString()

ToolStripComboBox2.Text = ""

ElseIf [....................]

ToolStripComboBox1.Text = ""

ToolStripComboBox2.Text = ""

Else

If [....................]

ToolStripComboBox2.Text = rtb1.SelectionFont.Name.ToString()

ToolStripComboBox1.Text = ""

Else

ToolStripComboBox2.Text = rtb1.SelectionFont.Name.ToString()

ToolStripComboBox1.Text = rtb1.SelectionFont.Size.ToString()

End If

End If

I think this works out all the combinations when size or font are altered.

The 2 expressions missing are these that express the size change.

I am still missing the code whichwill do thisin a vb.NET way Tongue Tied

Some help?? Smile

dreameR-  Thursday, September 20, 2007 1:25 PM

dreameR,

It could be accomplished using EM_GETCHARFORMAT message.It will retrieve selection text height even if different fonts were used.

Code Snippet

'DLL Imports

<StructLayout(LayoutKind.Sequential)> _

Public Structure CHARFORMAT2

Public cbSize As Integer

Public dwMask As UInt32

Public dwEffects As UInt32

Public yHeight As Integer

Public yOffset As Integer

Public crTextColor As Integer

Public bCharSet As Byte

Public bPitchAndFamily As Byte

<MarshalAs(UnmanagedType.ByValArray, SizeConst:=&H20)> _

Public szFaceName As Char()

Public wWeight As Short

Public sSpacing As Short

Public crBackColor As Integer

Public LCID As Integer

Public dwReserved As UInt32

Public sStyle As Short

Public wKerning As Short

Public bUnderlineType As Byte

Public bAnimation As Byte

Public bRevAuthor As Byte

End Structure

<DllImport("user32.dll", CharSet:=CharSet.Auto)> _

Public Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal msg As Integer, ByVal wParam As Integer, ByRef lParam As CHARFORMAT2) As Integer

End Function

' Constants

Private Const EM_GETCHARFORMAT As Integer = &H43A

Private Const SCF_SELECTION As Integer = 1

Private Const CFM_SIZE As Integer = &H80000000

Private Sub rtb1_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rtb1.SelectionChanged

If rtb1.SelectionFont Is Nothing Then

ToolStripComboBox2.Text = ""

Dim nHeight As Integer = GetSelectionHeight()

If nHeight <> 0 Then

ToolStripComboBox1.Text = nHeight.ToString()

Else

ToolStripComboBox1.Text = ""

End If

Else

ToolStripComboBox1.Text = rtb1.SelectionFont.SizeInPoints.ToString()

ToolStripComboBox2.Text = rtb1.SelectionFont.Name.ToString()

End If

End Sub

Private Function GetSelectionHeight() As Integer

Dim chFormat As New CHARFORMAT2

chFormat.cbSize = Marshal.SizeOf(chFormat)

Dim nSelectedTextFormatting As Integer = SendMessage(rtb1.Handle, EM_GETCHARFORMAT, SCF_SELECTION, chFormat)

Dim nFontHeight As Integer = 0

If ((nSelectedTextFormatting And CFM_SIZE) = CFM_SIZE) Then

nFontHeight = (chFormat.yHeight / 20) ' returned height is in twips(1/20 point)

End If

Return nFontHeight

End Function

Regards,
Oleh.

Oleh Svintsitskyy  Thursday, September 20, 2007 4:23 PM

Ok oleh this seems that is solving the problem.

Thanx a lot for your help.

Regards,

Theodor.

dreameR-  Wednesday, September 26, 2007 9:42 AM

You can use google to search for other answers

Custom Search

More Threads

• events are fired but the form looks frozen
• Auto resize columns in TableLayoutPanel
• error: null is not valid value for stream
• Windows Media Video 9 Codec Settings
• in vb.net windows program how find new attach pen drive
• Clarification on label_textchanged Event.
• Recording live video using VB6
• filling treeview from xml file
• NullReferenceException, how do I find the referenced object?
• Checkboxes that work similar to radio buttons????