vba excel中不同颜色的TextBox中的每个字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23598211/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 03:06:02  来源:igfitidea点击:

Every character in TextBox in different color in vba excel

excelvbaexcel-vbatextbox

提问by user3626548

I'm looking for a way to display every letter in a TextBoxwithin a UserFormin different colors using VBA. For example first character red,second blue,third... . Is there any way to do this?

我正在寻找一种方式来显示在每一个字母文本框一内用户窗体中使用VBA不同的颜色。例如第一个字符红色,第二个蓝色,第三个...。有没有办法做到这一点?

回答by L42

This is not possible using TextBox Control.
If you are using Excel 2010or higher however, you can use InkEdit Control.
It is an additional control under Tools > Additional Controls (Excel VBE).

使用TextBox Control是不可能的。但是,
如果您使用的是Excel 2010或更高版本,则可以使用InkEdit Control
它是工具 > 附加控件 (Excel VBE)下的附加控件

enter image description here

enter image description here

Once you've added it, it will be available in your Toolboxas seen below.

添加后,它将在您的工具箱中可用,如下所示。

enter image description here

enter image description here

You can then use it as another Controlin your UserForm.
To know more about what you can do with an InkEdit Control, check MSDN.
Btw, here is a sample code which colors the entry(per Char) based on its position as you type.

然后,您可以使用它作为另一个控制用户窗体
要了解有关使用InkEdit 控件可以做什么的更多信息,请查看MSDN
顺便说一句,这是一个示例代码,它根据您键入时的位置为条目(每个字符)着色。

Private Sub InkEdit1_Change()

Dim i As Long

If Me.InkEdit1.Text <> "" Then
    For i = 1 To Len(Me.InkEdit1.Text)
        Me.InkEdit1.SelStart = i - 1 '~~> identifies the start of selection
        Me.InkEdit1.SelLength = 1 '~~> identifies the length of selection
        Select Case i
        Case 1: Me.InkEdit1.SelColor = RGB(255, 0, 0) '~~> colors 1st char Red
        Case 2: Me.InkEdit1.SelColor = RGB(0, 255, 0) '~~> colors 2nd char Green
        Case 3: Me.InkEdit1.SelColor = RGB(0, 0, 255) '~~> colors 3rd char Blue
        Case 4: Me.InkEdit1.SelColor = RGB(255, 255, 0) '~~> colors 4th char Yellow
        Case 5: Me.InkEdit1.SelColor = RGB(255, 0, 255) '~~> colors 5th char Indigo
        End Select
        Me.InkEdit1.SelStart = i '~~> this puts the cursor to end
    Next
End If

End Sub

For the above sample, I limit the number of characters entered in the control to 5.
InkEditboth accepts RGBand Color Indexbut I'm comfortable in using RGB.
Your question is lacking details so I cannot provide more; just hopes that this helps you a bit.
If ever, your version of Excel is not 2010, I think there's an equivalent control for lower versions as well.

对于上面的示例,我将在控件中输入的字符数限制为
5。InkEdit都接受RGB颜色索引,但我很习惯使用RGB
您的问题缺乏详细信息,因此我无法提供更多信息;只是希望这对你有所帮助。
如果有的话,您的 Excel 版本不是 2010,我认为对于较低版本也有等效的控件。

Result:
enter image description here

结果:
enter image description here