CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Jul 2014
    Posts
    3

    IDataErrorInfo, implementing, binding to and propagating error messages back to UI

    VB.NET 2012, Windows Form Application
    Using the Person Class from Microsoft's web site below
    http://msdn.microsoft.com/en-us/libr...v=vs.110).aspx

    I bind TextBox1 to the "Age" property of the Person class in the form's Load event. This part works, each time the "Text" property of TextBox1 changes the code in the "Item" property of the Person class is executed.

    My question is how does one get the 'error condition' back to the UI where the user can see it when the project type is a (Windows Form Application)?

    Code:
    Public Class Form1
        Private _Person As New Person
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
            Me.TextBox1.DataBindings.Add("Text", _Person, "Age", False, DataSourceUpdateMode.OnPropertyChanged)
        End Sub
    
    End Class
    Code:
    Imports System.ComponentModel
    Public Class Person
        Implements IDataErrorInfo
    
        Private _age As Integer
        Public Property Age() As Integer
            Get
                Return _age
            End Get
            Set(ByVal value As Integer)
                _age = value
            End Set
        End Property
    
        Public ReadOnly Property [Error]() As String Implements IDataErrorInfo.Error
            Get
                Return Nothing
            End Get
        End Property
    
        Default Public ReadOnly Property Item(ByVal columnName As String) As String Implements IDataErrorInfo.Item
            Get
                Dim result As String = Nothing
    
                If columnName = "Age" Then
                    If Me._age < 0 OrElse Me._age > 150 Then
                        result = "Age must not be less than 0 or greater than 150."
                        ' When flow arrives here how to propagate back the UI, show ErrorProvider against TextBox1?
                        Debug.Print(_age.ToString & " " & result)
                    End If
                End If
                Return result
            End Get
        End Property
    End Class

  2. #2
    Join Date
    Jul 2014
    Posts
    3

    Re: IDataErrorInfo, implementing, binding to and propagating error messages back to U

    This works. Needed to add an Error Provider to the form and set the Error Provider's DataSource property to the Person object.
    Code:
    Public Class Form1
        Private _Person As New Person
        Private ep As New ErrorProvider
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
            Me.TextBox1.DataBindings.Add("Text", _Person, "Age", False, DataSourceUpdateMode.OnPropertyChanged)
            ep.ContainerControl = Me
            ep.DataSource = _Person
        End Sub
    
    End Class

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured