2

screen capture of defect

I have a groupbox with child buttons. All the child buttons have their .Enabled databound to form-level booleans. I also databound the booleans to labels for the sake of troubleshooting. This is a minimal reproduction of a problem I'm having in a larger app.

Disabling the groupbox works fine.

Re-enabling the groupbox does not, as can be seen in the screen capture. Button3 ought to get re-enabled. It doesn't. Why?

Imports System.ComponentModel

Public Class Form1
    Implements INotifyPropertyChanged


    Private _canClone As Boolean = True
    Private _canEdit  As Boolean = True


    Public Property CanClone As Boolean
        Get
            Return _canClone
        End Get
        Set(value As Boolean)
            _canClone = value
            RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs("CanClone"))
        End Set
    End Property


    Public Property CanEdit    As Boolean
        Get
            Return _canEdit
        End Get
        Set(value As Boolean)
            _canEdit = value
            RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs("CanEdit"))
        End Set
    End Property



    Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged


    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        GroupBox1.Enabled = Not GroupBox1.Enabled
    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Button1.DataBindings.Add(New Binding("Enabled", Me, "CanClone"   , True, DataSourceUpdateMode.OnPropertyChanged))
        Button3.DataBindings.Add(New Binding("Enabled", Me, "CanEdit"    , True, DataSourceUpdateMode.OnPropertyChanged))

        Label1 .DataBindings.Add(New Binding("Text",    Me, "CanClone"   , True, DataSourceUpdateMode.OnPropertyChanged))
        Label3 .DataBindings.Add(New Binding("Text",    Me, "CanEdit"    , True, DataSourceUpdateMode.OnPropertyChanged))
    End Sub


End Class
amonroejj
  • 573
  • 4
  • 16
  • Not 100% sure why the GroupBox does that, but since you are using DataBinding, you shouldn't handle this with the GroupBox.Enabled property. Your click event should just do `CanEdit = Not CanEdit`, and `CanClone = Not CanClone`. – LarsTech Jan 13 '20 at 20:41
  • The group box needs to be disabled regardless so that the user can't interact with it while certain activities are going on. I can do an ugly hack like cacheCanClone = CanClone, disable, re-enable, CanClone = cacheCanClone, but I would only want to do that as a last resort. – amonroejj Jan 13 '20 at 21:20
  • 1
    But you have competing forces at play here. The CanEdit and CanClone properties versus the Parent container. Both want to influence the Enabled property of the buttons. – LarsTech Jan 13 '20 at 21:28
  • For what it's worth, the same thing happens when you use a Panel in place of the GroupBox. – amonroejj Jan 14 '20 at 20:27
  • Why would that be any different? It's the same issue. You will have to re-think how you want to do this. – LarsTech Jan 14 '20 at 20:28
  • I removed the GroupBox altogether in the real project, but the asymmetry of disable versus enable in the example given still warrants a good mechanical explanation. – amonroejj Jan 15 '20 at 14:50
  • You would have to update your code now. What is the button click doing? – LarsTech Jan 15 '20 at 14:57

0 Answers0