changing the background color of single word in textbox
-
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
-
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
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.
-
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.
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
-
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
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.
-
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.
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. -
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.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.
-
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.
text should be selectable.
-
text should be selectable.
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.
-
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
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(); }