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 8459831
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T13:26:14+00:00 2026-06-10T13:26:14+00:00

Possible Duplicate: Conditional “Browsable” Attribute I define AppSettings class that have a few properties.

  • 0

Possible Duplicate:
Conditional “Browsable” Attribute

I define AppSettings class that have a few properties. In my form, When I click Button1, I want show property 1 and 2(1,2 is show, Other properties are hide or not displayed), When click Button2, I want show property 2 and 3(1 is hide, 2,3 are show, Other properties are hide or not displayed), How can I do it ?

public class AppSettings
{
    [BrowsableAttribute(true), CategoryAttribute("Document Settings"), DefaultValueAttribute(true)]
    public bool SaveOnClose{ get; set; }

    [BrowsableAttribute(true), CategoryAttribute("Global Settings"), ReadOnlyAttribute(true), DefaultValueAttribute("Welcome to AppDev!")]
    public string GreetingText { get; set; }

    [BrowsableAttribute(true), DescriptionAttribute("The rate in milliseconds that the text will repeat."), CategoryAttribute("Global Settings"), DefaultValueAttribute(10)]
    public int MaxRepeatRate { get; set; }

    [BrowsableAttribute(true), CategoryAttribute("Global Settings"), DefaultValueAttribute(4)]
    public int ItemsInMRUList { get; set; }

    [BrowsableAttribute(true), DefaultValueAttribute(false)]
    public bool SettingsChanged { get; set; }

    [BrowsableAttribute(true), CategoryAttribute("Version"), DefaultValueAttribute("1.0"), ReadOnlyAttribute(true)]
    public string AppVersion { get; set; }
}

I want change dynamically BrowseAttribute to true or false. How can I do it ?

Form code is :

AppSettings AppSet = new AppSettings();

AppSet.AppVersion = "2.3";
AppSet.SaveOnClose = true;
AppSet.GreetingText = "Welcome to Dar!";
AppSet.ItemsInMRUList = 4;
AppSet.MaxRepeatRate = 10;
AppSet.SettingsChanged = false;

...

propertyGrid1.SelectedObject = AppSet;

this change has error:

public static bool state = true;
BrowsableAttribute(state)

error:

An attribute argument must be a constant expression, typeof expression
or array creation expression of an attribute parameter type

  • 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-10T13:26:16+00:00Added an answer on June 10, 2026 at 1:26 pm

    For filtering, I would just change the BrowsableAttributes of the PropertyGrid. In the following, I:

    • define a custom attribute, [DisplayMode(...)], which describes when something should be visible
      • override IsMatch to indicate when attributes should be considered equivalent
    • decorate some of the settings on your type, AppSettings, with the attribute
    • change the BrowsableAttributes on the grid, specifying a particular DisplayModeAttribute, and display
    • repeat with a different set enabled

    Here’s the code:

    using System.ComponentModel;
    using System;
    using System.Windows.Forms;
    
    [AttributeUsage(AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
    public class DisplayModeAttribute : Attribute
    {
        private readonly string mode;
        public DisplayModeAttribute(string mode)
        {
            this.mode = mode ?? "";
        }
        public override bool Match(object obj)
        {
            var other = obj as DisplayModeAttribute;
            if (other == null) return false;
    
            if (other.mode == mode) return true;
    
            // allow for a comma-separated match, in either direction
            if (mode.IndexOf(',') >= 0)
            {
                string[] tokens = mode.Split(',');
                if (Array.IndexOf(tokens, other.mode) >= 0) return true;
            }
            else if (other.mode.IndexOf(',') >= 0)
            {
                string[] tokens = other.mode.Split(',');
                if (Array.IndexOf(tokens, mode) >= 0) return true;
            }
            return false;
        }
    }
    
    public class AppSettings
    {
        [DisplayMode("A"), CategoryAttribute("Document Settings"), DefaultValueAttribute(true)]
        public bool SaveOnClose { get; set; }
    
        [DisplayMode("A,B")]
        [CategoryAttribute("Global Settings"), ReadOnlyAttribute(true), DefaultValueAttribute("Welcome to AppDev!")]
        public string GreetingText { get; set; }
    
        [DisplayMode("B"), BrowsableAttribute(true), DescriptionAttribute("The rate in milliseconds that the text will repeat."), CategoryAttribute("Global Settings"), DefaultValueAttribute(10)]
        public int MaxRepeatRate { get; set; }
    
        [BrowsableAttribute(true), CategoryAttribute("Global Settings"), DefaultValueAttribute(4)]
        public int ItemsInMRUList { get; set; }
    
        [BrowsableAttribute(true), DefaultValueAttribute(false)]
        public bool SettingsChanged { get; set; }
    
        [BrowsableAttribute(true), CategoryAttribute("Version"), DefaultValueAttribute("1.0"), ReadOnlyAttribute(true)]
        public string AppVersion { get; set; }
    }
    static class Program
    {
        [STAThread]
        static void Main()
        {
            using (var form = new Form())
            using (var grid = new PropertyGrid())
            {
                grid.Dock = DockStyle.Fill;
                grid.SelectedObject = new AppSettings();
                grid.BrowsableAttributes = new AttributeCollection(
                    new DisplayModeAttribute("A"));
                form.Controls.Add(grid);
                form.ShowDialog();
    
                grid.BrowsableAttributes = new AttributeCollection(
                    new DisplayModeAttribute("B"));
                form.ShowDialog();
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Conditional Check in Where clause I have a few parameters that I
Possible Duplicate: Where and why do I have to put the “template” and “typename”
Possible Duplicates: C/C++ pragma in define macro Conditional “pragma omp” How can I use
Possible Duplicate: php false place in condition I have noticed that a lot of
Possible Duplicate: is “else if” faster than “switch() case” ? I've encountered a lot
Possible Duplicate: What’s the “condition” in C interview question? void main() { if(CONDITION) printf(Hello
Possible Duplicate: “Least Astonishment” in Python: The Mutable Default Argument Edit: This has nothing
Possible Duplicate: Conditional operator cannot cast implicitly? I have run into a peculiar situation
Possible Duplicate: Linq: “Or” equivalent of Where() I posted a question about a week
Possible Duplicate: Conditional operator in Python? Is there a c-like operator in python that

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.