honkus061098
asked on
How can my bound user control update its datasource?
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
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
You are welcome.
ASKER
Gerald