Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Draw string in different colors

Draw string in different colors

Scheduled Pinned Locked Moved Visual Basic
helpphpgraphicsquestion
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    tibmark
    wrote on last edited by
    #1

    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

    S 1 Reply Last reply
    0
    • T tibmark

      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

      S Offline
      S Offline
      Steve Pullan
      wrote on last edited by
      #2

      My suggestion of the Rich Edit control will be no use in this case. I hope someone else may be able to help. ...Steve

      T 1 Reply Last reply
      0
      • S Steve Pullan

        My suggestion of the Rich Edit control will be no use in this case. I hope someone else may be able to help. ...Steve

        T Offline
        T Offline
        tibmark
        wrote on last edited by
        #3

        Thank you for trying Mark Thibodeaux

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups