文本框中的每个字符在vba中都以不同的颜色表示 [英] Every character in TextBox in different color in vba excel
问题描述
我正在寻找一种使用VBA以不同颜色显示 UserForm 中 TextBox 中的每个字母的方式。例如第一个字符红色,第二个蓝色,第三个...。有没有办法这样做?
I'm looking for a way to display every letter in a TextBox within a UserForm in different colors using VBA. For example first character red,second blue,third... . Is there any way to do this?
推荐答案
这是不可能使用 TextBox控件。 >
如果您使用的是 Excel 2010 或更高版本,则可以使用 InkEdit Control 。
它是下的一个附加控件>工具>其他控件(Excel VBE)。
This is not possible using TextBox Control.
If you are using Excel 2010 or higher however, you can use InkEdit Control.
It is an additional control under Tools > Additional Controls (Excel VBE).
添加完毕后,您可以在工具箱中看到
Once you've added it, it will be available in your Toolbox as seen below.
然后,您可以将其作为您的用户窗体中的另一个控件使用。
要知道更多关于您可以使用 InkEdit控件的功能,请查看 MSDN 。
Btw,这里是一个示例代码,颜色为en
You can then use it as another Control in 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.
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
对于上述示例,我将在控件中输入的字符数限制为5.
InkEdit 都接受RGB 和颜色索引,但我很乐意使用 RGB 。
您的问题缺乏细节,因此无法提供更多信息。只是希望这有助于您。
如果有的话,您的Excel版本不是2010年,我认为对于较低版本也有同等的控制。
For the above sample, I limit the number of characters entered in the control to 5.
InkEdit both accepts RGB and Color Index but 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.
结果:
Result:
这篇关于文本框中的每个字符在vba中都以不同的颜色表示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!