troubleshooting Question

How can my bound user control update its datasource?

Avatar of honkus061098
honkus061098 asked on
Visual Basic ClassicC#Visual Basic.NET
3 Comments1 Solution764 ViewsLast Modified:
I am developing a .Net Windows Forms user control.  The control is a container for radio buttons, and exposes a custom property called "SelectedIndex".  The selectedIndex indicates which radiobutton's checked property is set to True.

I have bound the usercontrol to a property of type Enum on an object datasource.  The appropriate radiobutton is selected based upon the value of the enum when the control loads at runtime.

The problem that I have is that I'd like the object's property to be updated when the user selects a different radio button.

One way that I have found that does work (see code snippet) is to raise an event when the SelectedIndex property changes on the user control, catch that event in my form, and then update the object datasource by setting its enum property to the user control's SelectedIndex.

Is there a way to cause the databinding to work "both ways", like with the frameworks controls?

Thanks,
Gerald

Imports System.ComponentModel
Imports System.ComponentModel.Design
 
<Designer("System.Windows.Forms.Design.ParentControlDesigner,System.Design", GetType(IDesigner))> _
Public Class RadioPanel
 
    Public Event SelectedIndexChanged As EventHandler
 
    Private _list As New List(Of RadioButton)
 
    Public ReadOnly Property List() As RadioButton()
        Get
            Return _list.ToArray
        End Get
    End Property
 
    Protected Overrides Sub OnControlAdded(ByVal e As System.Windows.Forms.ControlEventArgs)
        If TypeOf e.Control Is RadioButton Then
            _list.Add(e.Control)
            AddHandler CType(e.Control, RadioButton).CheckedChanged, AddressOf opt_CheckedChanged
        End If
        MyBase.OnControlAdded(e)
    End Sub
 
    Protected Overrides Sub OnControlRemoved(ByVal e As System.Windows.Forms.ControlEventArgs)
        If TypeOf e.Control Is RadioButton Then
            _list.Remove(e.Control)
            RemoveHandler CType(e.Control, RadioButton).CheckedChanged, AddressOf opt_CheckedChanged
        End If
        MyBase.OnControlRemoved(e)
    End Sub
 
    Public Property SelectedIndex() As Integer
        Get
            For i As Integer = 0 To _list.Count - 1
                If _list(i).Checked Then Return i
            Next
            Return -1
        End Get
        Set(ByVal value As Integer)
            If value <> SelectedIndex Then
                If value = -1 Then
                    _list(SelectedIndex).Checked = False
                Else
                    _list(value).Checked = True
                End If
            End If
        End Set
    End Property
 
    Private Sub opt_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs)
        If TypeOf sender Is RadioButton Then
            If CType(sender, RadioButton).Checked Then
                RaiseEvent SelectedIndexChanged(Me, Nothing)
            End If
        End If
    End Sub
 
End Class
 
Friend Class MyForm
 
    Private Sub MyForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
 
        RadioPanel1.DataBindings.Add(New Binding("SelectedIndex", MyObjectDatasource, "EnumeratedType"))
        
    End Sub
 
    Private Sub RadioPanel1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles RadioPanel1.SelectedIndexChanged
 
        MyObjectDatasource.EnumeratedType = RadioPanel1.SelectedIndex
  
    End Sub
 
End Class
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 3 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 3 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros