转至繁体中文版     | 网站首页 | 图文教程 | 资源下载 | 站长博客 | 图片素材 | 武汉seo | 武汉网站优化 | 
最新公告:     敏韬网|教学资源学习资料永久免费分享站!  [mintao  2008年9月2日]        
您现在的位置: 学习笔记 >> 图文教程 >> 软件开发 >> VB.NET程序 >> 正文
自制控件的属性保存(WriteProperties、ReadProperties)         ★★★★

自制控件的属性保存(WriteProperties、ReadProperties)

作者:闵涛 文章来源:闵涛的学习笔记 点击数:863 更新时间:2009/4/23 18:59:12

   一年前,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

你也可以点击这里下载控件源程序

   水平有限,欢迎交流 MSN:Equn_cn@hotmail.com


没有相关教程
教程录入:mintao    责任编辑:mintao 
  • 上一篇教程:

  • 下一篇教程:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
      注:本站部分文章源于互联网,版权归原作者所有!如有侵权,请原作者与本站联系,本站将立即删除! 本站文章除特别注明外均可转载,但需注明出处! [MinTao学以致用网]
      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)

    同类栏目
    · C语言系列  · VB.NET程序
    · JAVA开发  · Delphi程序
    · 脚本语言
    更多内容
    热门推荐 更多内容
  • 没有教程
  • 赞助链接
    更多内容
    闵涛博文 更多关于武汉SEO的内容
    500 - 内部服务器错误。

    500 - 内部服务器错误。

    您查找的资源存在问题,因而无法显示。

    | 设为首页 |加入收藏 | 联系站长 | 友情链接 | 版权申明 | 广告服务
    MinTao学以致用网

    Copyright @ 2007-2012 敏韬网(敏而好学,文韬武略--MinTao.Net)(学习笔记) Inc All Rights Reserved.
    闵涛 投放广告、内容合作请Q我! E_mail:admin@mintao.net(欢迎提供学习资源)

    站长:MinTao ICP备案号:鄂ICP备11006601号-18

    闵涛站盟:医药大全-武穴网A打造BCD……
    咸宁网络警察报警平台