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. Windows Forms
  4. changing the background color of single word in textbox

changing the background color of single word in textbox

Scheduled Pinned Locked Moved Windows Forms
9 Posts 3 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.
  • U Offline
    U Offline
    User 2322509
    wrote on last edited by
    #1

    Hello All, Is there a way to change the background color of single word in textbox. I know it can be done in richtextbox, but i am looking for textbox. Thanks

    L A 2 Replies Last reply
    0
    • U User 2322509

      Hello All, Is there a way to change the background color of single word in textbox. I know it can be done in richtextbox, but i am looking for textbox. Thanks

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      No way. Use RTB. :)

      Luc Pattyn [Forum Guidelines] [My Articles]


      The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


      U 1 Reply Last reply
      0
      • L Luc Pattyn

        No way. Use RTB. :)

        Luc Pattyn [Forum Guidelines] [My Articles]


        The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


        U Offline
        U Offline
        User 2322509
        wrote on last edited by
        #3

        RTB is heavy for my scenario, i was thinking whether can be done by overriding OnPaint/WndProc? Thanks

        modified on Wednesday, July 22, 2009 6:19 PM

        L 1 Reply Last reply
        0
        • U User 2322509

          RTB is heavy for my scenario, i was thinking whether can be done by overriding OnPaint/WndProc? Thanks

          modified on Wednesday, July 22, 2009 6:19 PM

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          You can paint anything you want on any Control you choose, so the Form itself, or a Panel, would be fine. Why still use a Label if it doesn't do what you want? You still need to tell it somehow which parts need which colors. :)

          Luc Pattyn [Forum Guidelines] [My Articles]


          The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


          U 1 Reply Last reply
          0
          • L Luc Pattyn

            You can paint anything you want on any Control you choose, so the Form itself, or a Panel, would be fine. Why still use a Label if it doesn't do what you want? You still need to tell it somehow which parts need which colors. :)

            Luc Pattyn [Forum Guidelines] [My Articles]


            The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


            U Offline
            U Offline
            User 2322509
            wrote on last edited by
            #5

            I am able to accomplish this with following code. But the problem i am facing is, when i select the text the drawn string is vanishing and the string is back to unformatted with no background color. Any ideas whats going on? protected override void OnPaint(System.Windows.Forms.PaintEventArgs e) { Rectangle rect = new Rectangle(); rect.Y = this.Bounds.Y + 2; rect.Height = this.Bounds.Height - 5; string str = this.Text; int index = this.Text.IndexOf("test1"); string sBefore = str.Substring(0, index); string sWord = str.Substring(index, 5); Graphics g = this.CreateGraphics(); Size s1 = TextRenderer.MeasureText(e.Graphics, sBefore, this.Font, this.Bounds.Size); Size s2 = TextRenderer.MeasureText(e.Graphics, sWord, this.Font, this.Bounds.Size); //'adjust the widths to make the highlite more accurate if (s1.Width > 5) { rect.X = this.Bounds.X + s1.Width - 5; rect.Width = s2.Width - 6; } else { rect.X = this.Bounds.X + 2; rect.Width = s2.Width - 6; } //'use darker highlight when the row is selected SolidBrush hl_brush; hl_brush = new SolidBrush(Color.Yellow); //End If //'paint the background behind the search word e.Graphics.FillRectangle(hl_brush, rect); e.Graphics.DrawString(this.Text, this.Font, new SolidBrush(Color.Black), (Width - Size.Width) / 2, 0); //this.Text = string.Empty; //TextRenderer.DrawText(e.Graphics, this.Text, Font, Bounds, ForeColor, BackColor, // TextFormatFlags.HorizontalCenter | // TextFormatFlags.VerticalCenter | // TextFormatFlags.TextBoxControl); hl_brush.Dispose(); base.OnPaint(e); } Thanks.

            L 1 Reply Last reply
            0
            • U User 2322509

              I am able to accomplish this with following code. But the problem i am facing is, when i select the text the drawn string is vanishing and the string is back to unformatted with no background color. Any ideas whats going on? protected override void OnPaint(System.Windows.Forms.PaintEventArgs e) { Rectangle rect = new Rectangle(); rect.Y = this.Bounds.Y + 2; rect.Height = this.Bounds.Height - 5; string str = this.Text; int index = this.Text.IndexOf("test1"); string sBefore = str.Substring(0, index); string sWord = str.Substring(index, 5); Graphics g = this.CreateGraphics(); Size s1 = TextRenderer.MeasureText(e.Graphics, sBefore, this.Font, this.Bounds.Size); Size s2 = TextRenderer.MeasureText(e.Graphics, sWord, this.Font, this.Bounds.Size); //'adjust the widths to make the highlite more accurate if (s1.Width > 5) { rect.X = this.Bounds.X + s1.Width - 5; rect.Width = s2.Width - 6; } else { rect.X = this.Bounds.X + 2; rect.Width = s2.Width - 6; } //'use darker highlight when the row is selected SolidBrush hl_brush; hl_brush = new SolidBrush(Color.Yellow); //End If //'paint the background behind the search word e.Graphics.FillRectangle(hl_brush, rect); e.Graphics.DrawString(this.Text, this.Font, new SolidBrush(Color.Black), (Width - Size.Width) / 2, 0); //this.Text = string.Empty; //TextRenderer.DrawText(e.Graphics, this.Text, Font, Bounds, ForeColor, BackColor, // TextFormatFlags.HorizontalCenter | // TextFormatFlags.VerticalCenter | // TextFormatFlags.TextBoxControl); hl_brush.Dispose(); base.OnPaint(e); } Thanks.

              L Offline
              L Offline
              Luc Pattyn
              wrote on last edited by
              #6

              What do you think base.OnPaint(e); would do for a Label? :)

              Luc Pattyn [Forum Guidelines] [My Articles]


              The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


              U 1 Reply Last reply
              0
              • L Luc Pattyn

                What do you think base.OnPaint(e); would do for a Label? :)

                Luc Pattyn [Forum Guidelines] [My Articles]


                The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


                U Offline
                U Offline
                User 2322509
                wrote on last edited by
                #7

                text should be selectable.

                L 1 Reply Last reply
                0
                • U User 2322509

                  text should be selectable.

                  L Offline
                  L Offline
                  Luc Pattyn
                  wrote on last edited by
                  #8

                  I don't think OnPaint() contributes to selection. :)

                  Luc Pattyn [Forum Guidelines] [My Articles]


                  The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.


                  1 Reply Last reply
                  0
                  • U User 2322509

                    Hello All, Is there a way to change the background color of single word in textbox. I know it can be done in richtextbox, but i am looking for textbox. Thanks

                    A Offline
                    A Offline
                    Adrian Cole
                    wrote on last edited by
                    #9

                    You can't do this in the TextBox control. However, you could create your own control that derives from TextBox and override the Paint method to do whatever you wish. Of course you'd have to come up with some way of specifying which word(s) have different colour backgrounds. Seems like a lot of work when RichTextBox already does this.

                    while (e) { Coyote(); }

                    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