views:

24

answers:

1

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.Checked = 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
A: 

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.

SLaks
Thanks, but I think something more needs to be done than just adding the event and raising it.How should it be handled?Is there a way to automatically keep this two way binding?
clawson
No. All you need to do is create an event with the correct name and type (`EventHandler`) and fire it whenever the value changes.
SLaks
SLaks, i have added an event "Public Event isAOnChanged As EventHandler" and raised this event in the property setter, but the bound boolean property in the object is still not getting any activity.
clawson
I'm not sure why that wouldn't work; it works fine in my code. What are you binding it to?
SLaks
Working now! Great! Changed the Data Source Update Mode for the DataBinding from OnValidation to OnPropertyChanged and this pushes the change through immediately. Thanks for the help!
clawson