Nothing binds to my user control

DennisHarding

Member
Joined
Oct 25, 2009
Messages
11
Location
Tell City, Indiana
Programming Experience
10+
Nothing binds to my user control. It will bind to other controls.
code for UC property:
VB.NET:
#Region "IsOn"
    'Public Sub IsOnValue_Changed Handles
    Public Sub Me_IsOnChanged() Handles Me.IsOnChanged
        If IsOn = True Then
            If OnImagevalue = Nothing Then
                'MessageBox.Show("Control On Off True picture not set")
            Else
                PictureBox1.Image = Image.FromFile(OnImagevalue) 'Transparent
            End If
            'Me.BackColor = Color.OrangeRed
        Else
            If OffImagevalue = Nothing Then
                'MessageBox.Show("Control On Off False picture not set")
            Else
                PictureBox1.Image = Image.FromFile(OffImagevalue)
            End If
        End If
    End Sub
    Public Event IsOnChanged() ' Bindable(True),
    Public IsOnvalue As Boolean '
    <Editor(GetType(INotifyPropertyChanged), GetType(UITypeEditor))> _
    <Browsable(True), DebuggerBrowsable(True), _
     Category("Misc"), Description("Gets/Sets when the control is off or on."), System.ComponentModel.Bindable(True)> _
    Public Property IsOn() As Boolean
        Get
            IsOn = IsOnvalue
            Return IsOnvalue
        End Get
        Set(ByVal Value As Boolean)
            IsOnvalue = Value
            RaiseEvent IsOnChanged()
        End Set
    End Property
#End Region '"IsOn"
Binding the UC to a control works one way (CheckBox.checked changes UC)
Code for binding UC to CheckBox :
VB.NET:
UC1.DataBindings.Add(New Binding("IsOn", CheckBox2, "Checked"))
Code for binding CheckBox to UC (does not work; UC.IsOn changes but not CheckBox):
VB.NET:
CheckBox1.DataBindings.Add(New Binding("Checked", UC1, "IsOn"))
I am working with a Velleman K8055 project board that comes with a dll.
This one line of code works both ways (Vell.DO changes-Checkbox changes & checkbox changes-Vel.DO changes)
Code for Vel:
VB.NET:
K80551.DO1.DataBindings.Add(New Binding("Checked", CheckBox4, "Checked"))
At this point I would be happy if my UC could be bound to at all,
but would prefer it working two way like the Vell control.
Any help would be greatly appreciated.
BTW, I've played around with the IsOn property a lot
trying to get it to work: Bindable, INotifyPropertyChanged, etc.
It only has to bind (work) at runtime, not at designtime.

Thanks a million for any help.
 
I see you set the property as Bindable, have you tried binding the class?
<Bindable(True)> Your_UC Class
 
I also tried this:
VB.NET:
UC1.DataBindings.Add(New Binding("IsOn", CheckBox1, "Checked", False, DataSourceUpdateMode.OnPropertyChanged))
adding the DataSourceUpdateMode.OnPropertyChanged. It didnt work either. :confused::confused:
 
Something else here is still very confusing to me, as I said before the velleman works both ways with the one line:
VB.NET:
K80551.DO1.DataBindings.Add(New Binding("Checked", CheckBox4, "Checked"))

I've looked over their UC a lot and all I come up with is I'm not binding to a custom property but a checkbox on the control. If I add 2 checkboxes to my main form I still need 2 lines of code to get them to work both ways (databinding in both directions)

VB.NET:
CheckBox2.DataBindings.Add(New Binding("Checked", CheckBox3, "Checked"))
CheckBox3.DataBindings.Add(New Binding("Checked", CheckBox2, "Checked"))

Maybe I should make control outside of this project and save as a dll, then use it in this project? ?????
 
Found the answer, EXCELLENT.

This works the binding both ways with the one line of binding code.

VB.NET:
Imports System.ComponentModel

<System.ComponentModel.DefaultBindingProperty("IsOn")> _
Public Class UC1
    Implements INotifyPropertyChanged

    Private OnImageValue As String = "C:\Users\Public\Pictures\btst15h_0.gif"
    Private OffImageValue As String = "C:\Users\Public\Pictures\btst15h_1.gif"

    Public Sub New()
        InitializeComponent()
    End Sub

#Region "IsOn"
    Public Event PropertyChanged As PropertyChangedEventHandler _
        Implements INotifyPropertyChanged.PropertyChanged

    Private Sub NotifyPropertyChanged(ByVal info As String)
        RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(info))
    End Sub

    Private Sub IsOnChanged()
        If IsOnvalue = True Then
            If OnImageValue = Nothing Then
                'MessageBox.Show("Control On Off True picture not set")
            Else
                PictureBox1.Image = Image.FromFile(OnImageValue) 'Transparent
            End If
            'Me.BackColor = Color.OrangeRed
        Else
            If OffImageValue = Nothing Then
                'MessageBox.Show("Control On Off False picture not set")
            Else
                PictureBox1.Image = Image.FromFile(OffImageValue)
            End If
        End If
    End Sub

    Public IsOnvalue As Boolean '
    <Browsable(True), _
      DebuggerBrowsable(True), _
      Category("Misc"), _
      Description("Gets/Sets when the control is off or on."), _
      DefaultValue(False), _
      System.ComponentModel.Bindable(True)> _
      Public Property IsOn() As Boolean
        Get
            Return IsOnvalue
        End Get
        Set(ByVal Value As Boolean)
            If IsOnvalue <> Value Then
                IsOnvalue = Value
                IsOnChanged()
                NotifyPropertyChanged("IsOn")
            End If
        End Set
    End Property

#End Region '"IsOn"

End Class

Implements INotifyPropertyChanged seems to be the answer.
:D:D:D:D
 
Back
Top