Changing the forecolor and backcolor of text in a textbox

限于喜欢 提交于 2019-12-12 10:52:06

问题


I'm making a C++ code editor application using VB.NET. I'd like to change the color of the keywords as the user types it. Also I'm looking for a way to highlight some lines of the code.

Is there a way to change the forecolor and backcolour of a piece of text inside a textbox or a rich textbox?


回答1:


I do not really know what you want to do so here are some options.

If you want a step by step tutorial on how to write a syntax highlighting control, you can read these articles:

  1. Syntax Highlighting in RichTextBox Part 1 and Part 2.
  2. Syntax Highlighting in RichTextBox using C#


For a basic syntax highlighting control written in VB.NET, see this article: Color Syntax Control (VB.NET).


There are two very good options if you want a fully-fledged syntax highlighting control. These are:

  1. ScintillaNET for WinForms and;
  2. AvalonEdit for WPF.
    • There is a tutorial on Using AvalonEdit on CodeProject so be sure to check that out as well.



回答2:


Yes, use RTF in the richtextbox control.

From http://www.biblioscape.com/rtf15_spec.htm

{\f1\cb1\cf2 This is colored text. The background is color
1 and the foreground is color 2.}



回答3:


Unfortunately, the normal foreground and background properties of a textbox cannot help, and you need to use rich text and format that manually (through text coloring and highlighting algorithms).



来源:https://stackoverflow.com/questions/3974479/changing-the-forecolor-and-backcolor-of-text-in-a-textbox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!