看看下面代码就知道拉!其实很EASY!Option Explicit '事件声明: Event Click() 'MappingInfo=Text1,Text1,-1,Click Event DblClick() 'MappingInfo=Text1,Text1,-1,DblClick Event KeyDown(KeyCode As Integer, Shift As Integer) 'MappingInfo=Text1,Text1,-1,KeyDown Event KeyPress(KeyAscii As Integer) 'MappingInfo=Text1,Text1,-1,KeyPress Event KeyUp(KeyCode As Integer, Shift As Integer) 'MappingInfo=Text1,Text1,-1,KeyUp Event MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single) 'MappingInfo=Text1,Text1,-1,MouseDown Event MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single) 'MappingInfo=Text1,Text1,-1,MouseMove Event MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single) 'MappingInfo=Text1,Text1,-1,MouseUp Private Sub UserControl_Initialize()
End SubPrivate Sub UserControl_Resize() Text1.Top = 0 Text1.Left = 0 Text1.Height = UserControl.Height Text1.Width = UserControl.Width End Sub '注意!不要删除或修改下列被注释的行! 'MappingInfo=Text1,Text1,-1,BackColor Public Property Get BackColor() As OLE_COLOR BackColor = Text1.BackColor End PropertyPublic Property Let BackColor(ByVal New_BackColor As OLE_COLOR) Text1.BackColor() = New_BackColor PropertyChanged "BackColor" End Property'注意!不要删除或修改下列被注释的行! 'MappingInfo=UserControl,UserControl,-1,ForeColor Public Property Get ForeColor() As OLE_COLOR ForeColor = UserControl.ForeColor End PropertyPublic Property Let ForeColor(ByVal New_ForeColor As OLE_COLOR) UserControl.ForeColor() = New_ForeColor PropertyChanged "ForeColor" End Property'注意!不要删除或修改下列被注释的行! 'MappingInfo=Text1,Text1,-1,Enabled Public Property Get Enabled() As Boolean Enabled = Text1.Enabled End PropertyPublic Property Let Enabled(ByVal New_Enabled As Boolean) Text1.Enabled() = New_Enabled PropertyChanged "Enabled" End Property'注意!不要删除或修改下列被注释的行! 'MappingInfo=Text1,Text1,-1,Font Public Property Get Font() As Font Set Font = Text1.Font End PropertyPublic Property Set Font(ByVal New_Font As Font) Set Text1.Font = New_Font PropertyChanged "Font" End Property'注意!不要删除或修改下列被注释的行! 'MappingInfo=Text1,Text1,-1,Refresh Public Sub Refresh() Text1.Refresh End SubPrivate Sub Text1_Click() RaiseEvent Click End SubPrivate Sub Text1_DblClick() RaiseEvent DblClick End SubPrivate Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer) RaiseEvent KeyDown(KeyCode, Shift) End SubPrivate Sub Text1_KeyPress(KeyAscii As Integer) RaiseEvent KeyPress(KeyAscii) End SubPrivate Sub Text1_KeyUp(KeyCode As Integer, Shift As Integer) RaiseEvent KeyUp(KeyCode, Shift) End SubPrivate Sub Text1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single) RaiseEvent MouseDown(Button, Shift, X, Y) End SubPrivate Sub Text1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single) RaiseEvent MouseMove(Button, Shift, X, Y) End SubPrivate Sub Text1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single) RaiseEvent MouseUp(Button, Shift, X, Y) End Sub'注意!不要删除或修改下列被注释的行! 'MappingInfo=Text1,Text1,-1,Text Public Property Get myText() As String myText = Text1.Text End PropertyPublic Property Let myText(ByVal New_myText As String) Text1.Text() = New_myText PropertyChanged "myText" End Property'从存贮器中加载属性值 Private Sub UserControl_ReadProperties(PropBag As PropertyBag) Text1.BackColor = PropBag.ReadProperty("BackColor", &H80000005) UserControl.ForeColor = PropBag.ReadProperty("ForeColor", &H80000012) Text1.Enabled = PropBag.ReadProperty("Enabled", True) Set Text1.Font = PropBag.ReadProperty("Font", Ambient.Font) Text1.Text = PropBag.ReadProperty("myText", "Text1") End Sub'将属性值写到存储器 Private Sub UserControl_WriteProperties(PropBag As PropertyBag) Call PropBag.WriteProperty("BackColor", Text1.BackColor, &H80000005) Call PropBag.WriteProperty("ForeColor", UserControl.ForeColor, &H80000012) Call PropBag.WriteProperty("Enabled", Text1.Enabled, True) Call PropBag.WriteProperty("Font", Text1.Font, Ambient.Font) Call PropBag.WriteProperty("myText", Text1.Text, "Text1") End Sub
新建一个ActiveX 控件 在上面放一个 Text控件 在把下面代码Copy 一下,生成一个ocx控件,就可以实现你的功能。 Option Explicit '事件声明: Event Click() 'MappingInfo=Text1,Text1,-1,Click Event DblClick() 'MappingInfo=Text1,Text1,-1,DblClick Event KeyDown(KeyCode As Integer, Shift As Integer) 'MappingInfo=Text1,Text1,-1,KeyDown Event KeyPress(KeyAscii As Integer) 'MappingInfo=Text1,Text1,-1,KeyPress Event KeyUp(KeyCode As Integer, Shift As Integer) 'MappingInfo=Text1,Text1,-1,KeyUp Event MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single) 'MappingInfo=Text1,Text1,-1,MouseDown Event MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single) 'MappingInfo=Text1,Text1,-1,MouseMove Event MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single) 'MappingInfo=Text1,Text1,-1,MouseUp '缺省属性值: Const m_def_AfterGotFocus_Color = vbRed '属性变量: Dim m_AfterGotFocus_Color As OLE_COLOR Dim textBackColor As OLE_COLOR Private Sub Text1_GotFocus() textBackColor = Text1.BackColor Text1.BackColor = Me.AfterGotFocus_Color End SubPrivate Sub Text1_LostFocus() Text1.BackColor = textBackColor End SubPrivate Sub UserControl_GotFocus() textBackColor = Text1.BackColor Text1.BackColor = Me.AfterGotFocus_Color End SubPrivate Sub UserControl_Resize() Text1.Top = 0 Text1.Left = 0 Text1.Height = UserControl.Height Text1.Width = UserControl.Width End Sub '注意!不要删除或修改下列被注释的行! 'MappingInfo=Text1,Text1,-1,BackColor Public Property Get BackColor() As OLE_COLOR BackColor = Text1.BackColor End PropertyPublic Property Let BackColor(ByVal New_BackColor As OLE_COLOR) Text1.BackColor() = New_BackColor PropertyChanged "BackColor" End Property'注意!不要删除或修改下列被注释的行! 'MappingInfo=UserControl,UserControl,-1,ForeColor Public Property Get ForeColor() As OLE_COLOR ForeColor = UserControl.ForeColor End PropertyPublic Property Let ForeColor(ByVal New_ForeColor As OLE_COLOR) UserControl.ForeColor() = New_ForeColor PropertyChanged "ForeColor" End Property'注意!不要删除或修改下列被注释的行! 'MappingInfo=Text1,Text1,-1,Enabled Public Property Get Enabled() As Boolean Enabled = Text1.Enabled End PropertyPublic Property Let Enabled(ByVal New_Enabled As Boolean) Text1.Enabled() = New_Enabled PropertyChanged "Enabled" End Property'注意!不要删除或修改下列被注释的行! 'MappingInfo=Text1,Text1,-1,Font Public Property Get Font() As Font Set Font = Text1.Font End PropertyPublic Property Set Font(ByVal New_Font As Font) Set Text1.Font = New_Font PropertyChanged "Font" End Property'注意!不要删除或修改下列被注释的行! 'MappingInfo=Text1,Text1,-1,Refresh Public Sub Refresh() Text1.Refresh End SubPrivate Sub Text1_Click() RaiseEvent Click End SubPrivate Sub Text1_DblClick() RaiseEvent DblClick End SubPrivate Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer) RaiseEvent KeyDown(KeyCode, Shift) End SubPrivate Sub Text1_KeyPress(KeyAscii As Integer) RaiseEvent KeyPress(KeyAscii) End SubPrivate Sub Text1_KeyUp(KeyCode As Integer, Shift As Integer) RaiseEvent KeyUp(KeyCode, Shift) End SubPrivate Sub Text1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single) RaiseEvent MouseDown(Button, Shift, X, Y) End SubPrivate Sub Text1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single) RaiseEvent MouseMove(Button, Shift, X, Y) End SubPrivate Sub Text1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single) RaiseEvent MouseUp(Button, Shift, X, Y) End Sub'注意!不要删除或修改下列被注释的行! 'MappingInfo=Text1,Text1,-1,Text Public Property Get myText() As String myText = Text1.Text End PropertyPublic Property Let myText(ByVal New_myText As String) Text1.Text() = New_myText PropertyChanged "myText" End Property'从存贮器中加载属性值 Private Sub UserControl_ReadProperties(PropBag As PropertyBag) Text1.BackColor = PropBag.ReadProperty("BackColor", &H80000005) UserControl.ForeColor = PropBag.ReadProperty("ForeColor", &H80000012) Text1.Enabled = PropBag.ReadProperty("Enabled", True) Set Text1.Font = PropBag.ReadProperty("Font", Ambient.Font) Text1.Text = PropBag.ReadProperty("myText", "Text1") m_AfterGotFocus_Color = PropBag.ReadProperty("AfterGotFocus_Color", m_def_AfterGotFocus_Color) End Sub'将属性值写到存储器 Private Sub UserControl_WriteProperties(PropBag As PropertyBag) Call PropBag.WriteProperty("BackColor", Text1.BackColor, &H80000005) Call PropBag.WriteProperty("ForeColor", UserControl.ForeColor, &H80000012) Call PropBag.WriteProperty("Enabled", Text1.Enabled, True) Call PropBag.WriteProperty("Font", Text1.Font, Ambient.Font) Call PropBag.WriteProperty("myText", Text1.Text, "Text1") Call PropBag.WriteProperty("AfterGotFocus_Color", m_AfterGotFocus_Color, m_def_AfterGotFocus_Color) End Sub'注意!不要删除或修改下列被注释的行! 'MemberInfo=10,0,0,vbRed Public Property Get AfterGotFocus_Color() As OLE_COLOR AfterGotFocus_Color = m_AfterGotFocus_Color End PropertyPublic Property Let AfterGotFocus_Color(ByVal New_AfterGotFocus_Color As OLE_COLOR) m_AfterGotFocus_Color = New_AfterGotFocus_Color PropertyChanged "AfterGotFocus_Color" End Property'为用户控件初始化属性 Private Sub UserControl_InitProperties() m_AfterGotFocus_Color = m_def_AfterGotFocus_Color End Sub
选择VB ActiveX Control Interface Wizard
是用过一段时间!
这个可能没什么作用,现在我想要在textbox获得焦点时可以改变背景色(可以在设计时输入,就像textbox的backcolor属性),应该怎么做?
'事件声明:
Event Click() 'MappingInfo=Text1,Text1,-1,Click
Event DblClick() 'MappingInfo=Text1,Text1,-1,DblClick
Event KeyDown(KeyCode As Integer, Shift As Integer) 'MappingInfo=Text1,Text1,-1,KeyDown
Event KeyPress(KeyAscii As Integer) 'MappingInfo=Text1,Text1,-1,KeyPress
Event KeyUp(KeyCode As Integer, Shift As Integer) 'MappingInfo=Text1,Text1,-1,KeyUp
Event MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single) 'MappingInfo=Text1,Text1,-1,MouseDown
Event MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single) 'MappingInfo=Text1,Text1,-1,MouseMove
Event MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single) 'MappingInfo=Text1,Text1,-1,MouseUp
Private Sub UserControl_Initialize()
End SubPrivate Sub UserControl_Resize()
Text1.Top = 0
Text1.Left = 0
Text1.Height = UserControl.Height
Text1.Width = UserControl.Width
End Sub
'注意!不要删除或修改下列被注释的行!
'MappingInfo=Text1,Text1,-1,BackColor
Public Property Get BackColor() As OLE_COLOR
BackColor = Text1.BackColor
End PropertyPublic Property Let BackColor(ByVal New_BackColor As OLE_COLOR)
Text1.BackColor() = New_BackColor
PropertyChanged "BackColor"
End Property'注意!不要删除或修改下列被注释的行!
'MappingInfo=UserControl,UserControl,-1,ForeColor
Public Property Get ForeColor() As OLE_COLOR
ForeColor = UserControl.ForeColor
End PropertyPublic Property Let ForeColor(ByVal New_ForeColor As OLE_COLOR)
UserControl.ForeColor() = New_ForeColor
PropertyChanged "ForeColor"
End Property'注意!不要删除或修改下列被注释的行!
'MappingInfo=Text1,Text1,-1,Enabled
Public Property Get Enabled() As Boolean
Enabled = Text1.Enabled
End PropertyPublic Property Let Enabled(ByVal New_Enabled As Boolean)
Text1.Enabled() = New_Enabled
PropertyChanged "Enabled"
End Property'注意!不要删除或修改下列被注释的行!
'MappingInfo=Text1,Text1,-1,Font
Public Property Get Font() As Font
Set Font = Text1.Font
End PropertyPublic Property Set Font(ByVal New_Font As Font)
Set Text1.Font = New_Font
PropertyChanged "Font"
End Property'注意!不要删除或修改下列被注释的行!
'MappingInfo=Text1,Text1,-1,Refresh
Public Sub Refresh()
Text1.Refresh
End SubPrivate Sub Text1_Click()
RaiseEvent Click
End SubPrivate Sub Text1_DblClick()
RaiseEvent DblClick
End SubPrivate Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)
RaiseEvent KeyDown(KeyCode, Shift)
End SubPrivate Sub Text1_KeyPress(KeyAscii As Integer)
RaiseEvent KeyPress(KeyAscii)
End SubPrivate Sub Text1_KeyUp(KeyCode As Integer, Shift As Integer)
RaiseEvent KeyUp(KeyCode, Shift)
End SubPrivate Sub Text1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
RaiseEvent MouseDown(Button, Shift, X, Y)
End SubPrivate Sub Text1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
RaiseEvent MouseMove(Button, Shift, X, Y)
End SubPrivate Sub Text1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
RaiseEvent MouseUp(Button, Shift, X, Y)
End Sub'注意!不要删除或修改下列被注释的行!
'MappingInfo=Text1,Text1,-1,Text
Public Property Get myText() As String
myText = Text1.Text
End PropertyPublic Property Let myText(ByVal New_myText As String)
Text1.Text() = New_myText
PropertyChanged "myText"
End Property'从存贮器中加载属性值
Private Sub UserControl_ReadProperties(PropBag As PropertyBag) Text1.BackColor = PropBag.ReadProperty("BackColor", &H80000005)
UserControl.ForeColor = PropBag.ReadProperty("ForeColor", &H80000012)
Text1.Enabled = PropBag.ReadProperty("Enabled", True)
Set Text1.Font = PropBag.ReadProperty("Font", Ambient.Font)
Text1.Text = PropBag.ReadProperty("myText", "Text1")
End Sub'将属性值写到存储器
Private Sub UserControl_WriteProperties(PropBag As PropertyBag) Call PropBag.WriteProperty("BackColor", Text1.BackColor, &H80000005)
Call PropBag.WriteProperty("ForeColor", UserControl.ForeColor, &H80000012)
Call PropBag.WriteProperty("Enabled", Text1.Enabled, True)
Call PropBag.WriteProperty("Font", Text1.Font, Ambient.Font)
Call PropBag.WriteProperty("myText", Text1.Text, "Text1")
End Sub
Option Explicit
'事件声明:
Event Click() 'MappingInfo=Text1,Text1,-1,Click
Event DblClick() 'MappingInfo=Text1,Text1,-1,DblClick
Event KeyDown(KeyCode As Integer, Shift As Integer) 'MappingInfo=Text1,Text1,-1,KeyDown
Event KeyPress(KeyAscii As Integer) 'MappingInfo=Text1,Text1,-1,KeyPress
Event KeyUp(KeyCode As Integer, Shift As Integer) 'MappingInfo=Text1,Text1,-1,KeyUp
Event MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single) 'MappingInfo=Text1,Text1,-1,MouseDown
Event MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single) 'MappingInfo=Text1,Text1,-1,MouseMove
Event MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single) 'MappingInfo=Text1,Text1,-1,MouseUp
'缺省属性值:
Const m_def_AfterGotFocus_Color = vbRed
'属性变量:
Dim m_AfterGotFocus_Color As OLE_COLOR
Dim textBackColor As OLE_COLOR
Private Sub Text1_GotFocus()
textBackColor = Text1.BackColor
Text1.BackColor = Me.AfterGotFocus_Color
End SubPrivate Sub Text1_LostFocus()
Text1.BackColor = textBackColor
End SubPrivate Sub UserControl_GotFocus()
textBackColor = Text1.BackColor
Text1.BackColor = Me.AfterGotFocus_Color
End SubPrivate Sub UserControl_Resize()
Text1.Top = 0
Text1.Left = 0
Text1.Height = UserControl.Height
Text1.Width = UserControl.Width
End Sub
'注意!不要删除或修改下列被注释的行!
'MappingInfo=Text1,Text1,-1,BackColor
Public Property Get BackColor() As OLE_COLOR
BackColor = Text1.BackColor
End PropertyPublic Property Let BackColor(ByVal New_BackColor As OLE_COLOR)
Text1.BackColor() = New_BackColor
PropertyChanged "BackColor"
End Property'注意!不要删除或修改下列被注释的行!
'MappingInfo=UserControl,UserControl,-1,ForeColor
Public Property Get ForeColor() As OLE_COLOR
ForeColor = UserControl.ForeColor
End PropertyPublic Property Let ForeColor(ByVal New_ForeColor As OLE_COLOR)
UserControl.ForeColor() = New_ForeColor
PropertyChanged "ForeColor"
End Property'注意!不要删除或修改下列被注释的行!
'MappingInfo=Text1,Text1,-1,Enabled
Public Property Get Enabled() As Boolean
Enabled = Text1.Enabled
End PropertyPublic Property Let Enabled(ByVal New_Enabled As Boolean)
Text1.Enabled() = New_Enabled
PropertyChanged "Enabled"
End Property'注意!不要删除或修改下列被注释的行!
'MappingInfo=Text1,Text1,-1,Font
Public Property Get Font() As Font
Set Font = Text1.Font
End PropertyPublic Property Set Font(ByVal New_Font As Font)
Set Text1.Font = New_Font
PropertyChanged "Font"
End Property'注意!不要删除或修改下列被注释的行!
'MappingInfo=Text1,Text1,-1,Refresh
Public Sub Refresh()
Text1.Refresh
End SubPrivate Sub Text1_Click()
RaiseEvent Click
End SubPrivate Sub Text1_DblClick()
RaiseEvent DblClick
End SubPrivate Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)
RaiseEvent KeyDown(KeyCode, Shift)
End SubPrivate Sub Text1_KeyPress(KeyAscii As Integer)
RaiseEvent KeyPress(KeyAscii)
End SubPrivate Sub Text1_KeyUp(KeyCode As Integer, Shift As Integer)
RaiseEvent KeyUp(KeyCode, Shift)
End SubPrivate Sub Text1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
RaiseEvent MouseDown(Button, Shift, X, Y)
End SubPrivate Sub Text1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
RaiseEvent MouseMove(Button, Shift, X, Y)
End SubPrivate Sub Text1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
RaiseEvent MouseUp(Button, Shift, X, Y)
End Sub'注意!不要删除或修改下列被注释的行!
'MappingInfo=Text1,Text1,-1,Text
Public Property Get myText() As String
myText = Text1.Text
End PropertyPublic Property Let myText(ByVal New_myText As String)
Text1.Text() = New_myText
PropertyChanged "myText"
End Property'从存贮器中加载属性值
Private Sub UserControl_ReadProperties(PropBag As PropertyBag) Text1.BackColor = PropBag.ReadProperty("BackColor", &H80000005)
UserControl.ForeColor = PropBag.ReadProperty("ForeColor", &H80000012)
Text1.Enabled = PropBag.ReadProperty("Enabled", True)
Set Text1.Font = PropBag.ReadProperty("Font", Ambient.Font)
Text1.Text = PropBag.ReadProperty("myText", "Text1")
m_AfterGotFocus_Color = PropBag.ReadProperty("AfterGotFocus_Color", m_def_AfterGotFocus_Color)
End Sub'将属性值写到存储器
Private Sub UserControl_WriteProperties(PropBag As PropertyBag) Call PropBag.WriteProperty("BackColor", Text1.BackColor, &H80000005)
Call PropBag.WriteProperty("ForeColor", UserControl.ForeColor, &H80000012)
Call PropBag.WriteProperty("Enabled", Text1.Enabled, True)
Call PropBag.WriteProperty("Font", Text1.Font, Ambient.Font)
Call PropBag.WriteProperty("myText", Text1.Text, "Text1")
Call PropBag.WriteProperty("AfterGotFocus_Color", m_AfterGotFocus_Color, m_def_AfterGotFocus_Color)
End Sub'注意!不要删除或修改下列被注释的行!
'MemberInfo=10,0,0,vbRed
Public Property Get AfterGotFocus_Color() As OLE_COLOR
AfterGotFocus_Color = m_AfterGotFocus_Color
End PropertyPublic Property Let AfterGotFocus_Color(ByVal New_AfterGotFocus_Color As OLE_COLOR)
m_AfterGotFocus_Color = New_AfterGotFocus_Color
PropertyChanged "AfterGotFocus_Color"
End Property'为用户控件初始化属性
Private Sub UserControl_InitProperties()
m_AfterGotFocus_Color = m_def_AfterGotFocus_Color
End Sub