Showing object property value and its default value in two textboxes with binding?

Hobbyist_programmer 621 Reputation points
2020-12-23T14:06:05.137+00:00

Hallo,

I have a class object at present like below. I have this collection in binding list and with binding source it is bound to textbox controls. I have two text boxes , one to show the default value as read only and another textbox to enter value for the quantity. If no quantity entered manually, cost will be calculated with default quantity.

I would like to know is it possible to have only one property and to show its default property in one textbox or label , and get the value from another textbox? if the manual field cleared then it should calculated again with default value? Anyway to bind or simplify?

Public Class Test

Private QuantityValue as Integer = 1

Public Property Quantity() as Integer
        Get
            Return QuantityValue
        End Get
        Set(ByVal Value As Integer)
            QuantityValue = Value
        End Set
End property 

Public Property UserQuantity() as Integer
        Get
            Return UserQuantityValue
        End Get
        Set(ByVal Value As Integer)
            UserQuantityValue = Value
        End Set
End property 

Public Readonly Property Cost() as Double
        Get
        if UserQuantity>0 then   
                    Return UserQuantity*Price
        Else
            Return Quantity*price
        End if
        End Get

End property

End Class
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,724 questions
0 comments No comments
{count} votes

Accepted answer
  1. Xingyu Zhao-MSFT 5,366 Reputation points
    2020-12-24T02:55:22.07+00:00

    Hi @Hobbyist_programmer ,

    If no quantity entered manually, cost will be calculated with default quantity.

    I make a test based on your description, and you can refer to the following code.

    Public Class Form1  
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click  
            TextBox2.DataBindings.Clear()  
            Dim testLst As BindingList(Of Test) = New BindingList(Of Test)()  
            Dim result As Integer  
      
            If Integer.TryParse(TextBox1.Text, result) Then  
                testLst.Add(New Test() With {  
                    .Quantity = result  
                })  
            Else  
                testLst.Add(New Test())  
            End If  
      
            TextBox2.DataBindings.Add("Text", testLst(0), "Quantity")  
        End Sub  
    End Class  
    Public Class Test  
      
        Private QuantityValue As Integer = 1  
      
        Public Property Quantity() As Integer  
            Get  
                Return QuantityValue  
            End Get  
            Set(ByVal Value As Integer)  
                QuantityValue = Value  
            End Set  
        End Property  
      
    End Class  
    

    Result of my test.
    50992-gif.gif

    Best Regards,
    Xingyu Zhao
    *
    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Anonymous
    2020-12-23T15:34:41.867+00:00

    Hi

    Presumably there is more in the Class that you don't show (for example, the variable Price isn't shown as declared)

    Assuming the Price variable is valid then as for simplifying:

    You could delete all of the Quantity code and just use Return Price, which after all is just equivalent to 1 * Price.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.