简体   繁体   中英

How change the color of a specific word in a string? vb.net

I have a problem with color of a specific word in a string. I have my string like that:

1) i Create a table like that:

Dim SequenceTable As New DataTable
SequenceTable.Columns.Add("Paragraph", GetType(String))
SequenceTable.Columns.Add("Description", GetType(String))
SequenceTable.Columns.Add("ActivityName", GetType(String))

2) I get value Paragraph, Description, and activityName:

If SequenceRow.IsNull("ActivityNumber") = False Then
                                ActivityNumber = SequenceRow.ActivityNumber
                            End If

                            If SequenceRow.IsNull("Specification") = False Then
                                Specification = SequenceRow.Specification
                            End If

                            If SequenceRow.IsNull("Dimension") = False Then
                                Paragraph = SequenceRow.Dimension
                            End If

3) I put all string in table:

                    Cell = New TableCell
                    Cell.HorizontalAlign = HorizontalAlign.Left
                    Cell.Width = Unit.Percentage(16.5)
                    Cell.CssClass = "FormatTabel"
                    Cell.Text = Description
                    Cell.Font.Size = 6
                    Cell.Wrap = True
                    Row.Cells.Add(Cell)

My string in description

Description = "Presence and pictures required. Raw data and multiload required. "

I want the word pictures to be red and all word to be black? But how can i set red color for word "pictures" and after put in table?

Thanks

You can add <span/> with formatted text using InnerHtml in your cell instead Text

 Cell = New TableCell
 Cell.HorizontalAlign = HorizontalAlign.Left
 Cell.Width = Unit.Percentage(16.5)
 Cell.InnerHtml = "<span  style='color: yellow'>" + "Presence" + "</span>" + "<span  style='color: black'>" + "...black text" + "</span>";
 Cell.Font.Size = 6
 Cell.Wrap = True
 Row.Cells.Add(Cell)
string AddColor(string Text, string word string color)
{
    return Text.Replace(word, "<span color: '" + color + "'>"+word+"</span>")
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM