Class with combobox options

Hobbyist_programmer 621 Reputation points
2020-12-14T18:46:06.91+00:00

Hallo,

I am trying to make a class object which shows a options in a combobox and takes a parameters according to that object selection.

Lets say I have an object or class called Box, I want to show some options in a Combobox, for example one to calculate Area and other for Volume, etc and selected option should be calculated.

Also is there a possibility to populate the textbox controls on the form according to the object inputs needed? or it is only done by hiding and unhiding?

Public Class Box

Public Property Length as Double
Public Property Width as Double
Public Property Height as Double

Public Readonly Property Volume as double
Public Readonly Property Area as double
Public Readonly Property Other as double

End class

Thanks

Developer technologies | VB
0 comments No comments
{count} votes

Answer accepted by question author
  1. Karen Payne MVP 35,596 Reputation points Volunteer Moderator
    2020-12-19T16:46:12.463+00:00

    @Hobbyist_programmer

    In regards to CatProduct, I left that out but Visual Studio can auto create it for you although here it is in the code below. Concerting unique properties, the base Product class should have all properties even if not used for some products.

    Public Class CatProduct  
        Public Property Category As Category  
        Public Property List As List(Of Product)  
    End Class  
    

    Think of when a database is properly designed for orders as shown below. Perhaps some orders don't use discount or some products don't use discontinue. It's incorrect to stored some product types differently instead in this type of situation the property is empty in both cases, no harm no foul.

    49645-a1.png

    But if you want to break the rules you can by using base classes and overridable properties as per below.

    Visuals

    49508-a1a.png

    Base class

    Public Class Base  
        Public Property Id() As Integer  
        Public Overridable Property Name() As String  
    End Class  
    

    Revised Product class

    Public Class Product  
        Inherits Base  
        Implements INotifyPropertyChanged  
      
        Private _name As String  
        Private _category As Category  
        Private _price As Decimal  
        Private _quantity As Integer  
      
        Public Overrides Property Name() As String  
            Get  
                Return _name  
            End Get  
            Set  
                _name = Value  
                OnPropertyChanged()  
            End Set  
        End Property  
      
        Public Property Category() As Category  
            Get  
                Return _category  
            End Get  
            Set  
                _category = Value  
                OnPropertyChanged()  
            End Set  
        End Property  
      
        Public Property Price() As Decimal  
            Get  
                Return _price  
            End Get  
            Set  
                _price = Value  
                OnPropertyChanged()  
            End Set  
        End Property  
      
        Public Property Quantity() As Integer  
            Get  
                Return _quantity  
            End Get  
            Set  
                _quantity = Value  
                OnPropertyChanged()  
            End Set  
        End Property  
      
        Public Overrides Function ToString() As String  
            Return $"{Name} {Price}"  
        End Function  
      
        Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged  
        Protected Overridable Sub OnPropertyChanged(<CallerMemberName> Optional propertyName As String = Nothing)  
            PropertyChangedEvent?.Invoke(Me, New PropertyChangedEventArgs(propertyName))  
        End Sub  
      
    End Class  
    

    Other class with all properties

    For above but includes cost while cost should truly be in the classes above.

    Public Class SomeOther  
        Inherits Product  
      
        Private _cost As Integer  
      
        Public Property Cost() As Integer  
            Get  
                Return _cost  
            End Get  
            Set  
                _cost = Value  
                OnPropertyChanged()  
            End Set  
        End Property  
      
    End Class  
    

5 additional answers

Sort by: Most helpful
  1. Hobbyist_programmer 621 Reputation points
    2020-12-19T13:38:18.43+00:00

    Hallo Karen, What if i have some properties are common (like cost , quantity ) and some unique properties for the products?

    I get CatProduct not defined and results as error.

    0 comments No comments

Your answer

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