【转】自制控件的属性保存(WriteProperties、ReadProperties)
2008-05-03 20:17
411 查看
本文转自:http://www.hbjjrb.com/Jishu/vb/200704/9980.html
一年前,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
Private Sub UpDown1_Change()
text1.Text = UpDown1.value
End Sub
Private Sub UserControl_Resize()
On Error Resume Next
Dim t As Long
Dim b As Long
t = UserControl.Width - UpDown1.Width
b = UserControl.Height - 1
text1.Width = t
text1.Height = b
text1.Move 0, 0
UpDown1.Move t, 0
UpDown1.Height = b
End Sub
Public Property Get UpMax() As Long
UpMax = m_UpMax
End Property
Public Property Let UpMax(ByVal vNewvalue As Long)
m_UpMax = vNewvalue
UpDown1.Max = m_UpMax
PropertyChanged "UpMax"
End Property
Public Property Get DownMin() As Long
DownMin = m_DownMin
End Property
Public Property Let DownMin(ByVal vNewvalue As Long)
m_DownMin = vNewvalue
UpDown1.Min = vNewvalue
PropertyChanged "DownMin"
End Property
Private Sub UserControl_WriteProperties(PropBag As PropertyBag)
Call PropBag.WriteProperty("UpMax", m_UpMax, 0)
Call PropBag.WriteProperty("DownMin", m_DownMin, 0)
Call PropBag.WriteProperty("value", CLng(text1.Text), 0)
Call PropBag.WriteProperty("Enabled", m_Enabled, True)
End Sub
Private Sub UserControl_ReadProperties(PropBag As PropertyBag)
'使用PropBag.ReadProperty时一定要有默认值,否则如果没有在设计时改变过属性值(
'即没有使用writeProperty保存属性值)就无法读取,会产生“运行时错误”
m_UpMax = PropBag.ReadProperty("UpMax", 0)
m_DownMin = PropBag.ReadProperty("DownMin", 0)
m_value = PropBag.ReadProperty("value", 0)
m_Enabled = PropBag.ReadProperty("Enabled", True)
UpDown1.Max = m_UpMax
UpDown1.Min = m_DownMin
UpDown1.value = m_value
UpDown1.Enabled = m_Enabled
text1.Text = m_value
text1.Enabled = m_Enabled
End Sub
Public Property Get value() As Long
Attribute value.VB_UserMemId = 0
m_value = CLng(text1.Text)
value = m_value
End Property
Public Property Let value(ByVal vNewvalue As Long)
If vNewvalue > m_UpMax Then
m_value = m_UpMax
ElseIf vNewvalue < m_DownMin Then
m_value = m_DownMin
Else
m_value = vNewvalue
End If
text1.Text = m_value
PropertyChanged "value"
End Property
Public Property
Get Enabled() As Boolean
Enabled = m_Enabled
End Property
Public Property Let Enabled(ByVal vNewvalue As Boolean)
m_Enabled = vNewvalue
UpDown1.Enabled = m_Enabled
text1.Enabled = m_Enabled
PropertyChanged "Enabled"
End Property
一年前,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
Private Sub UpDown1_Change()
text1.Text = UpDown1.value
End Sub
Private Sub UserControl_Resize()
On Error Resume Next
Dim t As Long
Dim b As Long
t = UserControl.Width - UpDown1.Width
b = UserControl.Height - 1
text1.Width = t
text1.Height = b
text1.Move 0, 0
UpDown1.Move t, 0
UpDown1.Height = b
End Sub
Public Property Get UpMax() As Long
UpMax = m_UpMax
End Property
Public Property Let UpMax(ByVal vNewvalue As Long)
m_UpMax = vNewvalue
UpDown1.Max = m_UpMax
PropertyChanged "UpMax"
End Property
Public Property Get DownMin() As Long
DownMin = m_DownMin
End Property
Public Property Let DownMin(ByVal vNewvalue As Long)
m_DownMin = vNewvalue
UpDown1.Min = vNewvalue
PropertyChanged "DownMin"
End Property
Private Sub UserControl_WriteProperties(PropBag As PropertyBag)
Call PropBag.WriteProperty("UpMax", m_UpMax, 0)
Call PropBag.WriteProperty("DownMin", m_DownMin, 0)
Call PropBag.WriteProperty("value", CLng(text1.Text), 0)
Call PropBag.WriteProperty("Enabled", m_Enabled, True)
End Sub
Private Sub UserControl_ReadProperties(PropBag As PropertyBag)
'使用PropBag.ReadProperty时一定要有默认值,否则如果没有在设计时改变过属性值(
'即没有使用writeProperty保存属性值)就无法读取,会产生“运行时错误”
m_UpMax = PropBag.ReadProperty("UpMax", 0)
m_DownMin = PropBag.ReadProperty("DownMin", 0)
m_value = PropBag.ReadProperty("value", 0)
m_Enabled = PropBag.ReadProperty("Enabled", True)
UpDown1.Max = m_UpMax
UpDown1.Min = m_DownMin
UpDown1.value = m_value
UpDown1.Enabled = m_Enabled
text1.Text = m_value
text1.Enabled = m_Enabled
End Sub
Public Property Get value() As Long
Attribute value.VB_UserMemId = 0
m_value = CLng(text1.Text)
value = m_value
End Property
Public Property Let value(ByVal vNewvalue As Long)
If vNewvalue > m_UpMax Then
m_value = m_UpMax
ElseIf vNewvalue < m_DownMin Then
m_value = m_DownMin
Else
m_value = vNewvalue
End If
text1.Text = m_value
PropertyChanged "value"
End Property
Public Property
Enabled = m_Enabled
End Property
Public Property Let Enabled(ByVal vNewvalue As Boolean)
m_Enabled = vNewvalue
UpDown1.Enabled = m_Enabled
text1.Enabled = m_Enabled
PropertyChanged "Enabled"
End Property
相关文章推荐
- 自制控件的属性保存(WriteProperties、ReadProperties)
- android 所有控件属性 保存下来记得看看
- 自定义控件(9)控制控件属性的保存方式PersistenceMode
- 控件 ViewState 属性的值保存去哪里了
- 测试Delphi控件属性保存到dfm
- VB6.0 ActiveX 控件开发详解 [第三章:属性的设计,属性映射,属性的初始化,属性的保存]
- DuiDesigner修改:增加对RichEdit控件属性的保存
- 复合控件和事件(2)——属性,页面要回发,属性要保存
- 复合控件和事件(2)——属性,页面要回发,属性要保存
- Windows开发应用(2)通过静态属性Properties来保存全局数据
- [经验]自定义ASP.NET服务器控件属性的状态不能保存的问题
- 遍历控件的属性ctrl.GetType().GetProperties()
- WPF自制控件如何定义依赖属性
- 控件属性clickable影响滑动手势响应问题
- 窗体主控件Dock属性设置为Fill后,被工具条盖住了
- 控件属性、事件持久化(转)
- FLASH 上传控件传输,服务端保存文件
- VC下自制一个MusicPlayer:调用activemovie控件
- Android UI 之TextView控件属性列表
- Log4j.properties 属性详解