VB.NET如何显示多种颜色的一段文本?

来源:百度知道 编辑:UC知道 时间:2024/09/28 10:25:51
我想在一个固定宽度的Label中,显示一段由多种不同颜色的字符组成的多行文本,比如一个自定义格式字符串"<red>红色字<yellow>黄色字"。文本的长度和颜色的位置是在运行时确定的。应该怎么做?

Label控件是没有办法实现多种颜色的文字的,只能用RichTextBox来实现,而且你的自定义格式字符串也没有结尾的,这样很不好,至少也要<red>红色字</red><yellow>黄色字</yellow>,而且实现也很麻烦的,下面的代码我没有检测正确性,有错误的自己改一改吧

Dim colortag() as string
dim colors() as color
const txt as string="<red>红色字</red><yellow>黄色字</yellow>"

private sub Form_Load(object sender,eventargs e)handles mybase.load
colortag(0)="red":Colortag(1)="yellow"
colors(0)=color.red:colors(1)=color.yellow
richtextbox1.text=txt

for i as integer=0 to colortag.lenght-1
dim tag as string="<" & colortag(i) & ">"
dim endtag as string="</" & colortag(i) & ">"
dim find as integer=1

do
find=instr(find,txt,tag)+tag.lenght
if(find<>0)then
dim find1 as integer=instr(find,txt,endtag)
richtextbox1.SelectionStart=find
richtextbox1.selectionlenght=find1-find
richtextb