question

JohnRutherford-5874 avatar image
0 Votes"
JohnRutherford-5874 asked JohnRutherford-5874 answered

Class Propert definition

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
· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @JohnRutherford-5874,
>>Can I get some assistance on Property definition
Based on your code, what problem did you encounter.
Please explain in detail.
Best Regards,
Daniel Zhang

0 Votes 0 ·

Hi @DanielZhang-MSFT

The issue with the above code is that the value is not saved as a "converted" double ie. in this case in Radians eg. 180° (string) = PI Radians (double).

I'm not sure how to read in a string from Form, convert and save as double (within the Class) and be able to send back as string to the Form.

0 Votes 0 ·
karenpayneoregon avatar image
1 Vote"
karenpayneoregon answered

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





5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

JohnRutherford-5874 avatar image
0 Votes"
JohnRutherford-5874 answered

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

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.