各位大侠,快来帮帮我呀,我都快郁闷死了
我是一名新手,用VB6.0做了一个只能输入数字的文本框自定义控件,编译完后在新建的项目中插入该部件,双击该控件,出现GotFocus代码,即使不输入任何代代码的情况下运行程序,自定义的控件却消失了.
以下是我该自定义控件的代码,该自定义控件上只有一个名为NumTextBox的文本框Option ExplicitPrivate Sub UserControl_GotFocus()
NumTextBox_GotFocus
End SubPrivate Sub UserControl_KeyPress(KeyAscii As Integer)
NumTextBox_KeyPress (KeyAscii)
End SubPrivate Sub UserControl_ReadProperties(PropBag As PropertyBag)
Set NumTextbox.Font = PropBag.ReadProperty("Font", Ambient.Font)
NumTextbox.BackColor = PropBag.ReadProperty("BackColor", Ambient.BackColor)
NumTextbox.Text = PropBag.ReadProperty("Text", "")
NumTextbox.Enabled = PropBag.ReadProperty("Enabled", True)
NumTextbox.MaxLength = PropBag.ReadProperty("Maxlength", 0)
NumTextbox.Visible = PropBag.ReadProperty("Visible", True)
NumTextbox.HelpContextID = PropBag.ReadProperty("HelpContextID", 0)
NumTextbox.ForeColor = PropBag.ReadProperty("ForeColor", Ambient.ForeColor)
NumTextbox.ToolTipText = PropBag.ReadProperty("ToolTipText")
End Sub
Private Sub UserControl_WriteProperties(PropBag As PropertyBag)
Call PropBag.WriteProperty("Font", NumTextbox.Font, Ambient.Font)
Call PropBag.WriteProperty("BackColor", NumTextbox.BackColor, Ambient.BackColor)
Call PropBag.WriteProperty("Text", NumTextbox.Text, "")
Call PropBag.WriteProperty("Enabled", NumTextbox.Enabled, True)
Call PropBag.WriteProperty("MaxLength", NumTextbox.MaxLength, 0)
Call PropBag.WriteProperty("Visible", NumTextbox.Visible, True)
Call PropBag.WriteProperty("HelpContextID", NumTextbox.HelpContextID, 0)
Call PropBag.WriteProperty("ForeColor", NumTextbox.ForeColor, Ambient.ForeColor)
Call PropBag.WriteProperty("ToolTipText", NumTextbox.ToolTipText)
End SubPrivate Sub NumTextBox_KeyPress(KeyAscii As Integer)
Select Case KeyAscii
Case Asc("-") '允许负数
If NumTextbox.SelStart = 0 Then
If Left(NumTextbox.Text, 1) = "-" Then
KeyAscii = 0
Beep
End If
Else
KeyAscii = 0
Beep
End If
Case 8
'无变化,退格键不屏蔽
' Case Asc(0)
' If Left(NumTextBox.Text, 1) = "0" Then
' KeyAscii = 0
' End If
Case Asc(".") '46 '允许小数点
If InStr(NumTextbox.Text, ".") Or Len(NumTextbox.Text) = 0 Or NumTextbox.SelText = NumTextbox.Text Then
KeyAscii = 0
Beep
End If
Case Is < Asc(0) '48
KeyAscii = 0
Beep
Case Is > Asc(9) '57
KeyAscii = 0
Beep
Case Else
If Left(NumTextbox.Text, 1) = "0" And Mid(NumTextbox.Text, 2, 1) <> "." Then
KeyAscii = 0
Beep
End If
End Select
End Sub
Private Sub UserControl_Resize()
NumTextbox.Left = 0
NumTextbox.Top = 0
NumTextbox.Height = UserControl.Height
NumTextbox.Width = UserControl.Width
End SubPrivate Sub NumTextBox_GotFocus()
NumTextbox.Left = 0
NumTextbox.Top = 0
NumTextbox.Height = UserControl.Height
NumTextbox.Width = UserControl.Width
NumTextbox.SelStart = 0
NumTextbox.SelLength = Len(NumTextbox.Text)
End Sub
Public Property Get BackColor() As OLE_COLOR
BackColor = NumTextbox.BackColor
End Property
Public Property Let BackColor(ByVal New_Color As OLE_COLOR)
NumTextbox.BackColor = New_Color
PropertyChanged "BackColor"
End PropertyPublic Property Get Font() As Font
Set Font = NumTextbox.Font
End Property
Public Property Set Font(ByVal New_Font As Font)
Set NumTextbox.Font = New_Font
PropertyChanged "Font"
End PropertyPublic Property Get Text() As String
Text = NumTextbox.Text
End Property
Public Property Let Text(ByVal New_Text As String)
NumTextbox.Text = New_Text
PropertyChanged "Text"
End Property
Public Property Get Enabled() As Boolean
Enabled = NumTextbox.Enabled
End Property
Public Property Let Enabled(ByVal New_value As Boolean)
NumTextbox.Enabled = New_value
PropertyChanged "Enabled"
End PropertyPublic Property Get MaxLength() As Integer
MaxLength = NumTextbox.MaxLength
End PropertyPublic Property Let MaxLength(ByVal vNewValue As Integer)
NumTextbox.MaxLength = vNewValue
PropertyChanged "MaxLength"
End PropertyPublic Property Get ToolTipText() As String
ToolTipText = NumTextbox.ToolTipText
End PropertyPublic Property Let ToolTipText(ByVal vNewValue As String)
NumTextbox.ToolTipText = vNewValue
PropertyChanged "ToolTipText"
End PropertyPublic Property Get Visible() As Boolean
Visible = NumTextbox.Visible
End PropertyPublic Property Let Visible(ByVal vNewValue As Boolean)
NumTextbox.Visible = vNewValue
PropertyChanged "Visible"
End PropertyPublic Property Get HelpContextID() As Integer
HelpContextID = NumTextbox.HelpContextID
End PropertyPublic Property Let HelpContextID(ByVal vNewValue As Integer)
NumTextbox.HelpContextID = vNewValue
PropertyChanged "HelpContextID"
End PropertyPublic Property Get ForeColor() As OLE_COLOR
ForeColor = NumTextbox.ForeColor
End PropertyPublic Property Let ForeColor(ByVal vNewValue As OLE_COLOR)
NumTextbox.ForeColor = vNewValue
PropertyChanged "ForeColor"
End Property
我是一名新手,用VB6.0做了一个只能输入数字的文本框自定义控件,编译完后在新建的项目中插入该部件,双击该控件,出现GotFocus代码,即使不输入任何代代码的情况下运行程序,自定义的控件却消失了.
以下是我该自定义控件的代码,该自定义控件上只有一个名为NumTextBox的文本框Option ExplicitPrivate Sub UserControl_GotFocus()
NumTextBox_GotFocus
End SubPrivate Sub UserControl_KeyPress(KeyAscii As Integer)
NumTextBox_KeyPress (KeyAscii)
End SubPrivate Sub UserControl_ReadProperties(PropBag As PropertyBag)
Set NumTextbox.Font = PropBag.ReadProperty("Font", Ambient.Font)
NumTextbox.BackColor = PropBag.ReadProperty("BackColor", Ambient.BackColor)
NumTextbox.Text = PropBag.ReadProperty("Text", "")
NumTextbox.Enabled = PropBag.ReadProperty("Enabled", True)
NumTextbox.MaxLength = PropBag.ReadProperty("Maxlength", 0)
NumTextbox.Visible = PropBag.ReadProperty("Visible", True)
NumTextbox.HelpContextID = PropBag.ReadProperty("HelpContextID", 0)
NumTextbox.ForeColor = PropBag.ReadProperty("ForeColor", Ambient.ForeColor)
NumTextbox.ToolTipText = PropBag.ReadProperty("ToolTipText")
End Sub
Private Sub UserControl_WriteProperties(PropBag As PropertyBag)
Call PropBag.WriteProperty("Font", NumTextbox.Font, Ambient.Font)
Call PropBag.WriteProperty("BackColor", NumTextbox.BackColor, Ambient.BackColor)
Call PropBag.WriteProperty("Text", NumTextbox.Text, "")
Call PropBag.WriteProperty("Enabled", NumTextbox.Enabled, True)
Call PropBag.WriteProperty("MaxLength", NumTextbox.MaxLength, 0)
Call PropBag.WriteProperty("Visible", NumTextbox.Visible, True)
Call PropBag.WriteProperty("HelpContextID", NumTextbox.HelpContextID, 0)
Call PropBag.WriteProperty("ForeColor", NumTextbox.ForeColor, Ambient.ForeColor)
Call PropBag.WriteProperty("ToolTipText", NumTextbox.ToolTipText)
End SubPrivate Sub NumTextBox_KeyPress(KeyAscii As Integer)
Select Case KeyAscii
Case Asc("-") '允许负数
If NumTextbox.SelStart = 0 Then
If Left(NumTextbox.Text, 1) = "-" Then
KeyAscii = 0
Beep
End If
Else
KeyAscii = 0
Beep
End If
Case 8
'无变化,退格键不屏蔽
' Case Asc(0)
' If Left(NumTextBox.Text, 1) = "0" Then
' KeyAscii = 0
' End If
Case Asc(".") '46 '允许小数点
If InStr(NumTextbox.Text, ".") Or Len(NumTextbox.Text) = 0 Or NumTextbox.SelText = NumTextbox.Text Then
KeyAscii = 0
Beep
End If
Case Is < Asc(0) '48
KeyAscii = 0
Beep
Case Is > Asc(9) '57
KeyAscii = 0
Beep
Case Else
If Left(NumTextbox.Text, 1) = "0" And Mid(NumTextbox.Text, 2, 1) <> "." Then
KeyAscii = 0
Beep
End If
End Select
End Sub
Private Sub UserControl_Resize()
NumTextbox.Left = 0
NumTextbox.Top = 0
NumTextbox.Height = UserControl.Height
NumTextbox.Width = UserControl.Width
End SubPrivate Sub NumTextBox_GotFocus()
NumTextbox.Left = 0
NumTextbox.Top = 0
NumTextbox.Height = UserControl.Height
NumTextbox.Width = UserControl.Width
NumTextbox.SelStart = 0
NumTextbox.SelLength = Len(NumTextbox.Text)
End Sub
Public Property Get BackColor() As OLE_COLOR
BackColor = NumTextbox.BackColor
End Property
Public Property Let BackColor(ByVal New_Color As OLE_COLOR)
NumTextbox.BackColor = New_Color
PropertyChanged "BackColor"
End PropertyPublic Property Get Font() As Font
Set Font = NumTextbox.Font
End Property
Public Property Set Font(ByVal New_Font As Font)
Set NumTextbox.Font = New_Font
PropertyChanged "Font"
End PropertyPublic Property Get Text() As String
Text = NumTextbox.Text
End Property
Public Property Let Text(ByVal New_Text As String)
NumTextbox.Text = New_Text
PropertyChanged "Text"
End Property
Public Property Get Enabled() As Boolean
Enabled = NumTextbox.Enabled
End Property
Public Property Let Enabled(ByVal New_value As Boolean)
NumTextbox.Enabled = New_value
PropertyChanged "Enabled"
End PropertyPublic Property Get MaxLength() As Integer
MaxLength = NumTextbox.MaxLength
End PropertyPublic Property Let MaxLength(ByVal vNewValue As Integer)
NumTextbox.MaxLength = vNewValue
PropertyChanged "MaxLength"
End PropertyPublic Property Get ToolTipText() As String
ToolTipText = NumTextbox.ToolTipText
End PropertyPublic Property Let ToolTipText(ByVal vNewValue As String)
NumTextbox.ToolTipText = vNewValue
PropertyChanged "ToolTipText"
End PropertyPublic Property Get Visible() As Boolean
Visible = NumTextbox.Visible
End PropertyPublic Property Let Visible(ByVal vNewValue As Boolean)
NumTextbox.Visible = vNewValue
PropertyChanged "Visible"
End PropertyPublic Property Get HelpContextID() As Integer
HelpContextID = NumTextbox.HelpContextID
End PropertyPublic Property Let HelpContextID(ByVal vNewValue As Integer)
NumTextbox.HelpContextID = vNewValue
PropertyChanged "HelpContextID"
End PropertyPublic Property Get ForeColor() As OLE_COLOR
ForeColor = NumTextbox.ForeColor
End PropertyPublic Property Let ForeColor(ByVal vNewValue As OLE_COLOR)
NumTextbox.ForeColor = vNewValue
PropertyChanged "ForeColor"
End Property
Call PropBag.WriteProperty("Visible", NumTextBox.Visible, True)出了问题,我把它注释掉就没问题了,各位大侠能帮我解释一下吗,谢了
不过我总算弄明白了那到底是为什么了,因为自定义控件本身就有Visible和ToolTipText的属性,只要删除这两个定义,就OK了