据说用API可以做到textbox控件内的文本显示不同的颜色。如果是初学者,建议用richtextbox控件,完全可以满足你的要求。
在每个textbox控件的change事件中,先设置richtextbox1.selcolor,再显示输入的数字。
...
这个问题还不是初想的这么简单,主要问题是在richtextbox中新增内容时,会影响前面的字体属性。用到了下列API,自己建一工程试试。三个textbox(文本框组)、一个richtextbox。
Option Explicit
Private Declare Function SendMessage Lib "User32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Declare Function SendMessageByNum Lib "User32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Private Declare Function SendMessageByString Lib "User32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As String) As Long
Private Const EM_LINELENGTH = &HC1
Private Const EM_REPLACESEL = &HC2
Private Const EM_SETSEL = &HB1
Private Const EM_GETSEL = &HB0
Public Sub AddText(RichTextBoxName As Object, SendString As String)
Dim K As Long, Hwd As Long
Hwd = RichTextBoxName.hWnd
SendMessage Hwd, EM_GETSEL, 0, K
SendMessageByNum Hwd, EM_SETSEL, K, K
SendMessageByString Hwd, EM_REPLACESEL, 1, SendString
End Sub
Private Sub Form_Load()
Text1(0).ForeColor = vbRed
Text1(1).ForeColor = vbGreen
Text1(2).ForeColor = vbBlue
End Sub
Private Sub Text1_KeyDown(Index As Integer, KeyCode As Integer, Shift As Integer)
RichTextBox1.SelColor = Text1(Index).ForeColor
Call AddText(RichTextBox1, Chr(KeyCode))
End Sub
[
本帖最后由 lianyicq 于 2015-4-7 10:18 编辑 ]