How to different object properties in one propertyGrid?-VBForums
Results 1 to 4 of 4

Thread: How to different object properties in one propertyGrid?

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2018
    Posts
    2

    How to different object properties in one propertyGrid?

    Does anyone kow how to show different custom objects properties in one ProertyGrid, show properties of each object (dragged or clicked event) in the same propertyGrid. For example,I have two classes for values corresponding to respective objects.

    classLine contains Length and Thickness
    classSurface contains Perimeter and Area

    The objects can be a collection of several lines and surfaces

    So, my idea is to do the following

    When a Line is selected/dragged&dropped:
    PropertyGrid Shows:
    Length: value
    Thickness: value

    Surface is is selected/dragged&dropped:
    PropertyGrid Shows:
    Perimeter: value
    Area: value

    I know I can show all properties but can't get the idea if only the properties of a specific object can be shown. Showing all is also a problem at the list of properties shown in the propertyGrid will be very long when more objects are involved, each with their corresponding properties (parameters).

  2. #2
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,460

    Re: How to different object properties in one propertyGrid?

    Set SelectedObject to the class instance you want to show its properties, e.g.

    Code:
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            PropertyGrid1.SelectedObject = Button1
        End Sub
    
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            PropertyGrid1.SelectedObject = Button2
        End Sub



  3. #3

    Thread Starter
    New Member
    Join Date
    Jan 2018
    Posts
    2

    Re: How to different object properties in one propertyGrid?

    Thank you 4x2y,
    Under objectSelected event, I am able to see the properties of each object in one PropertyGrid. But I couldn't save values. I have two classes and the code is as follows:

    Code:
    Dim propertiesL As New clsLine()
                Dim propertiesS As New clsSurface()
    
                If Object is Line Then propertyGrid1.SelectedObject = propertiesL
                If Object is Surface Then propertyGrid1.SelectedObject = propertiesS
    How can save changed values?

  4. #4
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,460

    Re: How to different object properties in one propertyGrid?

    Use the event PropertyValueChanged to know which Object and Property is changed.

    Here is an example

    Code:
    Option Strict On
    Option Explicit On
    
    Imports System.ComponentModel
    
    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim cLine As New Line
            PropertyGrid1.SelectedObject = cLine
        End Sub
    
        Private Sub PropertyGrid1_PropertyValueChanged(s As Object, e As PropertyValueChangedEventArgs) Handles PropertyGrid1.PropertyValueChanged
            If TypeOf PropertyGrid1.SelectedObject Is Line Then
                Dim cLine As Line = DirectCast(PropertyGrid1.SelectedObject, Line)
                MessageBox.Show(Me, "'" & e.ChangedItem.Label & "' has been changed from '" & e.OldValue.ToString & "' to '" & e.ChangedItem.Value.ToString & "'" & Environment.NewLine & Environment.NewLine & "save it somewhere.", My.Application.Info.Title, MessageBoxButtons.OK, MessageBoxIcon.Information)
            End If
        End Sub
    End Class
    
    Public Class Line
        Private _Visible As Boolean
        <Browsable(False)>'Hide from the user
        Public Property Visible() As Boolean
            Get
                Return _Visible
            End Get
            Set(ByVal value As Boolean)
                _Visible = value
            End Set
        End Property
    
        Private _Length As Integer = 10
        '<DisplayName("Line Length")>'Optional, display another name to the user
        <CategoryAttribute("Appearance")>'Optional, add to a group
        Public Property Length() As Integer
            Get
                Return _Length
            End Get
            Set(ByVal value As Integer)
                _Length = value
            End Set
        End Property
    
        Private _Thickness As Integer = 1
        <CategoryAttribute("Appearance")>'Optional, add to a group
        Public Property Thickness() As Integer
            Get
                Return _Thickness
            End Get
            Set(ByVal value As Integer)
                _Thickness = value
            End Set
        End Property
    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
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.