【问题标题】:Cannot set some values of an usercontrol from the VS properties Window无法从 VS 属性窗口设置用户控件的某些值
【发布时间】:2013-10-21 17:26:08
【问题描述】:

在 Winforms 用户控件中,我已将 ProgressBar 集成到 Listview 中,这是属性之一:

''' <summary>
''' The ListView ProgressBar BorderColor
''' </summary>
Public Property ProgressBar_BorderColor As Pen
    Get
        Return _progressBar_bordercolor
    End Get
    Set(ByVal value As Pen)
        _progressBar_bordercolor = value
    End Set
End Property

嗯,问题是在属性窗口中我无法更改钢笔和画笔的值。

我可以通过编写代码手动更改值,但不能在属性窗口中更改。

我在编码属性时做错了什么,或者这些值不能在任何其他控件的属性窗口中更改,而不仅仅是我的控件?为什么?

我需要修改什么才能在我的用户控件的属性窗口中更改这些值?

【问题讨论】:

  • 我很确定这是可能的,但你必须实现自己的类型转换器或自定义编辑器用于您自己的控制。
  • @King King 谢谢你的评论,你能帮我找到关于如何做到这一点的在线参考/教程吗?对我来说是全新的。我认为 InDesign-Time 编辑器应该比类型转换器更难,但无论如何我认为我从未见过你的意思的类型转换器.. 我不知道要做到多难那个类型转换器。
  • @King King 您的意思是将属性类型设置为颜色,然后将该颜色转换为笔/画笔?如果是,那么我认为我可以做到,看起来很容易,但无论如何我想知道是否有更“本机”的方式可以在属性窗口中更改这些值。
  • UIDesigners 可能很难写。基于属性 NAMES,您真的只想为各种 ProgressBar 元素指定颜色,而不是实际的 GDI 项目(钢笔、画笔等)。在内部,您可以使用私有函数来获取指定颜色的 getPen getBrush - 比 UIDesigner 容易得多。
  • 谢谢,那么这个信息问题就解决了。

标签: c# vb.net winforms visual-studio-2012 user-controls


【解决方案1】:

最好在使用时创建 GDI 对象,以便正确处理它们:

Using g as Graphics = Graphics.FromWhereEver, 
           P as New Pen(ProgressBar_BorderColor), 
           Br as New SolidBrush(ProgressBar_BackColor)

    ... draw and paint
    ... paint and draw
End Using          ' Graphics, Pen and Brush properly disposed of

它不会有很大的不同,因为我怀疑你会坐下来来回改变颜色作为一种爱好,但是当设置新的颜色值时它们不会被处理掉。

【讨论】:

    【解决方案2】:

    解决方案:

    · 刷上颜色 刷上颜色

    Private _progressBar_backcolor As SolidBrush = New SolidBrush(Color.Red)
    
    Public Property ProgressBar_BackColor As Color
        Get
            Return _progressBar_backcolor.Color
        End Get
        Set(ByVal value As Color)
            _progressBar_backcolor = New SolidBrush(value)
        End Set
    End Property
    

    · 笔到颜色 颜色到笔

    Private _progressBar_bordercolor As Pen = New Pen(Color.LightGray)
    
    Public Property ProgressBar_BorderColor As Color
        Get
            Return _progressBar_bordercolor.Color
        End Get
        Set(ByVal value As Color)
            _progressBar_bordercolor = New Pen(value)
        End Set
    End Property
    

    【讨论】:

    • 我很好奇缓存 GDI 对象的速度差异是什么,并认为您可能会感兴趣。在使用 Pen 和 Brush 写入 120 个位图的循环中,使用预制的 Pen 和 Brush 与为每次迭代创建新的位图之间的总差异为 100 毫秒。不用担心考虑Using 会为我们处理它们。
    猜你喜欢
    • 2016-05-10
    • 2016-11-08
    • 2022-09-23
    • 1970-01-01
    • 2011-03-12
    • 1970-01-01
    • 2010-09-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多