Skip to content

Replies

0
Tony Tullemans
Tony Tullemans answered on May 12, 2011 4:33 AM

Never say never…

1. Create a module with these functions:

  <Extension()> _
  Public Function GetIndex(ByVal items As ValueListItemsCollection, ByVal dataValue As Object) As Integer
    Dim result As Integer = -1
    Try
      If dataValue IsNot Nothing Then
        Dim i As Integer = 0
        For Each item As Infragistics.Win.ValueListItem In items
          If item.DataValue IsNot Nothing AndAlso dataValue.ToString.Equals(CStr(item.DataValue)) Then
            result = i
            Exit For
          End If
          i += 1
        Next
      End If
    Catch ex As Exception
      Throw New Exception(String.Format("{0}.GetIndex Error", items.GetType.Name), ex)
    End Try
    Return result
  End Function
  <Extension()> _
  Public Function GetItem(ByVal items As ValueListItemsCollection, ByVal dataValue As Object) As ValueListItem
    Dim result As ValueListItem = Nothing
    Try
      If dataValue IsNot Nothing Then
        For Each item As Infragistics.Win.ValueListItem In items
          If item.DataValue IsNot Nothing AndAlso dataValue.ToString.Equals(CStr(item.DataValue)) Then
            result = item
            Exit For
          End If
        Next
      End If
    Catch ex As Exception
      Throw New Exception(String.Format("{0}.GetItem Error", items.GetType.Name), ex)
    End Try
    Return result
  End Function
  <Extension()> _
  Public Function GetUIItem(ByVal ctrl As Infragistics.Win.UltraWinEditors.UltraOptionSet, _
                            ByVal dataValue As Object) As OptionSetOptionButtonUIElement
    Dim result As OptionSetOptionButtonUIElement = Nothing
    Try
      If dataValue Is Nothing Then Throw New ArgumentNullException("The data value may not be null")
      Dim item As ValueListItem = ctrl.Items.GetItem(dataValue)
      If item IsNot Nothing Then
        For Each childElement As UIElement In ctrl.UIElement.ChildElements
          If TypeOf childElement Is OptionSetEmbeddableUIElement Then
            Dim embeddableElement As OptionSetEmbeddableUIElement = CType(childElement, _
                                     OptionSetEmbeddableUIElement)
            For Each subElement As UIElement In embeddableElement.ChildElements
              If TypeOf subElement Is OptionSetOptionButtonUIElement Then
                Dim buttonElement As OptionSetOptionButtonUIElement = CType(subElement, _
                                     OptionSetOptionButtonUIElement)
                If buttonElement.Text = item.DisplayText Then
                  result = buttonElement
                  Exit For
                End If
              End If
            Next
            Exit For
          End If
        Next
      End If
    Catch ex As Exception
      Throw New Exception(String.Format("{0}.GetUIItem Error", ctrl.GetType.Name), ex)
    End Try
    Return result
  End Function
  <Extension()> _
  Public Function IsItemEnabled(ByVal ctrl As Infragistics.Win.UltraWinEditors.UltraOptionSet, _
                                ByVal dataValue As Object) As Boolean
    Dim result As Boolean = False
    Try
      If dataValue Is Nothing Then Throw New ArgumentNullException("The data value may not be null")
      Dim buttonElement As OptionSetOptionButtonUIElement = ctrl.GetUIItem(dataValue)
      If buttonElement IsNot Nothing Then
        result = buttonElement.Enabled
      End If
    Catch ex As Exception
      Throw New Exception(String.Format("{0}.IsItemEnabled Error", ctrl.GetType.Name), ex)
    End Try
    Return result
  End Function
  <Extension()> _
  Public Sub EnableItem(ByVal ctrl As Infragistics.Win.UltraWinEditors.UltraOptionSet, _
                        ByVal dataValue As Object, ByVal enabled As Boolean)
    Try
      If dataValue Is Nothing Then Throw New ArgumentNullException("The data value may not be null")
      Dim buttonElement As OptionSetOptionButtonUIElement = ctrl.GetUIItem(dataValue)
      If buttonElement IsNot Nothing Then
        buttonElement.Enabled = enabled
      End If
    Catch ex As Exception
      Throw New Exception(String.Format("{0}.EnableItem Error", ctrl.GetType.Name), ex)
    End Try
  End Sub

2. Add code like this to the ValueChanged event handler so that disabled items cannot be selected:

 Private Sub UltraOptionSet1_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) _
                  Handles UltraOptionSet1.ValueChanged
    If UltraOptionSet1.Value IsNot Nothing Then
       If UltraOptionSet1.IsItemEnabled(UltraOptionSet1.Value) Then
          UltraOptionSet1.Tag = UltraOptionSet1.Value ' Store the last valid selected value.
          ' Do something else if you need to.
       Else
          UltraOptionSet1.Value = UltraOptionSet1.Tag
          UltraOptionSet1.FocusedIndex = UltraOptionSet1.Items.GetIndex(UltraOptionSet1.Tag)
          UltraOptionSet1.Refresh()
       End If
     End If
   End If
End Sub

3. Enable or disable items like this:

UltraOptionSet1.EnableItem(5, False)

This example will disable the first option item with a data value of 5.