如何在InkEdit控件中高亮搜索结果的指定文本?
解决InkEdit控件中格式化指定单元格内容的问题
InkEdit控件不支持通过嵌入格式标记(如\b)来设置文本样式,必须通过其自身的选择属性动态应用格式。你无需大幅重构现有代码,只需将原有的字符串拼接逻辑改为直接向InkEdit控件逐段添加内容,并在添加目标单元格内容时开启加粗效果:
修改后的代码
If matchFound Then ' 假设你的InkEdit控件名为inkEditResults With inkEditResults ' 添加第1列(加粗) .SelBold = True .SelText = row.Cells(1, 1).Value & " | " .SelBold = False ' 添加第2列(正常) .SelText = row.Cells(1, 2).Value & " | " ' 添加第3列(加粗) .SelBold = True .SelText = row.Cells(1, 3).Value & " | " .SelBold = False ' 添加第4列(正常) .SelText = "se" & row.Cells(1, 4).Value & " | " ' 添加第5列(加粗) .SelBold = True .SelText = "ep" & row.Cells(1, 5).Value & " | " .SelBold = False ' 添加第6列(正常) .SelText = row.Cells(1, 6).Value & " | " ' 添加第7列(加粗) .SelBold = True .SelText = row.Cells(1, 7).Value & " | " .SelBold = False ' 添加第8列(正常) .SelText = row.Cells(1, 8).Value & " | " ' 添加第9列(加粗) .SelBold = True .SelText = row.Cells(1, 9).Value .SelBold = False ' 添加分隔线和换行 .SelText = vbCrLf & String(284, "-") & vbCrLf End With resultCount = resultCount + 1 End If
关键说明
- 使用
SelBold = True开启加粗,添加完目标单元格内容后设回False恢复正常样式。 - 通过
SelText直接向控件追加内容,无需预先构建整个结果字符串。 - 保留了原有的分隔符、换行和计数逻辑,仅调整了内容输出方式。
内容的提问来源于stack exchange,提问作者Spero
相关产品推荐
相关产品推荐

