Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8081363
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T16:42:30+00:00 2026-06-05T16:42:30+00:00

I am using a PropertyGrid control to edit my class properties and I am

  • 0

I am using a PropertyGrid control to edit my class properties and I am trying to set certain properties read-only depending on other property settings.

This is the code of my class:

Imports System.ComponentModel
Imports System.Reflection

Public Class PropertyClass

    Private _someProperty As Boolean = False

    <DefaultValue(False)>
    Public Property SomeProperty As Boolean
        Get
            Return _someProperty
        End Get
        Set(value As Boolean)
            _someProperty = value
            If value Then
                SetReadOnlyProperty("SerialPortNum", True)
                SetReadOnlyProperty("IPAddress", False)
            Else
                SetReadOnlyProperty("SerialPortNum", False)
                SetReadOnlyProperty("IPAddress", True)
            End If
        End Set
    End Property

    Public Property IPAddress As String = "0.0.0.0"

    Public Property SerialPortNum As Integer = 0

    Private Sub SetReadOnlyProperty(ByVal propertyName As String, ByVal readOnlyValue As Boolean)
        Dim descriptor As PropertyDescriptor = TypeDescriptor.GetProperties(Me.GetType)(propertyName)
        Dim attrib As ReadOnlyAttribute = CType(descriptor.Attributes(GetType(ReadOnlyAttribute)), ReadOnlyAttribute)
        Dim isReadOnly As FieldInfo = attrib.GetType.GetField("isReadOnly", (BindingFlags.NonPublic Or BindingFlags.Instance))
        isReadOnly.SetValue(attrib, readOnlyValue)
    End Sub
End Class

This is the code I am using to edit the values:

    Dim c As New PropertyClass
    PropertyGrid1.SelectedObject = c

The problem is that when I set SomeProperty to True, nothing happens and when I then set it to False again it sets all properties Read-Only. Can someone see an error in my code?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T16:42:31+00:00Added an answer on June 5, 2026 at 4:42 pm

    Try decorating ALL of your class properties with the ReadOnly attribute:

    <[ReadOnly](False)> _
    Public Property SomeProperty As Boolean
      Get
        Return _someProperty
      End Get
      Set(value As Boolean)
        _someProperty = value
        If value Then
          SetReadOnlyProperty("SerialPortNum", True)
          SetReadOnlyProperty("IPAddress", False)
        Else
          SetReadOnlyProperty("SerialPortNum", False)
          SetReadOnlyProperty("IPAddress", True)
        End If
      End Set
    End Property
    
    <[ReadOnly](False)> _
    Public Property IPAddress As String = "0.0.0.0"
    
    <[ReadOnly](False)> _
    Public Property SerialPortNum As Integer = 0
    

    Found it from this Code Project: Enabling/disabling properties at runtime in the PropertyGrid

    In order for all this to work properly, it is important to statically define the ReadOnly attribute of every property of the class to whatever value you want. If not, changing the attribute at runtime that way will wrongly modify the attributes of every property of the class.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to open and read from a serial port using the System.IO.Ports.SerialPort class.
I'm using PropertyGrid to edit an object containing a collection. Collection is edited using
I'm using a WinForms property grid to display the properties of an object. However,
I'm using the PropertyGrid control in a WinForms application. The window itself binds to
I'm using PRISM and a new control which I found in CodePlex: WPF Property
I'm trying to build a quick administrative interface using the built in Windows.Forms PropertyGrid
I'm using the PropertyGrid control from DevExpress in multiple forms. I have a ImageComboBoxEdit
I'm using PropertyGrid control for editing some objects in my application. I'm using custom
I am using a .NET PropertyGrid control in my C# project. When the form
I am using a PropertyGrid to show properties from my objects. However, I'm also

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.