VB.NET 2015:UserControl和属性更改 [英] VB.NET 2015: UserControl and property change

查看:306
本文介绍了VB.NET 2015:UserControl和属性更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的用户控件有一个奇怪的问题.基本上,它由31个称为C1 .... C31的标签组成.我希望能够从申请表中:更改Cx(x为1..31)的背景和/或文本,所以我为Labels添加了一个类

I have a strange issue with a user control. Basically, it is composed with 31 labels called C1....C31. I want to be able, from an application form, to: Change Cx's (x being 1..31) background and/or text So i've added one class for the Labels

Public Class CellDay
Public Event PropertyItemChanged As EventHandler(Of ItemArgs)

Private _name As String   'handle the name of the Label
Private _color As Color   ' handles the background of the Label
Private _text As String   ' handle the Text of the Label

Public Sub New()
    _name = ""
    _color = Color.White
    _text = ""
End Sub

Public Sub New( pname As String,pcolor As Color, ptext As String)
    _name = pname
    _color = pcolor
    _text = ptext
End Sub

Public Property CellName As String
    Get
        Return _name
    End Get
    Set(value As String)
        If Not _name.Equals(value) Then
            _name = value
            OnItemCPropertyChanged()
        End If

    End Set
End Property
Public Property CellColor As Color
    Get
        Return _color
    End Get
    Set(value As Color)
        If _color <> value Then
            _color = value
            OnItemCPropertyChanged()
        End If

    End Set
End Property
Public Property CellText As String
    Get
        Return _text
    End Get
    Set(value As String)
        If Not _name.Equals(value) Then
            _text = value
            OnItemCPropertyChanged()
        End If

    End Set
End Property

Private Sub OnItemCPropertyChanged()

    RaiseEvent PropertyItemChanged(Me, New ItemArgs(_name, _color,_text))

End Sub
End Class

Public Class ItemArgs
Inherits EventArgs

Public Class ItemArgs
Inherits EventArgs
Public Sub New(ByVal pname As String, ByVal pcolor As Color, ByVal ptext As String)
    CellName = pname
    CellColor = pcolor
    CellText = ptext
End Sub

Public Property CellName As String
Public Property CellColor As Color
Public Property CellText As String
End Class

然后我创建一个班级,该班级是上一个班级的集合:

Then I create the class wich is a collection of previous class:

Imports System.Collections.ObjectModel
Public Class Days
Inherits Collection(Of CellDay)

End Class

在UserControl代码中,我有:

In the UserControl Code I have:

Public Class Calendrier
Inherits System.Windows.Forms.UserControl


Private _cellClicked As CellDay
Private _Cells As New Days


Public Sub New()

    ' Cet appel est requis par le concepteur.
    InitializeComponent()

    DrawDays()    '

End Sub

Public ReadOnly Property Days(ByVal Index As Integer) As CellDay
    Get
        Return _Cells(Index - 1)
    End Get
End Property


Private Sub DrawDays()
    Dim i As Int16
    Dim lblRef As Label

    _Cells.Clear()

    'now fill the collection
    For i = 1 To 31

        lblRef = Me.Controls("C" & (i).ToString)              

        Dim cell As New CellDay With {.CellText = i.ToString, .CellName = lblRef.Name, .CellColor = _bgcolor}
        AddHandler cell.PropertyItemChanged, AddressOf ItemPropChanged
        _Cells.Add(cell)

    Next

End Sub

Private Sub ItemPropChanged(sender As Object, ByVal args As ItemArgs)
    Dim Cell As CellDay
    Dim lblName As String, Labelx As Label

    Cell = CType(sender, CellDay)

    lblName = Cell.CellName
    Labelx = CType(Me.Controls(lblName), Label)

    With Labelx
        .BackColor = args.CellColor   'In debug mode I've checked that CellColor was as expected
        .Text = args.CellText
        .ForeColor = args.CellTextColor
    End With
End Sub

End Class

我将此控件(所有Labels的默认背景为灰色)放在表单上,​​并添加了一个带有代码的按钮:

I put this control (default background of all Labels are grey) on my form and added a button with the code:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    With Calendrier1.Days(15)

        .CellColor = Color.AliceBlue
        .CellText = "X"

    End With
End Sub


好,结果给出带有文本"X"的第15个标签.但是背景颜色是白色!我花了几个小时进行调试,但看不到为什么颜色不符合预期.任何人都可以看到"bug".用我的编码方式?非常感谢你的帮助 Olivier


Well the result gives the 15th Label with the text "X" BUT the background color is white! I spent hours with debuging but I can't see why the color is not as expected. Anyone sees the "bug" in the way I coded? Thanks a lot for your help Olivier

推荐答案

.CellColor = _bgcolor}

???????????似乎来自天空

??????????? Seems to come from the sky


这篇关于VB.NET 2015:UserControl和属性更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆