一年前,VB中在编制OCX控件时的一点心得,发出来一起共享。
控件的属性在运行与设计模式切换时,属性值为丢失,可以要用WriteProperties来保存属性值,再用ReadProperties读取,具体情况如下:
1、在设计模式往运行模式切换时,先用WriteProperties保存属性值,由于在运行时要使用该值,所以用ReadProperties读出。
2、在结速运行状态切换到设计模式时,不需要保存控件在运行状态的值,所以不调用WriteProperties保存,而直接读取程序员在设计模式下的值。
3、WriteProperties及ReadProperties事件只在设计调试中有效,编译为EXE文件后不再有效。此两事件及PropBag对象只是为了方便设计而存在。
例程:
'本控件是text与updown控件的结合,使用updown改变数据值
'也可手工改变text值
Dim m_UpMax As Long
Dim m_DownMin As Long
Dim m_value As Long
Dim m_Enabled As Boolean
Private Sub Text1_Change()
On Error GoTo Err
Dim txti As Long
'txti用来零时保存text1值
txti = CLng(text1.Text)
If txti > UpDown1.Max Then
text1.Text = UpDown1.Max
End If
If txti < UpDown1.Min Then
text1.Text = UpDown1.Min
End If
txti = CLng(text1.Text)
UpDown1.value = txti
Exit Sub
Err:
text1.Text = UpDown1.value
End Sub
控件的属性在运行与设计模式切换时,属性值为丢失,可以要用WriteProperties来保存属性值,再用ReadProperties读取,具体情况如下:
1、在设计模式往运行模式切换时,先用WriteProperties保存属性值,由于在运行时要使用该值,所以用ReadProperties读出。
2、在结速运行状态切换到设计模式时,不需要保存控件在运行状态的值,所以不调用WriteProperties保存,而直接读取程序员在设计模式下的值。
3、WriteProperties及ReadProperties事件只在设计调试中有效,编译为EXE文件后不再有效。此两事件及PropBag对象只是为了方便设计而存在。
例程:
'本控件是text与updown控件的结合,使用updown改变数据值
'也可手工改变text值
Dim m_UpMax As Long
Dim m_DownMin As Long
Dim m_value As Long
Dim m_Enabled As Boolean
Private Sub Text1_Change()
On Error GoTo Err
Dim txti As Long
'txti用来零时保存text1值
txti = CLng(text1.Text)
If txti > UpDown1.Max Then
text1.Text = UpDown1.Max
End If
If txti < UpDown1.Min Then
text1.Text = UpDown1.Min
End If
txti = CLng(text1.Text)
UpDown1.value = txti
Exit Sub
Err:
text1.Text = UpDown1.value
End Sub