开发者

User Control and Binding

I've created a custom control which consists of two radio buttons with their appearance set to "Button". This control is meant to act like two toggle buttons with an "On" toggle button and an "Off" toggle button. I have a public boolean property "isAOn" which is used to set the state of both buttons and I want to be able to bind this property to boolean values. I have imported the component model and set

Now when I add this to a form for a boolean value in one of my classes it doesn't seem to update the boolean value of the class when I change the button pressed.

Advice on how to resolve this issue and constructive criticism on class design is more than welcome.

Thanks!

Here is the code:

Imports System.ComponentModel

''#
<DefaultBindingProperty("isAOn")> _
Public Class ToggleButtons

    Private _isAOn As Boolean
    Private _aText As String
    Private _bText As String
    Private _aOnColor As Color
    Private _aOffColor As Color
    Private _bOnColor As Color
    Private _bOffColor As Color

    Public Sub New()

        ''# This call is required by the Windows Form Designer.
        InitializeComponent()

        ''# Add any initialization after the InitializeComponent() call.
        aOffColor = Color.LightGreen
        aOnColor = Color.DarkGreen
        bOffColor = Color.FromArgb(255, 192, 192)
        bOnColor = Color.Maroon
        isAOn = False
        aText = "A"
        bText = "B"
    End Sub

    Private Sub configButtons()
        If _isAOn Then
            rbA.Ch开发者_Go百科ecked = True
            rbA.BackColor = _aOnColor
            rbB.Checked = False
            rbB.BackColor = _bOffColor
        Else
            rbA.Checked = False
            rbA.BackColor = _aOffColor
            rbB.Checked = True
            rbB.BackColor = _bOnColor
        End If
        rbA.Text = aText
        rbB.Text = bText
    End Sub

    Public Property isAOn() As Boolean
        Get
            Return _isAOn
        End Get
        Set(ByVal value As Boolean)
            _isAOn = value
            configButtons()

        End Set
    End Property

    Private Sub rbOn_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbA.CheckedChanged
        isAOn = rbA.Checked
    End Sub

    Private Sub rbOff_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbB.CheckedChanged
        isAOn = Not rbB.Checked
    End Sub

    Public Property aText() As String
        Get
            Return _aText
        End Get
        Set(ByVal value As String)
            _aText = value
        End Set
    End Property
    Public Property bText() As String
        Get
            Return _bText
        End Get
        Set(ByVal value As String)
            _bText = value
        End Set
    End Property
    Public Property aOnColor() As Color
        Get
            Return _aOnColor
        End Get
        Set(ByVal value As Color)
            _aOnColor = value
        End Set
    End Property
    Public Property aOffColor() As Color
        Get
            Return _aOffColor
        End Get
        Set(ByVal value As Color)
            _aOffColor = value
        End Set
    End Property
    Public Property bOnColor() As Color
        Get
            Return _bOnColor
        End Get
        Set(ByVal value As Color)
            _bOnColor = value
        End Set
    End Property
    Public Property bOffColor() As Color
        Get
            Return _bOffColor
        End Get
        Set(ByVal value As Color)
            _bOffColor = value
        End Set
    End Property

End Class


You need to add an isAOnChanged event and raise it in the property setter.

By the way, properties and methods in .Net should be UpperCamelCased.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜