Class Propert definition

John Rutherford 21 Reputation points
2021-03-28T06:54:00.393+00:00

I have a class - Trav with a property Bearing

I am using WinForms VB.Net

I want to enter the string value in a textbox, validate entry and save value as a double so that I can use it in calculations. I want to return the value for display as a formatted string using INotifyPropertyChanged

Can I get some assistance on Property definition eg.

Public Property DisplayBear As String
        Get
            Return mDisplayBear
        End Get
        Set(value As String)
            If String.IsNullOrEmpty(value) Then
                value = "0"
            End If
            If ValidAngle(value) Then
                mDisplayBear = value
                NotifyPropertyChanged()
            Else
                Throw New Exception("Invalid Angle. Enter ddd.mmssss")
            End If

        End Set
    End Property

Thanks

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,326 questions
{count} votes

Accepted answer
  1. Karen Payne MVP 29,236 Reputation points Microsoft MVP
    2021-03-29T15:03:27.903+00:00

    Perhaps use a custom TextBox e.g.

    Public Class NumericTextbox
        Inherits TextBox
    
        Const WM_PASTE As Integer = &H302
        <Browsable(False)>
        Public ReadOnly Property AsDouble As Double
            Get
                If String.IsNullOrWhiteSpace(Text) Then
                    Return 0
                Else
                    Return CDbl(Text)
                End If
            End Get
        End Property
        Public ReadOnly Property AsDecimal As Decimal
            Get
                If String.IsNullOrWhiteSpace(Text) Then
                    Return 0
                Else
                    Return CDec(Text)
                End If
            End Get
        End Property
        Public ReadOnly Property AsInteger As Integer
            Get
                If String.IsNullOrWhiteSpace(Text) Then
                    Return 0
                Else
                    Return CInt(Text)
                End If
            End Get
        End Property
        Public ReadOnly Property IsInteger As Boolean
            Get
                If Integer.TryParse(Text, Nothing) Then
                    Return True
                Else
                    Return False
                End If
            End Get
        End Property
        Public Sub SetDoubleValue(value As Double)
            Text = value.ToString()
        End Sub
        Public Sub SetInteger(value As Integer)
            Text = value.ToString()
        End Sub
        Public Sub SetDecimal(value As Decimal)
            Text = value.ToString()
        End Sub
        Protected Overrides Sub OnKeyPress(ByVal e As KeyPressEventArgs)
            Dim value As String = Text
            value = value.Remove(SelectionStart, SelectionLength)
            value = value.Insert(SelectionStart, e.KeyChar)
    
            e.Handled = value.LastIndexOf("-", StringComparison.Ordinal) > 0 Or Not (Char.IsControl(e.KeyChar) OrElse
                Char.IsDigit(e.KeyChar) OrElse (e.KeyChar = "."c And Not Text.Contains(".") Or
                e.KeyChar = "."c And MyBase.SelectedText.Contains(".")) OrElse
                (e.KeyChar = "-"c And SelectionStart = 0))
    
            MyBase.OnKeyPress(e)
    
        End Sub
        Protected Overrides Sub WndProc(ByRef m As Message)
    
            If m.Msg = WM_PASTE Then
    
                Dim value As String = Text
    
                value = value.Remove(SelectionStart, SelectionLength)
                value = value.Insert(SelectionStart, Clipboard.GetText)
    
                Dim result As Decimal = 0
                If Not Decimal.TryParse(value, result) Then
                    Return
                End If
    
            End If
    
            MyBase.WndProc(m)
    
        End Sub
    End Class
    

    Example class

    Note I don't know the rules for validating the angle so I simply did a hard-wired dummy rule.

    Public Class Example
        Implements INotifyPropertyChanged
    
        Private _displayBear As Double
    
        Public Property DisplayBear As Double
            Get
                Return _displayBear
            End Get
            Set
    
                If ValidAngle(Value) Then
                    Console.WriteLine("Valid")
                    _displayBear = Value
                    OnPropertyChanged()
                Else
                    Console.WriteLine("No valid")
                End If
    
            End Set
        End Property
        Private Function ValidAngle(value As Double) As Boolean
            If value = 10D Then
                Return True
            Else
                Return False
            End If
        End Function
        Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
    
        Protected Overridable Sub OnPropertyChanged(<CallerMemberName> Optional ByVal propertyName As String = Nothing)
            PropertyChangedEvent?.Invoke(Me, New PropertyChangedEventArgs(propertyName))
        End Sub
    End Class
    

    Here the TextBox above is used.

    Usage 1

        Private Sub BearButton_Click(sender As Object, e As EventArgs) Handles Button3.Click
            Dim example As New Example With {.DisplayBear = BearNumericTextbox.AsDouble}
        End Sub
    

    Usage 2

    Private _example As New Example
    Private Sub BearButton_Click(sender As Object, e As EventArgs) Handles Button3.Click
        _example.DisplayBear = BearNumericTextbox.AsDouble
    End Sub
    
    No comments

1 additional answer

Sort by: Most helpful
  1. John Rutherford 21 Reputation points
    2021-03-30T02:15:27.583+00:00

    @Karen Payne MVP - many thanks for your reply. I'll try this in my code.

    No comments