Draw string in different colors
-
I am trying to write a tooltip that can display a hyperlink or change the color of one word in a string. This what I have Dim MainString As String = "This word is a different color" Dim stringToChangeColor As String = "word" Dim range() As CharacterRange = {New CharacterRange(MainString.IndexOf(stringToChangeColor), stringToChangeColor.Length)} Dim format As New StringFormat format.SetMeasurableCharacterRanges(range) format.FormatFlags = StringFormatFlags.NoClip Dim region(1) As Region region = e.Graphics.MeasureCharacterRanges(MainString, Me.Font, New RectangleF(0, 0, Me.Width, Me.Height), format) Dim rec As New RectangleF rec = region(0).GetBounds(e.Graphics) Dim stringB As New StringBuilder stringB.Append(MainString) stringB.Replace(stringToChangeColor, " ", stringB.ToString.IndexOf(stringToChangeColor), stringToChangeColor.Length) rec.Width = rec.Width + 10 rec.X = rec.X - 2 rec.Y = 0 e.Graphics.DrawString(stringB.ToString, Me.Font, Brushes.Black, 0, 0) e.Graphics.DrawString(stringToChangeColor, Me.Font, Brushes.Red, rec.X, rec.Y, format) ' e.Graphics.DrawString(MainString, Me.Font, Brushes.RoyalBlue, 0, 20) The problem I have is that I have to manually set the number of spaces in the stringB.Replace. Is there another way? Please help. Thanks in advance Mark Thibodeaux
-
I am trying to write a tooltip that can display a hyperlink or change the color of one word in a string. This what I have Dim MainString As String = "This word is a different color" Dim stringToChangeColor As String = "word" Dim range() As CharacterRange = {New CharacterRange(MainString.IndexOf(stringToChangeColor), stringToChangeColor.Length)} Dim format As New StringFormat format.SetMeasurableCharacterRanges(range) format.FormatFlags = StringFormatFlags.NoClip Dim region(1) As Region region = e.Graphics.MeasureCharacterRanges(MainString, Me.Font, New RectangleF(0, 0, Me.Width, Me.Height), format) Dim rec As New RectangleF rec = region(0).GetBounds(e.Graphics) Dim stringB As New StringBuilder stringB.Append(MainString) stringB.Replace(stringToChangeColor, " ", stringB.ToString.IndexOf(stringToChangeColor), stringToChangeColor.Length) rec.Width = rec.Width + 10 rec.X = rec.X - 2 rec.Y = 0 e.Graphics.DrawString(stringB.ToString, Me.Font, Brushes.Black, 0, 0) e.Graphics.DrawString(stringToChangeColor, Me.Font, Brushes.Red, rec.X, rec.Y, format) ' e.Graphics.DrawString(MainString, Me.Font, Brushes.RoyalBlue, 0, 20) The problem I have is that I have to manually set the number of spaces in the stringB.Replace. Is there another way? Please help. Thanks in advance Mark Thibodeaux
My suggestion of the Rich Edit control will be no use in this case. I hope someone else may be able to help. ...Steve
-
My suggestion of the Rich Edit control will be no use in this case. I hope someone else may be able to help. ...Steve